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 1Sample Output
3 2给你N个数,然后要你对下面M个查询回答:(L,R,K)。回答第L个数到第R个数之间的第K小数的值是多少。其中任意给定的两个区间[Li,Ri]和[Lj,Rj]之间不存在包含关系
Treap,每次超出区间的删掉,不在区间内的加上,再找第k小值
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int maxn=100000+5;
int ch[maxn][2],val[maxn],counts[maxn],r[maxn],size[maxn],tot,root;
int num[maxn];
struct A
{
int l,r,k,id,ans;
}a[50005];
bool cmp1(A x,A y)
{
if(x.l == y.l) return x.r < y.r;
return x.l < y.l;
}
bool cmp2(A x,A y)
{
return x.id < y.id;
}
struct Treap
{
void newnode(int &rt,int v)
{
rt=++tot;
val[rt]=v;
ch[rt][0]=ch[rt][1]=0;
counts[rt]=size[rt]=1;
r[rt]=rand();
}
void pushup(int rt)
{
size[rt]=size[ch[rt][0]]+size[ch[rt][1]]+counts[rt];
}
void rate(int &x,int kind)
{
int y=ch[x][kind^1];
ch[x][kind^1]=ch[y][kind];
ch[y][kind]=x;
pushup(x);
pushup(y);
x=y;
}
void add(int &rt,int v)
{
if(rt==0)
{
newnode(rt,v);
return ;
}
if(v==val[rt]) counts[rt]++;
else
{
int kind=(v>val[rt]);
add(ch[rt][kind],v);
if(r[ch[rt][kind]]<r[rt])
rate(rt,kind^1);
}
pushup(rt);
}
int select(int rt,int k)
{
if(size[ch[rt][0]]>=k) return select(ch[rt][0],k);
if(size[ch[rt][0]]+counts[rt]>=k) return val[rt];
return select(ch[rt][1],k-size[ch[rt][0]]-counts[rt]);
}
void rmove(int &rt,int v)
{
if(val[rt]==v)
{
if(counts[rt]>1)
counts[rt]--;
else if(!ch[rt][0]&&!ch[rt][1])
{rt=0;return ;}
else
{
int kind=r[ch[rt][0]]<r[ch[rt][1]];
rate(rt,kind);
rmove(rt,v);
}
}
else rmove(ch[rt][v>val[rt]],v);
pushup(rt);
}
void init()
{
ch[0][0]=ch[0][1]=0;
size[0]=counts[0]=val[0]=0;
tot=root=0;
r[0]=(1LL<<31)-1;
newnode(root,2000000001);
}
}Treap;
int main()
{
int n,m,lastl,lastr;
while(~scanf("%d%d",&n,&m))
{
Treap.init();
for (int i=1; i<=n; i++)
scanf("%d",&num[i]);
for(int i = 1;i <=m ;i ++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].k);
a[i].id = i;
}
sort(a+1,a+1+m,cmp1);
for(int i = 1;i <= m;i ++)
{
if(i == 1)
{
for(int j = a[i].l; j <= a[i].r;j ++)
Treap.add(root, num[j]);
a[i].ans = Treap.select(root, a[i].k);
lastl = a[i].l;
lastr = a[i].r;
}
else
{
if (a[i].l>lastr)
{
for (int j=lastl; j<=lastr; j++)
Treap.rmove(root,num[j]);
for (int j=a[i].l; j<=a[i].r; j++)
Treap.add(root,num[j]);
}
else
{
for (int j=lastl; j<a[i].l; j++)
Treap.rmove(root,num[j]);
for (int j=lastr+1; j<=a[i].r; j++)
Treap.add(root,num[j]);
}
a[i].ans=Treap.select(root,a[i].k);
lastl=a[i].l;
lastr=a[i].r;
}
}
sort(a+1,a+1+m,cmp2);
for(int i = 1;i <= m;i ++)
printf("%d\n",a[i].ans);
}
return 0;
}