Question 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 question) to maintain the number. The following operations are required:
1. insert X Data
2. Delete the number of X (if there are multiple identical numbers, because only one is deleted)
3. query the ranking of the number of X (if there are multiple identical numbers, the output is the smallest ranking)
4. query the number of X
5. Evaluate the precursor of X (the precursor is defined as less than X and the maximum number)
6. Find the successor of X (defined as the number greater than X and the smallest number)
Input
The first behavior n indicates the number of operations. Each row in the N rows below has two numbers OPT and X, and opt indicates the number of operations (1 <= opt <= 6)
Output
For operations 3, 4, 5, 6, each row outputs a number, 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
1. Data range of N: n <= 100000
2. Data range of each number: [-1e7, 1e7]
Source
Balance Tree
Question
STL great! Vector directly, although it may be slower.
Code
1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 vector<int> v; 7 int n; 8 int opt,num; 9 inline int read(){10 int x=0,f=1;char ch=getchar();11 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}12 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}13 return x*f;14 }15 inline void insert(int x){16 v.insert(upper_bound(v.begin(),v.end(),x),x);17 }18 inline void del(int x){19 v.erase(lower_bound(v.begin(),v.end(),x));20 }21 inline int find(int x){22 return lower_bound(v.begin(),v.end(),x)-v.begin()+1; 23 }24 int main(){25 n=read();26 v.reserve(200000);27 for (int i=1;i<=n;i++){28 opt=read();num=read();29 switch(opt){30 case 1:insert(num);break;31 case 2:del(num);break;32 case 3:printf("%d\n",find(num));break;33 case 4:printf("%d\n",v[num-1]);break; 34 case 5:printf("%d\n",*--lower_bound(v.begin(),v.end(),num));break;35 case 6:printf("%d\n",*upper_bound(v.begin(),v.end(),num));break;36 }37 }38 return 0;39 }
View code
Bzoj 3224: tyvj 1728 normal Balance Tree