3224:TYVJ 1728 normal balance tree time limit: ten Sec Memory Limit: MB
Submit: 4350 Solved: 1769
[Submit] [Status] [Discuss] 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]
Source
Balance Tree
Well, since yesterday has been changed this code,, right-handed when the size of the 1,,,re a piece of Ah, mainly this is heavy.
AC Code
/************************************************************** problem:3224 user:kxh1995 language:c++ Resu lt:accepted time:484 Ms memory:47708 kb****************************************************************/#include &L t;stdio.h> #include <string.h> #define MIN (A, b) (a>b?b:a) #define INF 0xfffffffstruct s {int key,left,r ight,size,sc,cnt; }TREE[2001000]; int top,root; void Left_rot (int &x) {int y=tree[x].right; Tree[x].right=tree[y].left; Tree[y].left=x; Tree[y].size=tree[x].size; Tree[y].sc=tree[x].sc; tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1; tree[x].sc=tree[tree[x].left].sc+tree[tree[x].right].sc+tree[x].cnt; X=y; } void Right_rot (int &x) {int y=tree[x].left; Tree[x].left=tree[y].right; Tree[y].right=x; Tree[y].size=tree[x].size; Tree[y].sc=tree[x].sc; tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1; Tree[x].sc=tree[tree[x].left].sc+tree[tree[x].right].sc+tree[x].cnt; X=y; } void maintain (int &x,bool flag) {tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1; tree[x].sc=tree[tree[x].left].sc+tree[tree[x].right].sc+tree[x].cnt; if (flag==false) {if (tree[tree[tree[x].left].left].size>tree[tree[x].right].size) Right_ro t (x); else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size) {Le Ft_rot (Tree[x].left); Right_rot (x); } else return; } else {if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size) Left_rot (x) ; else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size) {right _rot (Tree[x].right); Left_rot (x); }else return; } maintain (Tree[x].left,false); Maintain (tree[x].right,true); Maintain (x,true); Maintain (X,FALSE); } void Insert (int &x,int key) {if (x==0) {x=++top; tree[x].left=0; tree[x].right=0; Tree[x].size=tree[x].sc=tree[x].cnt=1; Tree[x].key=key; } else {//tree[x].size++; if (Tree[x].key==key) {tree[x].cnt++; tree[x].sc++; } else if (Key<tree[x].key) {insert (Tree[x].left,key); Maintain (X,FALSE); } else {insert (Tree[x].right,key); Maintain (x,true); } maintain (X,key>=tree[x].key); }}void Remove (int &x,int key) {if (Tree[x].key==key) {if (tree[x].cnt>1) {tree[x ].cnt--; tree[x].sc--; } else if (!tree[x].left&&!tree[x].right) {x=0; } else if (! ( tree[x].left*tree[x].right)) X=tree[x].left+tree[x].right; else if (tree[tree[x].left].size>tree[tree[x].right].size) { Right_rot (x); Remove (Tree[x].right,key); Maintain (X,FALSE); } else {Left_rot (x); Remove (Tree[x].left,key); Maintain (x,true); }} else if (Key<tree[x].key) {remove (Tree[x].left,key); Maintain (x,true); } else {remove (Tree[x].right,key); Maintain (X,FALSE); }}int getmin (int x) {while (tree[x].left) X=tree[x].left; return tree[x].key; } int Getmax (int x) {while (tree[x].right) x=tree[x].right; return tree[x].key; } int pred (int &x,int y,int key) {if (x==0) {return tree[y].key; } if (Key>tree[x].key) return pred (Tree[x].right,x,key); else return pred (Tree[x].left,y,key); } int succ (int &x,int y,int key) {if (x==0) {return tree[y].key; } if (Key<tree[x].key) return succ (Tree[x].left,x,key); else return succ (Tree[x].right,y,key); }int get_min_k (int &x,int k)//Select K small number {if (tree[tree[x].left].sc<k&&k<=tree[tree[x].left].sc+ TREE[X].CNT) return tree[x].key; if (K<=tree[tree[x].left].sc) return Get_min_k (TREE[X].LEFT,K); else return Get_min_k (tree[x].right,k-tree[tree[x].left].sc-tree[x].cnt);} int rank (int &x,int key)//key row {if (Key<tree[x].key) { return rank (Tree[x].left,key); } else if (Key>tree[x].key) return rank (tree[x].right,key) +tree[tree[x].left].sc+tree[x].cnt; else return tree[tree[x].left].sc+1; } int main () {int n,m; while (scanf ("%d", &n)!=eof) {int i; top=root=0; while (n--) {int op,x; scanf ("%d%d", &op,&x); if (op==1) insert (ROOT,X); else if (op==2) {remove (root,x); } else if (op==3) {printf ("%d\n", Rank (root,x)) ; } else if (op==4) {printf ("%d\ N ", Get_min_k (root,x)); } else if (op==5) { printf ("%d\n", Pred (root,0,x)); } else printf ("%d\n", Succ (root,0,x)); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj topic 3224:tyvj 1728 Normal balance tree (SBT)