Problem Description
Give you a sequence and ask you the kth big number of a inteval.
Input
The first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
Output
For each test case, output m lines. Each line contains the kth big number.
Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
Sample Output
2
主席树和线段树还是有很多不一样,主席树就是把很多线段树连起来,除了更新的节点其他的都一样
先离散化
这道题目就是rt[r]-rt[l-1],看看第k个在哪边节点,然后往下找。
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
int n,m;
int ls[maxn*20],rs[maxn*20],sum[maxn*20],rt[maxn*20];
int tot,a[maxn],b[maxn];
void build(int l,int r,int &root)
{
root=++tot;
sum[root]=0;
if(l==r)
return ;
int mid=l+r>>1;
build(l,mid,ls[root]);
build(mid+1,r,rs[root]);
}
void update(int l,int r,int &root,int last,int val)
{
root=++tot;
ls[root]=ls[last];
rs[root]=rs[last];
sum[root]=sum[last]+1;
if(l==r)
return ;
int mid=l+r>>1;
if(mid>=val)
update(l,mid,ls[root],ls[last],val);
else
update(mid+1,r,rs[root],rs[last],val);
}
int query(int l,int r,int ql,int qr,int num)
{
if(l==r)
return l;
int mid=l+r>>1;
int pos=sum[ls[qr]]-sum[ls[ql]];
if(num<=pos)
return query(l,mid,ls[ql],ls[qr],num);
else
return query(mid+1,r,rs[ql],rs[qr],num-pos);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+1+n);
int all=unique(b+1,b+1+n)-(b+1);
tot=0;
build(1,all,rt[0]);
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+1+all,a[i])-b;
for(int i=1;i<=n;i++)
update(1,all,rt[i],rt[i-1],a[i]);
int l,r,k;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[query(1,all,rt[l-1],rt[r],k)]);
}
}
return 0;
}