3224:TYVJ 1728 Normal Balance tree
Description
You need to write a data structure (refer to the title of the topic) to maintain some of the numbers, which need to provide the following actions:
1. Insert X number
2. Delete the number of x (if there are multiple identical numbers, because only one is deleted)
3. Query the rank of x number (if there are multiple identical numbers, because the output is the lowest ranking)
4. The number of queries ranked as X
5. Find the precursor of X (the precursor is defined as less than X and the maximum number)
6. The successor of X (subsequent definition is greater than X, and the smallest number)
Input
The first action N, which represents the number of operations, the following n rows have two numbers per row opt and x,opt denote the ordinal number of the operation (1<=OPT<=6)
Output
Output one number per line for the operation 3,4,5,6, indicating the corresponding answer
Sample Input10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output106465
84185
492737
HINT
Data range for 1.N: n<=100000
2. Data range for each number: [ -1e7,1e7]
The data is as follows Http://pan.baidu.com/s/1jHMJwO2
If you can't write it, go to Taobanzi.
#include <iostream>#include<cstdio>#include<cstdlib>using namespacestd;structdata{intL,r,v,size,rnd,w;} tr[5200005];intN,size,root,ans;voidUpdateintK//Update node Information{tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+TR[K].W;}voidRturn (int&k) { intT=tr[k].l;tr[k].l=tr[t].r;tr[t].r=K; Tr[t].size=tr[k].size;update (k); k=t;}voidLturn (int&k) { intT=tr[k].r;tr[k].r=tr[t].l;tr[t].l=K; Tr[t].size=tr[k].size;update (k); k=t;}voidInsertint&k,intx) { if(k==0) {size++;k=size; Tr[k].size=tr[k].w=1; tr[k].v=x;tr[k].rnd=rand (); return; } tr[k].size++; if(tr[k].v==x) tr[k].w++; Else if(x>tr[k].v) {Insert (TR[K].R,X); if(tr[tr[k].r].rnd<tr[k].rnd) Lturn (k); } Else{Insert (tr[k].l,x); if(tr[tr[k].l].rnd<tr[k].rnd) Rturn (k); }}voidDelint&k,intx) { if(k==0)return; if(tr[k].v==x) {if(tr[k].w>1) {TR[K].W--;tr[k].size--;return; } if(tr[k].l*tr[k].r==0) k=tr[k].l+TR[K].R; Else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd) Rturn (k), Del (K,X); ElseLturn (k), Del (K,X); } Else if(x>tr[k].v) Tr[k].size--, Del (TR[K].R,X); Elsetr[k].size--, Del (tr[k].l,x);}intQuery_rank (intKintx) { if(k==0)return 0; if(tr[k].v==x)returnTr[tr[k].l].size+1; Else if(x>tr[k].v)returntr[tr[k].l].size+tr[k].w+Query_rank (tr[k].r,x); Else returnQuery_rank (tr[k].l,x);}intQuery_num (intKintx) { if(k==0)return 0; if(x<=tr[tr[k].l].size)returnQuery_num (tr[k].l,x); Else if(x>tr[tr[k].l].size+TR[K].W)returnQuery_num (tr[k].r,x-tr[tr[k].l].size-TR[K].W); Else returntr[k].v;}voidQuery_pro (intKintx) { if(k==0)return; if(tr[k].v<x) {ans=K;query_pro (tr[k].r,x); } ElseQuery_pro (tr[k].l,x);}voidQuery_sub (intKintx) { if(k==0)return; if(tr[k].v>x) {ans=k;query_sub (tr[k].l,x); } Elsequery_sub (tr[k].r,x);}intMain () {scanf ("%d",&N); while(n--) { intop,x; scanf ("%d%d",&op,&x); if(OP = =1) Insert (ROOT,X); if(OP = =2) del (root,x); if(OP = =3) printf ("%d\n", Query_rank (root,x)); if(OP = =4) printf ("%d\n", Query_num (root,x)); if(OP = =5) ans =0, Query_pro (root,x), printf ("%d\n", TR[ANS].V); if(OP = =6) ans =0, Query_sub (root,x), printf ("%d\n", TR[ANS].V); }}
Bzoj 3224:tyvj 1728 normal balance tree treap