Language:
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 Source
POJ Monthly--2006.02.26,zgl & twb
|
题意:给定一个n个元素的数组,m次询问,每次询问(l,r,k),即区间[l,r]中第k大的值,题目保证区间不存在包含关系,但可相交。
思路:先将m次询问存储,并对l从小到大排序,之后用treap动态维护第k大元素,存储到相应的res中。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100000+100;
int n,m;
int res[MAXN],a[MAXN];
struct node
{
node *ch[2];
int r,v,s;
node(int v):v(v)
{
s=1;
r=rand();
ch[0]=ch[1]=NULL;
}
bool operator <(const node &rhs) const
{
return r<rhs.r;
}
int cmp(int x)
{
if(x==v)
return -1;
return x<v ? 0:1;
}
void maintain()
{
s=1;
if(ch[0]!=NULL) s+=ch[0]->s;
if(ch[1]!=NULL) s+=ch[1]->s;
}
};
struct Treap
{
inline void rotate(node * &o,int d)
{
node *k=o->ch[d^1];
o->ch[d^1]=k->ch[d];
k->ch[d]=o;
o->maintain(); k->maintain();
o=k;
}
inline void insert(node * &o,int x)
{
if(o==NULL) o=new node(x);
else
{
int d=(x<o->v?0:1);
insert(o->ch[d],x);
if(o->ch[d]->r>o->r)
rotate(o,d^1);
}
o->maintain();
}
inline void remove(node * &o,int x)
{
int d=o->cmp(x);
if(d==-1)
{
node *u=o;
if(o->ch[0]!=NULL && o->ch[1]!=NULL)
{
int d2=(o->ch[0]->r>o->ch[1]->r? 1:0);
rotate(o,d2);remove(o->ch[d2],x);
}
else
{
if(o->ch[0]==NULL)
o=o->ch[1];
else
o=o->ch[0];
delete u;
}
}
else
remove(o->ch[d],x);
if(o!=NULL)
o->maintain();
}
inline int kth(node * o,int k)
{
if(o==NULL || k<=0 || k>o->s) return 0;
int s=(o->ch[0]==NULL ? 0: o->ch[0]->s);
if(k==s+1) return o->v;
else if(k<=s) return kth(o->ch[0],k);
else return kth(o->ch[1],k-s-1);
}
}treap;
struct Q
{
int l,r,k,id;
bool operator < (const Q &rhs) const
{
return l<rhs.l;
}
}query[MAXN];
int main()
{
//freopen("text.txt","r",stdin);
scanf("%d%d",&n,&m);
node *rt=NULL;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&query[i].l,&query[i].r,&query[i].k);
query[i].id=i;
}
sort(query+1,query+m+1);
int l=query[1].l,r=query[1].l;
for(int i=1;i<=m;i++)
{
while(l<query[i].l)
{
treap.remove(rt,a[l]);
l++;
}
while(r<=query[i].r)
{
treap.insert(rt,a[r]);
r++;
}
res[query[i].id]=treap.kth(rt,query[i].k);
}
for(int i=1;i<=m;i++)
printf("%d\n",res[i]);
return 0;
}