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
s
t
indicates the interval and k indicates the kth big number in interval s,t
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
题意:求区间第K大
solution: 主席树
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100007
int a[N], b[N], cnt[N*20], rt[N*20], rs[N*20], ls[N*20];
int n ,m, ql, qr, qk, sz, tot;
void build( int &o, int l, int r ){
o = ++tot;
cnt[o] = 0;
if ( l == r ) return ;
int mid = ( l + r ) >> 1;
build( ls[o], l, mid), build( rs[o], mid+1, r);
}
void update( int &o, int l, int r, int last, int p){
o = ++tot;
ls[o] = ls[last];
rs[o] = rs[last];
cnt[o] = cnt[last] + 1;
if ( l == r ) return ;
int mid = ( l + r ) >> 1;
if ( p <= mid ) update( ls[o], l, mid, ls[last], p);
else update( rs[o], mid+1, r, rs[last], p );
}
int query( int s, int t, int l, int r, int k){
if ( l == r ) return l;
int mid = ( l + r ) >> 1;
int ans = cnt[ls[t]] - cnt[ls[s]];
if ( k <= ans ) return query( ls[s], ls[t], l, mid, k);
else return query( rs[s], rs[t], mid+1, r, k-ans);
}
void work(){
scanf( "%d%d%d", &ql, &qr, &qk);
printf( "%d\n", b[query( rt[ql-1], rt[qr], 1, sz, qk)] );
}
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 );
sz = unique( b+1, b+1+n ) - (b+1);
tot = 0;
build( rt[0], 1, sz);
for ( int i = 1; i <= n; i++) a[i] = lower_bound( b+1, b+1+sz, a[i]) - b;
for ( int i = 1; i <= n; i++) update( rt[i], 1, sz, rt[i-1], a[i]);
while ( m-- ) work();
}
return 0;
}