4000 【POJ - 2761】Feed the dogs 【主席树 求静态区间第k大】
2017-11-02 13:20
429 查看
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 should 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 serious. 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 should 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 should contain the pretty value of the dog who got the food in the i-th feeding.
Sample Input
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1
Sample Output
3
2
分析 : 还好前几天硬着头皮把 可持久的01Trie弄懂了,主席树的主体思想和那个一样,每个数字都是一颗线段树,不过没有修改的部分直接连接上一个版本的。
代码 模板
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 should 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 should contain the pretty value of the dog who got the food in the i-th feeding.
Sample Input
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1
Sample Output
3
2
分析 : 还好前几天硬着头皮把 可持久的01Trie弄懂了,主席树的主体思想和那个一样,每个数字都是一颗线段树,不过没有修改的部分直接连接上一个版本的。
代码 模板
#include<algorithm> #include<cstdio> #include<cstring> #include<iostream> using namespace std; typedef pair<int,int>pii; #define first fi #define second se #define LL long long #define fread() freopen("in.txt","r",stdin) #define fwrite() freopen("out.txt","w",stdout) #define CLOSE() ios_base::sync_with_stdio(false) const int MAXN = 100001+11; const int MAXM = 1e6; const int mod = 1e9+7; const int inf = 0x3f3f3f3f; int root[MAXN<<5],lson[MAXN<<5],rson[MAXN<<5],sum[MAXN<<5],sz; void Build(int& rt,int le,int ri){ rt=++sz; sum[rt]=0; if(le==ri) return ; int mid=(le+ri)>>1; Build(lson[rt],le,mid); Build(rson[rt],mid+1,ri); } void Update(int pre,int &rt,int le,int ri,int val){ rt=++sz; lson[rt]=lson[pre],rson[rt]=rson[pre],sum[rt]=sum[pre]+1; if(le==ri) return ; int mid=(le+ri)>>1; if(val<=mid) Update(lson[pre],lson[rt],le,mid,val); else Update(rson[pre],rson[rt],mid+1,ri,val); } int Query(int st,int ed,int le,int ri,int k){ if(le==ri) return le; int mid=(le+ri)>>1; int t=sum[lson[ed]]-sum[lson[st]] ; if(k<=t) Query(lson[st],lson[ed],le,mid,k); else Query(rson[st],rson[ed],mid+1,ri,k-t); } int a[MAXN],X[MAXN],cnt; int main(){ CLOSE(); // fread(); // fwrite(); int n,m; while(~scanf("%d%d",&n,&m)){ for(int i=1;i<=n;i++) { scanf("%d",&a[i]); X[i]=a[i]; } sort(X+1,X+1+n); cnt=unique(X+1,X+1+n)-X-1; sz=0; Build(root[0],1,cnt); for(int i=1;i<=n;i++){ int t=lower_bound(X+1,X+1+cnt,a[i])-X; Update(root[i-1],root[i],1,cnt,t); } while(m--){ int x,y,k;scanf("%d%d%d",&x,&y,&k); int ans=Query(root[x-1],root[y],1,cnt,k); printf("%d\n",X[ans]); } } return 0; }
相关文章推荐
- POJ 2104 && POJ 2761 (静态区间第k大,主席树)
- POJ - 2104 K-th Number && POJ 2761 Feed the dogs(主席树静态区间第k大)
- POJ 2761 Feed the dogs 主席树(区间第k大)
- poj 2104 K-th Number (静态区间第k大,主席树)
- poj2761&&poj2104 主席树(静态区间第K大)
- 【POJ - 2104】K-th Number 【主席树 求静态区间第k大】
- POJ 2104 & HDU 2665 & POJ 2761 K-th Number (主席树入门题 区间第K大)
- POJ 2104 K-th Number (主席树 静态区间第K大)
- POJ 2761 Feed the dogs(树状数组求区间第K大)
- poj2761——treap,区间第K小值
- poj 2104 主席树(区间第k大)
- poj 2761 线段树(多次查询不覆盖区间的第k小数)
- HDU 2665 / POJ 2104 静态区间第k小 主席树
- POJ 2104 【主席树】【区间第K大】
- POJ 2104(K-th Number-区间第k大-主席树)
- POJ 2104 K-th Number 主席树(区间第k大)
- poj 2104 主席树(区间第k大)
- poj 2104 K-th Number(主席树,静态第k小)
- 【可持久化线段树】poj 2104 静态区间第k大
- POJ 2104(K-th Number-区间第k大-主席树)