C ++ - LUOGU3834 be persistent segment tree (Chairman of the tree) [template]

Oh, topics are so clear, what is the solution to a problem, you do not need it!

And on a poj2104 meaning of the questions exactly the same, only the data range * 2, you can directly copy the code past a fall

But I'm still dedicated to re-knock again, this kind of thing to plant trees hard thing!

Between just learning yesterday, naturally can not comprehend Insert operation subtlety

So today, when writing code first built a nlogn empty tree, easy to find (understand)? !

In fact, Insert operation to be competent entire process from tree to empty the achievements, it is mysterious!

Attach ac code is well understood:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=2e5+10;
 4 struct Node{int l,r,sum;};
 5 struct Data{int x,id;};
 6 bool cmp(Data a,Data b){return a.x<b.x;}
 7 Node t[MAXN<<5];Data a[MAXN];
 8 int rt[MAXN],cnt,rk[MAXN];
 9 void Build(int&x,int l,int r){//Contribution operation will either transfer or record the current reference node id, cnt global variables no longer used again after the recursive 
10      X CNT = ++; IF (R & lt == L) return ;
 . 11      int MID = (L + R & lt) >> . 1 ;
 12 is      the Build (T [X] .L, L, MID);
 13 is      the Build (T [X] .r, MID + . 1 , R & lt);
 14  }
 15  void the Insert ( int & A, int & X, int L, int R & lt ) {
 16      T [CNT ++] = T [X], X = CNT, T [X] .sum ++ ;
 . 17      IF (R & lt == L) return ; int MID = (L + R & lt) >> . 1 ;
 18 is      IF(a<=mid)Insert(a,t[x].l,l,mid);
19     else Insert(a,t[x].r,mid+1,r);
20 }
21 int Query(int L,int R,int k,int l,int r){
22     if(l==r)return l;int mid=(l+r)>>1;
23     int s=t[t[R].l].sum-t[t[L].l].sum;
24     if(k<=s)return Query(t[L].l,t[R].l,k,l,mid);
25     else return Query(t[L].r,t[R].r,k-s,mid+1,r);
26 }
27 int n,m,l,r,k;
28 void InputAndInit(){
29     cin>>n>>m;
30     for(int i=1;i<=n;i++)cin>>a[i].x,a[i].id=i;
31     sort(a+1,a+n+1,cmp);
32     for(int i=1;i<=n;i++)rk[a[i].id]=i;
33 }
34 int main(){
35     InputAndInit(),Build(rt[0],1,n);
36     for(int i=1;i<=n;i++)rt[i]=rt[i-1],Insert(rk[i],rt[i],1,n);
37     while(m--)cin>>l>>r>>k,cout<<a[Query(rt[l-1],rt[r],k,1,n)].x<<endl;    
38     return 0;
39 }

 

Guess you like

Origin www.cnblogs.com/JasonCow/p/12389226.html