luogu 3834 【模板】可持久化线段树 1(主席树)
我这种菜鸡还是%一下棒神比较好
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstdlib> 5 #include<cstring> 6 #include<algorithm> 7 #include<vector> 8 #include<queue> 9 #define inf 2139062143 10 #define ll long long 11 #define MAXN 200100 12 using namespace std; 13 inline int read() 14 { 15 int x=0,f=1;char ch=getchar(); 16 while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();} 17 while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();} 18 return x*f; 19 } 20 int n,T,cnt; 21 struct node{int ls,rs,sum;}tr[MAXN<<5]; 22 struct data{int val,pos;}g[MAXN]; 23 bool cmp(data a,data b) {return a.val<b.val;} 24 int rt[MAXN],rnk[MAXN]; 25 void inst(int &k,int x,int l,int r) 26 { 27 //cout<<"I : "<<k<<" "<<x<<" "<<l<<" "<<r<<" "<<cnt<<endl; 28 tr[++cnt]=tr[k]; 29 k=cnt,tr[k].sum++; 30 if(l==r) return ; 31 int mid=(l+r)>>1; 32 if(x<=mid) inst(tr[k].ls,x,l,mid); 33 else inst(tr[k].rs,x,mid+1,r); 34 } 35 int query(int k,int a,int b,int l,int r) 36 { 37 //cout<<"Q : "<<k<<" "<<a<<" "<<b<<" "<<l<<" "<<r<<endl; 38 int res=tr[tr[b].ls].sum-tr[tr[a].ls].sum; 39 if(l==r) return l; 40 int mid=(l+r)>>1; 41 if(k<=res) return query(k,tr[a].ls,tr[b].ls,l,mid); 42 else return query(k-res,tr[a].rs,tr[b].rs,mid+1,r); 43 } 44 int main() 45 { 46 n=read(),T=read();int a,b,c; 47 for(int i=1;i<=n;i++) g[i].val=read(),g[i].pos=i; 48 sort(g+1,g+n+1,cmp); 49 for(int i=1;i<=n;i++) rnk[g[i].pos]=i; 50 for(int i=1;i<=n;i++) 51 { 52 rt[i]=rt[i-1]; 53 inst(rt[i],rnk[i],1,n); 54 } 55 while(T--) 56 { 57 a=read(),b=read(),c=read(); 58 printf("%d\n", g[query(c,rt[a-1],rt[b],1,n)].val); 59 } 60 }