[COGS 257] Dynamic ranking System tree Array set Chairman tree

Source: Internet
Author: User

The Chairman tree is the so-called durable line tree. Currently only the interval K value operation is played.

What about the changes? Because the chairman of the tree is also engaged in the prefix and, so the analogy array operation, set a tree array can be.

I would like to commemorate this type of tree nesting tree introduction

#include <iostream> #include <cstdio> #include <cstring>using namespace std; #define POS (i,a,b) for ( int i= (a); i<= (b); i++) #define N 51000#include<vector> #include <algorithm>int lowbit (int x) {return x & (-X);} int Tes;int n,m,sz,len;int a[n],root[n];vector<int> b;int Find (int x) {return lower_bound (B.begin (), B.end (), X)- B.begin () +1;} struct Haha{int lc,rc,sum;} Tree[n*100];int ans;struct xixi{int x,y,k;} Cun[n];int downx[n],downy[n];void init () {memset (tree,0,sizeof (tree)), memset (cun,0,sizeof (Cun)), b.clear ();p OS (i,0 , n+100) a[i]=root[i]=0;sz=0;ans=0;len=0;} void update (int &rt,int pos,int l,int r,int num) {if (!RT) rt=++sz;if (l==r) {Tree[rt].sum+=num;return;} int mid= (L+R) >>1;if (pos<=mid) update (tree[rt].lc,pos,l,mid,num); else Update (Tree[rt].rc,pos,mid+1,r,num) ; tree[rt].sum=tree[tree[rt].lc].sum+tree[tree[rt].rc].sum;} void Add (int x,int pos,int num) {while (x<=n) {update (root[x],pos,1,len,num); x+=lowbit (x);}} int query (int l,int r,int k) {if (l==r) returnL;int mid= (l+r) >>1;int sumx (0), Sumy (0), T (0);p OS (i,1,downx[0]) Sumx+=tree[tree[downx[i]].lc].sum;pos (i,1, Downy[0]) sumy+=tree[tree[downy[i]].lc].sum;t=sumy-sumx;if (t>=k) {pos (i,1,downx[0]) downx[i]=tree[downx[i]].lc ;p OS (i,1,downy[0]) Downy[i]=tree[downy[i]].lc;return query (l,mid,k);} Else{pos (I,1,downx[0]) Downx[i]=tree[downx[i]].rc;pos (i,1,downy[0]) Downy[i]=tree[downy[i]].rc;return query (mid+1 , r,k-t);}} int Query (int x,int y,int k) {Downx[0]=downy[0]=0;int tx=x,ty=y;while (tx>0) {Downx[++downx[0]]=root[tx];tx-=lowbit (TX);} while (ty>0) {downy[++downy[0]]=root[ty];ty-=lowbit (Ty);} return query (1,LEN,K);} int main () {scanf ("%d", &tes), while (tes--) {scanf ("%d%d", &n,&m), init ();p OS (i,1,n) scanf ("%d", &a[i]) , B.push_back (A[i]);p OS (i,1,m) {char c;cin>>c;int x,y,k;scanf ("%d%d", &x,&y); cun[i].x=x;cun[i].y=y;if (c== ' Q ') {scanf ("%d", &k); cun[i].k=k;} Else{b.push_back (y);}} Sort (B.begin (), B.end ()), B.erase (Unique (B.begin (), B.end ()), B.end ()), len=b.size ();p OS (i,1,n) {Add (i,fIND (A[i]), 1);} POS (i,1,m) {int x=cun[i].x,y=cun[i].y,k=cun[i].k;if (k) {ans=query (x-1,y,k); Ans=b[ans-1];p rintf ("%d\n", ans);} Else{add (X,find (a[x]), -1), add (X,find (y), 1); a[x]=y;}}} return 0;}

  

[COGS 257] Dynamic ranking System tree Array set Chairman tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.