主席树模板
1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : @klay 5 * Date : 2018-08-27-16.18.54 6 * Description 主席树模板动态第k小 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #include<algorithm> 11 #include<vector> 12 #include<cstring> 13 #include<map> 14 #include<set> 15 #include<queue> 16 #include<bitset> 17 #include<utility> 18 #include<functional> 19 #include<iomanip> 20 #include<sstream> 21 #include<ctime> 22 #include<cassert> 23 #define A first 24 #define B second 25 #define mp make_pair 26 #define pb push_back 27 #define pw(x) (1ll << (x)) 28 #define sz(x) ((int)(x).size()) 29 #define all(x) (x).begin(),(x).end() 30 #define rep(i,l,r) for(int i=(l);i<(r);i++) 31 #define per(i,r,l) for(int i=(r);i>=(l);i--) 32 #define FOR(i,l,r) for(int i=(l);i<=(r);i++) 33 #define eps 1e-9 34 #define PIE acos(-1) 35 #define cl(a,b) memset(a,b,sizeof(a)) 36 #define fastio ios::sync_with_stdio(false);cin.tie(0); 37 #define lson l , mid , ls 38 #define rson mid + 1 , r , rs 39 #define ls (rt<<1) 40 #define rs (ls|1) 41 #define INF 0x3f3f3f3f 42 #define lowbit(x) (x&(-x)) 43 #define sqr(a) a*a 44 #define ll long long 45 #define vi vector<int> 46 #define pii pair<int, int> 47 #define dd(x) cout << #x << " = " << (x) << ", " 48 #define de(x) cout << #x << " = " << (x) << "\n" 49 #define endl "\n" 50 using namespace std; 51 //********************************** 52 const int maxn=1e6+7; 53 vector<int>v; 54 int n,m,cnt,x,y,k; 55 int root[maxn],a[maxn]; 56 struct Node{ 57 int l,r,sum; 58 Node(){} 59 }T[maxn*40]; 60 //********************************** 61 int getid(int x){return lower_bound(all(v),x)-v.begin()+1;} 62 void update(int l,int r,int &x,int y,int pos) 63 { 64 T[++cnt]=T[y],T[cnt].sum++,x=cnt; 65 if(l==r)return ; 66 int mid=l+r>>1; 67 if(mid>=pos)update(l,mid,T[x].l,T[y].l,pos); 68 else update(mid+1,r,T[x].r,T[y].r,pos); 69 } 70 int query(int l,int r,int x,int y,int k) 71 { 72 if(l==r)return l; 73 int mid=l+r>>1; 74 int sum=T[T[y].l].sum-T[T[x].l].sum; 75 if(sum>=k)return query(l,mid,T[x].l,T[y].l,k); 76 return query(mid+1,r,T[x].r,T[y].r,k-sum); 77 } 78 //********************************** 79 int main() 80 { 81 scanf("%d%d",&n,&m); 82 FOR(i,1,n)scanf("%d",&a[i]),v.pb(a[i]); 83 sort(all(v)),v.erase(unique(all(v)),v.end()); 84 FOR(i,1,n)update(1,n,root[i],root[i-1],getid(a[i])); 85 FOR(i,1,m)scanf("%d%d%d",&x,&y,&k),printf("%d\n",v[query(1,n,root[x-1],root[y],k)-1]); 86 return 0; 87 }