yinhy09 @ 2022-08-18 19:32:33
// Problem: P6136 【模板】普通平衡树(数据加强版)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P6136
// Memory Limit: 89 MB
// Time Limit: 3000 ms
// Author: Luogu-yinhy09(575320),Codeforces-yinhy09
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll read()
{
char c;
ll x=0;
ll f=1;
c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')f*=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
ll n,m,op,x;
const int maxn=1e5+5;
ll qwq;
const int maxm=5e6+5;
struct Node
{
ll s[2];
ll p;
ll v;
ll cnt;
ll size;
void init(ll x,ll y)
{
p=x,v=y;
cnt=1;size=1;
}
}t[maxn];
ll root;
ll idx=0;
void pushup(ll x)
{
t[x].size=t[t[x].s[0]].size+t[t[x].s[1]].size+t[x].cnt;
}
void rotate(ll x)
{
ll y=t[x].p,z=t[y].p;
ll k;
if(t[y].s[1]==x)k=1;
else k=0;
ll q;
if(t[z].s[1]==y)q=1;
else q=0;
t[z].s[q]=x;
t[x].p=z;
t[y].s[k]=t[x].s[k^1];
t[t[x].s[k^1]].p=y;
t[x].s[k^1]=y;
t[y].p=x;
pushup(y);
pushup(x);
}
void splay(ll x,ll k)
{
while(t[x].p!=k)
{
ll y=t[x].p,z=t[y].p;
if(z!=k)
{
if(((t[y].s[0]==x)^(t[z].s[0]==y))!=0)rotate(x);
else rotate(y);
}
rotate(x);
}
if(k==0)root=x;
}
void find(ll v)
{
ll x=root;
while(t[x].s[(v>t[x].v)]!=0&&v!=t[x].v)
{
x=t[x].s[(v>t[x].v)];
}
splay(x,0);
}
ll get_pre(ll v)
{
find(v);
ll x=root;
if(t[x].v<v)return x;
x=t[x].s[0];
while(t[x].s[1]!=0)x=t[x].s[1];
return x;
}
ll get_suc(ll v)
{
find(v);
ll x=root;
if(t[x].v>v)return x;
x=t[x].s[1];
while(t[x].s[0]!=0)x=t[x].s[0];
return x;
}
void del(ll v)
{
ll pre=get_pre(v);
ll suc=get_suc(v);
splay(pre,0),splay(suc,pre);
ll del=t[suc].s[0];
if(t[del].cnt>1)
{
t[del].cnt--;
splay(del,0);
}
else
{
t[suc].s[0]=0;
splay(suc,0);
}
}
ll get_rank(ll v)
{
find(v);
return t[t[root].s[0]].size;
}
ll get_val(ll k)
{
ll x=root;
while(1)
{
ll y=t[x].s[0];
if(t[y].size+t[x].cnt<k)
{
k-=(t[y].size+t[x].cnt);
x=t[x].s[1];
}
else
{
if(t[y].size>=k)x=y;
else break;
}
}
splay(x,0);
return t[x].v;
}
void insert(ll v)
{
ll x=root,p=0;
while(x!=0&&t[x].v!=v)
{
p=x;
x=t[x].s[(v>t[x].v)];
}
if(x!=0)t[x].cnt++;
else
{
x=++idx;
t[p].s[(v>t[p].v)]=x;
t[x].init(p,v);
}
splay(x,0);
}
int main()
{
// freopen("P6136_1.in","r",stdin);
// freopen("1.txt","w",stdout);
ll ans=0,l=0;
n=read(),m=read();
for(int i=1;i<=n;i++)qwq=read(),insert(qwq);
insert(-9e18),insert(9e18);
while(m--)
{
op=read(),x=read();
x^=l;
cout<<x<<endl;
if(op==1)insert(x);
else if(op==2)del(x);
else if(op==3)insert(x),l=get_rank(x),del(x);
else if(op==4)l=get_val(x+1);
else if(op==5)insert(x),l=t[get_pre(x)].v,del(x);
else if(op==6)insert(x),l=t[get_suc(x)].v,del(x);
if(op>2)ans^=l;
}
printf("%lld\n",ans);
return 0;
}
用普通平衡树板子改的,普通的板子 AC 了,改完就 TLE+RE,大佬们帮忙看一下,thanks
by EastSnowLotus @ 2022-08-18 19:56:31
您爆杀平衡树/bx
by EastSnowLotus @ 2022-08-18 19:57:51
但是为什么不用 FHQ-Treap 捏
by Demeanor_Roy @ 2022-08-19 15:57:48
数组开小了啊
by Demeanor_Roy @ 2022-08-19 15:59:55
@yinhy09 还有插入操作,t数组肯定不够
by yinhy09 @ 2022-08-19 16:10:00
@Demeanor_Roy 把maxm打成maxn了,智障了
by yinhy09 @ 2022-08-19 16:11:16
@Demeanor_Roy 过了,谢谢
帖结