3224:TYVJ 1728 normal balance tree time limit:10 Sec Memory limit:128 MB 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
Source
#include <cstdio>#include<cstdlib>#include<iostream>using namespacestd;#defineN 100010inlineintRead () {inttmp=0, pp=1; CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') pp=-1; Ch=GetChar ();} while(ch>='0'&&ch<='9') {tmp=tmp*Ten+ch-'0'; ch=GetChar ();} returntmp*pp;}intOp,x,ls[n],rs[n],w[n],a[n],siz[n],rnd[n],cnt,n,root,q,h;inlinevoidPuintP) {siz[p]=siz[ls[p]]+siz[rs[p]]+w[p];} Inlinevoidlxint&p) { intt=Rs[p]; RS[P]=Ls[t]; Ls[t]=p; PU (P);p u (t); P=T;} InlinevoidRxint&p) { intt=Ls[p]; LS[P]=Rs[t]; Rs[t]=p; PU (P);p u (t); P=T;} InlinevoidCrint&p,intv) { if(!p) {p=++cnt,a[p]=v,siz[p]=1, w[p]=1, rnd[p]=rand (); return; } if(V==a[p]) w[p]++; Else if(v<A[p]) {cr (LS[P],V); if(rnd[ls[p]]>rnd[p]) Rx (p); } Else{cr (RS[P],V); if(rnd[rs[p]]>rnd[p]) LX (p); } PU (P);} InlinevoidDelint&p,intv) { if(!p)return; if(v==A[p]) { if(w[p]>1) w[p]--; Else if(ls[p]*rs[p]==0) p=ls[p]+Rs[p]; Else if(rnd[ls[p]]>Rnd[rs[p]]) Rx (p), Del (P,V); ElseLX (P), Del (P,V); } Else if(v>A[p]) del (RS[P],V); Elsedel (LS[P],V); PU (p);} InlineintPaim (intPintv) { if(V==a[p])returnsiz[ls[p]]+1; Else if(V>a[p])returnsiz[ls[p]]+w[p]+Paim (RS[P],V); Else returnPaim (ls[p],v);} InlineintFinx (intPintv) { if(SIZ[LS[P]]>=V)returnFinx (LS[P],V); Else if(SIZ[LS[P]]+W[P]>=V)returnA[p]; Else returnFinx (rs[p],v-siz[ls[p]]-w[p]); }inlinevoidQqintPintv) { if(!p)return; Else if(v>A[p]) {Q=A[p]; QQ (RS[P],V); } ElseQQ (LS[P],V);} InlinevoidHjintPintv) { if(!p)return; Else if(v<A[p]) {h=A[p]; HJ (LS[P],V); } ElseHJ (RS[P],V);}intMain () {n=read (); while(n--) {op=read (); x=read (); if(op==1) CR (ROOT,X); Else if(op==2) del (root,x); Else if(op==3) printf ("%d\n", Paim (root,x)); Else if(op==4) printf ("%d\n", Finx (root,x)); Else if(op==5) {QQ (root,x);p rintf ("%d\n", q);} Else{HJ (root,x);p rintf ("%d\n", h);} }}
Bzoj 3224:tyvj 1728 Normal balance tree