zhangzhixu000001 @ 2024-06-25 16:31:32
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+1e5+5,randmax=9999,randmin=1;
int cnt,root,lastans;
struct fhq_treap{
int ls,rs;
int data,val;
int size;
}tr[maxn];
void inittreap(int data)
{
cnt++;
tr[cnt].size=1;
tr[cnt].ls=0;tr[cnt].rs=0;
tr[cnt].data=data;
tr[cnt].val=rand()%randmax+randmin;
}
void upsize(int p)
{
tr[p].size=tr[tr[p].ls].size+tr[tr[p].rs].size+1;
}
void split(int u,int x,int &l,int &r)
{
if (!u)
{
l=0;r=0;return;
}
if (tr[u].data<=x)
{
l=u;
split(tr[u].rs,x,tr[u].rs,r);
}
else
{
r=u;
split(tr[u].ls,x,l,tr[u].ls);
}
upsize(u);
}
int merge(int l,int r)
{
if (l==0||r==0) return l+r;
if (tr[l].val>tr[r].val)
{
tr[l].rs=merge(tr[l].rs,r);
upsize(l);
return l;
}
else
{
tr[r].ls=merge(l,tr[r].ls);
upsize(r);
return r;
}
}
void Insert(int x)
{
int l,r;
split(root,x,l,r);
inittreap(x);
int a=merge(l,cnt);
root=merge(a,r);
}
void del(int x)
{
int l,r,p;
split(root,x,l,r);
split(l,x-1,l,p);
p=merge(tr[p].ls,tr[p].rs);
root=merge(merge(l,p),r);
}
void paiming(int x)
{
int l,r;
split(root,x-1,l,r);
lastans=tr[l].size+1;cout<<lastans<<endl;
root=merge(l,r);
}
int kth(int u,int k)
{
if (k==tr[tr[u].ls].size+1) return u;
if (k<=tr[tr[u].ls].size) return kth(tr[u].ls,k);
if (k>tr[tr[u].ls].size) return kth(tr[u].rs,k-tr[tr[u].ls].size-1);
}
void pre(int x)
{
int l,r;
split(root,x-1,l,r);
lastans=tr[kth(l,tr[l].size)].data;
cout<<lastans<<endl;
root=merge(l,r);
}
void suf(int x)
{
int l,r;
split(root,x,l,r);
lastans=tr[kth(r,1)].data;
cout<<lastans<<endl;
root=merge(l,r);
}
int main()
{
ios::sync_with_stdio(0);
srand(time(0));
int opt,x,n,m;cin>>n>>m;
for (int i=1;i<=n;i++) {cin>>x;Insert(x);}
for (int i=1;i<=m;i++)
{
cin>>opt>>x;
x^=lastans;
switch(opt)
{
case 1:Insert(x);break;
case 2:del(x);break;
case 3:paiming(x);break;
case 4:lastans=tr[kth(root,x)].data;cout<<lastans<<endl;break;
case 5:pre(x);break;
case 6:suf(x);break;
}
}
return 0;
}
by zhangzhixu000001 @ 2024-06-25 16:38:06
对不起我是盲人,没看到题目条件,此贴结