Poj 2761 feed the dogs

Source: Internet
Author: User

The static range is k-large, and the Chairman tree ....


Feed the dogs
Time limit:6000 ms   Memory limit:65536 K
Total submissions:15491   Accepted:4780

Description

Wind loves pretty dogs very much, and she has n pet dogs. so Jiajia has to feed the dogs every day for wind. jiajia loves wind, but not the dogs, so Jiajia use a special way to feed the dogs. at lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. in each feeding, Jiajia choose an inteval [I, j], select the k-th pretty dog to feed. of course Jiajia has his own way of deciding the pretty value of each dog. it shoshould be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. if so, wind will be angry and the aftereffect will be serous. hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.

Your task is to help Jiajia calculate which dog ate the food after each feeding.

Input

The first line contains N and M, indicates the number of dogs and the number of feedings.

The second line contains N integers, describe the pretty value of each dog from left to right. You shoshould notice that the dog with lower pretty value is prettier.

Each of following M lines contain three integer I, J, K, it means that Jiajia feed the k-th pretty dog in this feeding.

You can assume that n <100001 and M <50001.

Output

Output file has M lines. the I-th line shoshould contain in the pretty value of the dog who got the food in the I-th feeding.

Sample Input

7 21 5 2 6 3 7 41 5 32 7 1

Sample output

32

Source

Poj monthly -- 2006.02.26, zgl & twb

[Submit] [Go Back] [Status] [discuss]


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=110000;int n,m,q;int a[maxn],t[maxn];int tot,T[maxn],lson[maxn*30],rson[maxn*30],c[maxn*30];void hash_init(){sort(t+1,t+1+n);m=unique(t+1,t+1+n)-t;}int hash(int x){return lower_bound(t+1,t+1+m,x)-t;}int build(int l,int r){int root=tot++,temp=root;c[root]=0;if(l!=r){int mid=(l+r)/2;lson[root]=build(l,mid);rson[root]=build(mid+1,r);}return temp;}int update(int root,int pos,int val){int newroot=tot++,temp=newroot;c[newroot]=c[root]+val;int l=1,r=m;while(l<r){int mid=(l+r)/2;if(pos<=mid){lson[newroot]=tot++; rson[newroot]=rson[root];root=lson[root]; newroot=lson[newroot];r=mid;}else{rson[newroot]=tot++; lson[newroot]=lson[root];root=rson[root]; newroot=rson[newroot];l=mid+1;}c[newroot]=c[root]+val;}return temp;}int query(int left_root,int right_root,int x){int l=1,r=m;while(l<r){int mid=(l+r)/2;int tt=c[lson[left_root]]-c[lson[right_root]];if(tt>=x){left_root=lson[left_root];right_root=lson[right_root];r=mid;}else{x-=tt;left_root=rson[left_root];right_root=rson[right_root];l=mid+1;}}return l;}int main(){while(scanf("%d%d",&n,&q)!=EOF){memset(c,0,sizeof(c)); tot=0;for(int i=1;i<=n;i++){scanf("%d",a+i);t[i]=a[i];}hash_init();T[n+1]=build(1,m);for(int i=n;i;i--){T[i]=update(T[i+1],hash(a[i]),1);}while(q--){int a,b,c;scanf("%d%d%d",&a,&b,&c);printf("%d\n",t[query(T[a],T[b+1],c)]);}}return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.