概述
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 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
题意:区间第k小
solution:主席树
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100007
struct Node{
int l, r, ls, rs, sum;
}tree[N<<5];
int rt[N], a[N], b[N], pos, cnt;
void build( int &nd, int l, int r ){
nd=++cnt;
tree[nd].l=l, tree[nd].r=r;
if( l==r ) return ;
int mid=(l+r)>>1;
build(tree[nd].ls,l,mid);
build(tree[nd].rs,mid+1,r);
}
void insert( int pre, int &nd ){
nd=++cnt;
tree[nd]=tree[pre];
tree[nd].sum++;
int mid=(tree[nd].l+tree[nd].r)>>1;
if( tree[nd].l==tree[nd].r ) return;
if( mid>=pos ) insert(tree[pre].ls,tree[nd].ls);
else insert(tree[pre].rs,tree[nd].rs);
}
int query( int pre, int nd, int k ){
if( tree[nd].ls==tree[nd].rs ) return b[tree[nd].l];
int cmp=tree[tree[nd].ls].sum-tree[tree[pre].ls].sum;
if( cmp>=k ) return query(tree[pre].ls,tree[nd].ls,k);
else return query(tree[pre].rs,tree[nd].rs,k-cmp);
}
int main(){
int n, m;
scanf("%d%d", &n, &m );
for ( int i=1; i<=n; b[i]=a[i], i++ ) scanf("%d", &a[i] );
cnt=0;
sort(b+1,b+n+1);
build(rt[0],1,n);
for ( int i=1; i<=n; i++ ){
pos=lower_bound(b+1,b+1+n,a[i])-b;
insert(rt[i-1],rt[i]);
}
while( m-- ){
int l, r, k;
scanf("%d%d%d", &l, &r, &k );
printf("%dn", query(rt[l-1],rt[r],k) );
}
return 0;
}
最后
以上就是想人陪面包为你收集整理的POJ2761[Feed the dogs ] 不带修改主席树题意:区间第k小solution:主席树的全部内容,希望文章能够帮你解决POJ2761[Feed the dogs ] 不带修改主席树题意:区间第k小solution:主席树所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复