sansesantongshun @ 2024-02-18 22:24:07
#include<bits/stdc++.h>
using namespace std;
int n,m,k,x,lst=0,ans=0;
struct node;
typedef node* BST;
BST tree,tmp;
struct node
{
int num,cnt,sz;
BST son[2],fa;
node(int x): num{x},cnt{},sz{},son{},fa{}{}
void pushup()
{
sz=cnt;
if (son[0])
sz+=son[0]->sz;
if (son[1])
sz+=son[1]->sz;
}
bool get()
{
return fa && this==fa->son[1];
}
void rotate()
{
BST f=fa;
bool k1=get(),k2=fa->get();
fa=f->fa;
if (fa)
fa->son[k2]=this;
f->son[k1]=son[!k1];
if (f->son[k1])
f->son[k1]->fa=f;
son[!k1]=f;
f->fa=this;
f->pushup();
pushup();
}
void splay(BST x)
{
while (fa!=x)
{
if (fa->fa!=x)
{
if (fa->get()==get())
fa->rotate();
else
rotate();
}
rotate();
}
if (!x)
tree=this;
}
BST query(int x)
{
int siz=son[0]?son[0]->sz:0;
if (x<=siz)
return son[0]->query(x);
else if (x<=siz+cnt)
return this;
else
return son[1]->query(x-siz-cnt);
}
};
void insert(int x)
{
BST t=tree;
if (!t)
t=new node(x);
while (x!=t->num)
{
if (!t->son[x>t->num])
{
t->son[x>t->num]=new node(x);
t->son[x>t->num]->fa=t;
}
t=t->son[x>t->num];
}
++t->cnt;
++t->sz;
t->pushup();
t->splay(nullptr);
}
BST find(BST t,int x)
{
if (!t || x==t->num)
return t;
else
return find(t->son[x>t->num],x);
}
void erase(int x)
{
BST t=find(tree,x);
t->splay(nullptr);
if (t->cnt>1)
{
--t->cnt;
--t->sz;
}
else if (t->sz==1)
{
tree=nullptr;
delete t;
}
else if (!t->son[0])
{
tree=t->son[1];
tree->fa=nullptr;
delete t;
}
else if (!t->son[1])
{
tree=t->son[0];
tree->fa=nullptr;
delete t;
}
else
{
BST rt=t->son[1];
while (rt->son[0])
rt=rt->son[0];
rt->splay(t);
(rt->son[0]=t->son[0])->fa=rt;
tree=rt;
tree->fa=nullptr;
delete t;
}
}
int lub(BST t,int x,bool k)
{
if (t)
{
int siz=t->son[0]?t->son[0]->sz:0;
if (x==t->num)
return siz+k*t->cnt;
else if (x<t->num)
return lub(t->son[0],x,k);
else
return siz+t->cnt+lub(t->son[1],x,k);
}
else
return 0;
}
int main()
{
cin>>n>>m;
while (n--)
{
scanf("%d",&x);
insert(x);
}
while (m--)
{
scanf("%d%d",&k,&x);
x^=lst;
if (k==1)
insert(x);
else if (k==2)
erase(x);
else if (k==3)
{
lst=lub(tree,x,0)+1;
ans^=lst;
tmp=find(tree,x);
if (tmp)
tmp->splay(nullptr);
}
else if (k==4)
{
lst=tree->query(x)->num;
ans^=lst;
}
else if (k==5)
{
lst=tree->query(lub(tree,x,0))->num;
ans^=lst;
tmp=find(tree,x);
if (tmp)
tmp->splay(nullptr);
}
else
{
lst=tree->query(lub(tree,x,1)+1)->num;
ans^=lst;
tmp=find(tree,x);
if (tmp)
tmp->splay(nullptr);
}
}
cout<<ans;
}
by sansesantongshun @ 2024-07-12 17:04:34
用 FHQ Treap