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
Input # 1
10 1 106465 4 1 1 317721 1 460929 1 644985 1 84185 1 89851 6 81968 1 492737 5 493598
Output # 1
View Code
106465 84185 492 737
balance weights template tree maintenance problem
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define ll long long #define see(x) (cerr<<(#x)<<'='<<(x)<<endl) #define pb push_back #define inf 0x3f3f3f3f #define CLR(A,v) memset(A,v,sizeof A) typedef pair<int,int>pii; ////////////////////////////////// const int N=2e6+10; int son[N][2],fa[N],siz[N],cnt[N],val[N],ncnt,root; int chk(int x) { return son[fa[x]][1]==x; } void up(int x) { siz[x]=siz[son[x][0]]+siz[son[x][1]]+cnt[x]; } void rotate(int x) { int y=fa[x],z=fa[y],k=chk(x),w=son[x][k^1]; son[y][k]=w;fa[w]=y; son[z][chk(y)]=x;fa[x]=z; son[x][k^1]=y;fa[y]=x; up(x);up(y); } void splay(int x,int goal=0) { while(fa[x]!=goal) { int y=fa[x],z=fa[y]; if(z!=goal) { if(chk(y)==chk(z))rotate(y); else rotate(x); } rotate(x); } if(!goal)root=x; } void find(int x) { if(!root)return ; int pos=root; while(son[pos][x>val[pos]]&&x!=val[pos]) pos=son[pos][x>val[pos]]; splay(pos); } void insert(int x) { int pos=root,p=0; while(pos&&val[pos]!=x) { p=pos; pos=son[pos][x>val[pos]]; } if(pos)cnt[pos]++; else { pos=++ncnt; if(p)son[p][x>val[p]]=pos; son[pos][0]=son[pos][1]=0; val[pos]=x;fa[pos]=p; cnt[pos]=siz[pos]=1; } splay(pos); } int kth(int k) { int pos=root; while(1) { if(son[pos][0]&&siz[son[pos][0]]>=k) pos=son[pos][0]; else if(k>siz[son[pos][0]]+cnt[pos]) k-=siz[son[pos][0]]+cnt[pos],pos=son[pos][1]; else {splay(pos);return pos;} } } int pre(int x) { find(x); if(val[root]<x)return root; int pos=son[root][0]; while(son[pos][1])pos=son[pos][1]; splay(pos);return pos; } int succ(int x) { find(x); if(val[root]>x)return root; int pos=son[root][1]; while(son[pos][0])pos=son[pos][0]; splay(pos);return pos; } void remove(int x) { int last=pre(x),nex=succ(x); splay(last);splay(nex,last); int pos=son[nex][0]; if(cnt[pos]>1) { cnt[pos]--;splay(pos); } else son[nex][0]=0; up(root);up(nex); } void rank1(int x) { find(x); if(val[root]<=x) printf("%d\n",siz[son[root][0]]); else if(val[root]>x) printf("%d\n",siz[son[root][0]]+cnt[root]); int main() { int m;cin>>m; int a,b; insert(0x3f3f3f3f); insert(0xcfcfcfcf); while(m--) { scanf("%d%d",&a,&b); if(a==1)insert(b); if(a==2)remove(b); if(a==3)rank1(b); if(a==4)printf("%d\n", val[kth(b+1)] ); if(a==5)printf("%d\n",val[pre(b)]); if(a==6)printf("%d\n",val[succ(b)]); } return 0; }