P3369 [template] general balanced tree
Title Description
You need to write a data structure (refer to the subject of the title), to maintain some of the numbers, which need to provide the following:
- Insert x number x
- Delete the X- the X-number (the same number if more than one, because only a delete)
- Query x rank number x (rank number is defined as the ratio of the number of the current number of small + 1 + if the same number of a plurality of 1, because the minimum output rank)
- Queries ranked x number x
- Seeking X X precursor (precursor defined as less than X X, and the maximum number)
- Seeking X X successor (defined as greater than the subsequent X X, and the smallest number)
Input Format
The first line n the number n, the operation, the following n n lines each have two numbers opt O P T and X X, opt O P T represents the serial number of the operation ( . 1 \ Leq opt \ Leq. 6 . 1 ≤ O P t ≤ 6)
Output Format
For operation 3,4,5,6 3 , 4 , 5 , 6 a number of outputs per row, indicates that the corresponding answer
Sample input and output
10 1 106465 4 1 1 317721 1 460929 1 644985 1 84185 1 89851 6 81968 1 492737 5 493598
106465 84185 492737
Description / Tips
Constraints of time: 1000ms, 128M
1.n data ranges: n-\ 100000 Leq n- ≤ . 1 0 0 0 0 0
2. Each of the number of data ranges: [- 7 ^ {10}, {10 ^ 7}] [ - . 1 0 7 , . 1 0 7 ]
Source: Tyvj1728 original name: Plain balanced tree
In Acknowledgments
#include <bits / STDC ++ H.> #define INF 1E7 the using namespace STD; const int MAXN = 100005 , MAXM MAXN * = 20 is , N = 24 ; // 2 ^ 20 is int CH [MAXM] [ 2 ], V [MAXM ]; int CNT = . 1 ; void the Add ( int POS, int QX) { POS + = INF; // prevent negative int P = . 1 ; // the vacant node number 0 for ( int I = N; I> = 0 ; i--) { BOOL B = (POS >> I) & . 1 ; // judgment is not. 1 IF (! CH [P] [B]) CH [P] [B] = CNT ++; // dynamic prescription v [ P] + = QX; P = CH [P] [B]; } V [P] + = QX; // leaf nodes are not modified } int the Sum ( int POS) { POS + = + INF . 1 ; // + 1 is less than or equal the int RES = 0 , P = 1 ; for ( int I = N; I> = 0;i--) { bool b=(pos>>i)&1; if(b)// right res+=v[ch[p][0]]; p=ch[p][b]; if(!p) break; } return res; } int Get(int rank) { int p=1,res=0; for(int i=N;i>=0;i--) { if(v[ch[p][0]]>=rank) p=ch[p][0]; else rank-=v[ch[p][0]],p=ch[p][1],res|=1<<i; } return res-inf; } int main() { int T; scanf("%d",&T); while(T--) { int opt,x; scanf("%d%d",&opt,&x); if(opt==1) { Add(x,1); } if(opt==2) { Add(x,-1); } if(opt==3) { printf("%d\n",Sum(x-1)+1); } if(opt==4) { printf("%d\n",Get(x)); } if(opt==5) { int tmp=Sum(x-1)+1;//rank x printf("%d\n",Get(tmp-1)); } if(opt==6) { int tmp=Sum(x)+1;//rank x+1 printf("%d\n",Get(tmp)); } } return 0; }
Binary digital conversion (as a string) is present Trie tree segment tree thought that a similar bipartite weight