Llx2022 @ 2023-04-27 07:54:24
#include<iostream>
#define int long long
using namespace std;
const int N=2e7+9;
const int inf=1e12;
int n;
struct node{
int l,r;
int key,val;
int cnt,size;
}tr[N];
int root,idx;
void push_up(int p){
tr[p].size=tr[tr[p].l].size+tr[tr[p].r].size+tr[p].cnt;
}
int get_node(int key){
tr[++idx].key=key;
tr[idx].val=rand();
tr[idx].cnt=tr[idx].size=1;
return idx;
}
void zig(int &p){
int q=tr[p].l;
tr[p].l=tr[q].r;
tr[q].r=p;
p=q;
push_up(tr[p].r);
push_up(p);
}
void zag(int &p){
int q=tr[p].r;
tr[p].r=tr[q].l;
tr[q].l=p;
p=q;
push_up(tr[p].l);
push_up(p);
}
void build(){
get_node(-inf),get_node(inf);
root=1;
tr[1].r=2;
push_up(root);
if(tr[1].val<tr[2].val) zag(root);
}
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);
}
push_up(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);
push_up(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 inf;
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[tr[p].l].size-tr[p].cnt);
}
int get_pre(int &p,int key){
if(!p) return -inf;
if(tr[p].key>=key){
return get_pre(tr[p].l,key);
}
return max(tr[p].key,get_pre(tr[p].r,key));
}
int get_next(int &p,int key){
if(!p) return inf;
if(tr[p].key<=key){
return get_next(tr[p].r,key);
}
return min(tr[p].key,get_next(tr[p].l,key));
}
int m;
signed main(){
build();
// freopen("data.in","r",stdin);
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++){
int x;
scanf("%lld",&x);
insert(root,x);
// cout<<n;
}
int last;
last=0;
int ans=0;
while(m--){
int opt,x;
scanf("%lld%lld",&opt,&x);
x^=last;
if(opt==1){
insert(root,x);
}
else if(opt==2){
remove(root,x);
}
else if(opt==3){
int b=get_rank_by_key(root,x-1);
last=b;
ans^=last;
}
else if(opt==4){
int b=get_key_by_rank(root,x+1);
if(b!=inf||b!=-inf){
last=b;
ans^=last;
}
}
else if(opt==5){
int b=(get_pre(root,x));
if(b!=inf||b!=-inf){
last=b;
ans^=last;
}
}
else{
int b=(get_next(root,x));
if(b!=inf||b!=-inf){
last=b;
ans^=last;
}
}
}
cout<<ans;
return 0;
}
by crimson000 @ 2023-04-27 07:56:49
可能每个节点会有重复?
by Llx2022 @ 2023-04-27 08:10:09
对,确实是
by _Adolf_Hitler_ @ 2023-04-27 10:37:38
的确有可能会重复