招生
Description
?浙江理工大學招生,一開始有0名學生報考,現在有如下幾種情況;
1.增加一名報考學生,報考學生成績為x;
2.一名成績為x的學生放棄報考。
3.從現在報考的學生來看,老師想知道如果要招生至少x名學生,需要將分數線最高設置為多少;
4.從現在報考的學生來看,如果分數線設置為x,能有幾名學生被錄取。
第一行先輸入一個n,表示有n次操作或查詢;
接下來n行,每行輸入兩個整數opt和x(用空格隔開):
如果opt為1,則增加一名報考學生,報考學生成績為x;
如果opt為2,則表示一名成績為x的學生放棄報考。
如果opt為3,從現在報考的學生來看,老師想知道如果要招生至少x名學生,需要將分數線最高設置為多少,輸出最高分數線。
如果opt為4,從現在報考的學生來看,如果分數線設置為x,能有幾名學生被錄取,輸出錄取人數。
對于每個輸出占一行。
n不超過50000;0<=x<=1000000;1<=k<=現在的學生數。
Input
第一行輸入一個n,接下來n行,每行輸出兩個整數opt,x,用空格隔開
Output
對于每次輸出, 輸出一個整數占一行
?
?
Sample Input
18 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 2 8 3 2 3 3 3 4 3 5 4 8 4 9 4 7Sample Output
11 10 9 7 4 4 5HINT
題解:
平衡樹Treap直接用
#include<bits/stdc++.h> #define fi first #define se second #define INF 0x3f3f3f3f #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define pqueue priority_queue #define NEW(a,b) memset(a,b,sizeof(a)) const double pi=4.0*atan(1.0); const double e=exp(1.0); const int maxn=1e6+8; typedef long long LL; typedef unsigned long long ULL; //typedef pair<LL,LL> P; const LL mod=1e9+7; const ULL base=1e7+7; using namespace std; struct node{int son[2];int siz;int key,w; }a[maxn]; int tot=0; int root=0,vis[maxn]; void up(int i){a[i].siz=a[a[i].son[0]].siz+a[a[i].son[1]].siz+1; } void Rotate(int &i,int d){int t=a[i].son[d];a[i].son[d]=a[t].son[!d];a[t].son[!d]=i;up(i);up(t);i=t; } void Insert(int &i,int key){if(i==0){i=++tot;a[i].siz=1;a[i].key=key;a[i].w=rand();return ;}a[i].siz++;if(a[i].key>=key) {Insert(a[i].son[0],key);if(a[a[i].son[0]].w<a[i].w) Rotate(i,0);}else{Insert(a[i].son[1],key);if(a[a[i].son[1]].w<a[i].w) Rotate(i,1);} } void Del(int &i,int key){if(a[i].key==key){if(a[i].son[0]*a[i].son[1]==0) {i=a[i].son[0]+a[i].son[1];return ;}if(a[a[i].son[0]].w>a[a[i].son[1]].w){Rotate(i,1);Del(a[i].son[0],key);}else{Rotate(i,0);Del(a[i].son[1],key);}}else if(a[i].key>key){Del(a[i].son[0],key);}else{Del(a[i].son[1],key);}up(i); } int Find(int i,int key){if(i==0) return 1;if(a[i].key>=key) return Find(a[i].son[0],key);else return a[a[i].son[0]].siz+Find(a[i].son[1],key)+1; } int Search(int i,int rak){if(a[a[i].son[0]].siz==rak-1) return a[i].key;if(a[a[i].son[0]].siz>=rak) return Search(a[i].son[0],rak);else return Search(a[i].son[1],rak-a[a[i].son[0]].siz-1); } int pre(int i,int key){if(i==0) return -10000008;if(a[i].key<key) return max(a[i].key,pre(a[i].son[1],key));return pre(a[i].son[0],key); } int bhe(int i,int key){if(i==0) return 10000008;if(a[i].key>key) return min(a[i].key,bhe(a[i].son[0],key));return bhe(a[i].son[1],key); } int main(){int n;freopen("data1.in","r",stdin);freopen("data1.out","w",stdout);scanf("%d",&n);int opt,x;int sum=0;while(n--){scanf("%d%d",&opt,&x);if(opt==1){Insert(root,x);sum++;vis[x]++;}if(opt==2){if(vis[x]){Del(root,x);sum--;vis[x]--;}}if(opt==3){printf("%d\n",Search(root,sum-x+1));}if(opt==4){printf("%d\n",sum-Find(root,x)+1);}} }?
總結