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
Source
HDU男生专场公开赛——赶在女生之前先过节(From WHU)
Recommend
zty | We have carefully selected several similar problems for you: 2660 2662 1754 2667 2663
关于主席树就是对每一个点都建一个线段树,对于线段树中没有变化的部分直接连接到上一个线段树,用以节省空间。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 200001;
const int maxn2= 6010;
const int inf =0x3f3f3f3f;
struct node{
int ls,rs,qu;
}s[maxn*40];
int w[maxn],ro[maxn],a[maxn],cnt;
void push_up(int p){
s[p].qu=s[s[p].rs].qu+s[s[p].ls].qu;
}
int build_tree(int l,int r){
int p=cnt++;
if(l==r){
s[p].qu=0; return p;
}
int mid=(l+r)>>1;
s[p].ls=build_tree(l,mid);
s[p].rs=build_tree(mid+1,r);
push_up(p);
return p;
}
int update(int old,int l,int r,int pos,int val){
int p=cnt++;
s[p]=s[old];
if(l==r&&r==pos){
s[p].qu+=val;
return p;
}
int mid=(l+r)>>1;
if(pos<=mid) s[p].ls=update(s[old].ls,l,mid,pos,val);
else s[p].rs=update(s[old].rs,mid+1,r,pos,val);
push_up(p);
return p;
}
int query(int l,int r,int old,int now,int k){
if(l==r) return l;
int mid=(l+r)>>1;
int res=s[s[now].ls].qu-s[s[old].ls].qu;
if(res>=k) return query(l,mid,s[old].ls,s[now].ls,k);
else return query(mid+1,r,s[old].rs,s[now].rs,k-res);
}
int main()
{
int n,m,t;cin>>t;
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
a[i]=w[i];
}
int res=0;
sort(w+1,w+1+n);cnt=0;
int len=unique(w+1,w+n+1)-w;
ro[res++]=build_tree(1,len);
for(int i=1;i<=n;i++){
int b=lower_bound(w+1,w+len,a[i])-w;
ro[res++]=update(ro[i-1],1,len,b,1);
}
for(int i=1;i<=m;i++){
int b,c,k;
scanf("%d%d%d",&b,&c,&k);
int ans=query(1,len,ro[b-1],ro[c],k);
printf("%d\n",w[ans]);
}
}
return 0;
}