Ordinary balanced tree
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 the value x.
Delete the value x (if a plurality of the same number, one should only be deleted).
Ranking the query value x (if a plurality of the same number, ranking should be the minimum output).
Queries ranked as the value of x.
Find the value of x of the precursor (precursor is defined as a maximum number less than x).
Find the value of x in the subsequent (subsequent is defined as the minimum number larger than x).
Note: Data must exist to ensure the results of the query.
Input format
first row n, the number of operations.
The next two lines each n opt and numbers x, opt denotes the operation number (1 <= opt <= 6).
Output format
for each line of output 3,4,5,6 operation a number indicates that the corresponding answer.
Data range
n≤100000, all numbers are in the 107 to -107.
Sample input:
. 8
. 1 10
. 1 20 is
. 1 30
. 3 20 is
. 4 2
2 10
. 5 25
. 6 -1
Output Sample:
2
20 is
20 is
20 is
Too terrible this data structure.
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+7,inf=0x3f3f3f3f;
int n,root,idx;
struct Node{
int l,r;
int key,val;//树的权值和堆的权值
int cnt,size;
}tr[N];
int get_node(int key)
{
tr[++idx].key=key;
tr[idx].val=rand();
tr[idx].cnt=tr[idx].size=1;
return idx;
}
void pushup(int p)
{
tr[p].size=tr[tr[p].l].size+tr[tr[p].r].size+tr[p].cnt;
}
void zig(int &p)
{
int q = tr[p].l;
tr[p].l = tr[q].r, tr[q].r = p, p = q;
pushup(tr[p].r), pushup(p);
}
void zag(int &p)
{
int q = tr[p].r;
tr[p].r = tr[q].l, tr[q].l = p, p = q;
pushup(tr[p].l), pushup(p);
}
void insert(int &p,int key)
{
if(!p) p=get_node(key);
else if(tr[p].key==key) tr[p].cnt++;
else if(tr[p].key>key){
insert(tr[p].l,key);
if(tr[tr[p].l].val>tr[p].val) zig(p);
}else {
insert(tr[p].r,key);
if(tr[tr[p].r].val>tr[p].val) zag(p);
}
pushup(p);
}
void remove(int &p,int key)
{
if(!p) return ;
if(tr[p].key==key){
if(tr[p].cnt>1) tr[p].cnt--;
else if(tr[p].l||tr[p].r){//如果不是叶子节点
if(!tr[p].r||tr[tr[p].l].val>tr[tr[p].r].val){//并且可以右旋
zig(p);
remove(tr[p].r,key);
}else {//否则就左旋
zag(p);
remove(tr[p].l,key);
}
}else p=0;//否则直接删除就可以了
}
else if(tr[p].key>key) remove(tr[p].l,key);
else remove(tr[p].r,key);
pushup(p);
}
int get_rank_by_key(int p,int key)
{
if(!p) return 0;
if(tr[p].key==key) return tr[tr[p].l].size+1;
if(tr[p].key>key) return get_rank_by_key(tr[p].l,key);
return tr[tr[p].l].size+tr[p].cnt+get_rank_by_key(tr[p].r,key);
}
int get_key_by_rank(int p,int rank)
{
if(!p) return 0x3f3f3f3f;
if(tr[tr[p].l].size>=rank) return get_key_by_rank(tr[p].l,rank);
if(tr[tr[p].l].size+tr[p].cnt>=rank) return tr[p].key;
return get_key_by_rank(tr[p].r,rank-tr[p].cnt-tr[tr[p].l].size);
}
int get_prev(int p,int key)
{
if(!p) return -0x3f3f3f3f;
if(tr[p].key>=key) return get_prev(tr[p].l,key);
return max(tr[p].key,get_prev(tr[p].r,key));
}
int get_next(int p,int key)
{
if(!p) return 0x3f3f3f3f;
if(tr[p].key<=key) return get_next(tr[p].r,key);
return min(tr[p].key,get_next(tr[p].l,key));
}
void build(){
get_node(-0x3f3f3f3f),get_node(0x3f3f3f3f);//哨兵
root=1,tr[1].r=2;
pushup(root);
}
int main(){
build();
int n; cin>>n;
while (n--) {
int opt, x;
cin >> opt >> x;
if (opt == 1) insert(root, x);
else if (opt == 2) remove(root, x);
else if (opt == 3) cout << get_rank_by_key(root, x) - 1 << endl;
else if (opt == 4) cout << get_key_by_rank(root, x + 1) << endl;
else if (opt == 5) cout << get_prev(root, x) << endl;
else cout << get_next(root, x) << endl;
}
return 0;
}