#include<algorithm>
#include<cctype>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn=2e5,maxlog=128;
int n,opt,x,y,total,ver,root[maxn+1];
long long lastans;
struct node
{
bool reversed;
int value,child[2],priority,size;
long long sum;
node(int t1=0,int t2=0,int t3=0,int t4=0,int t5=0):value(t1),sum(t1),priority(t4),size(t5){child[0]=t2;child[1]=t3;}
}BST[maxn*maxlog+1];
inline void read(int &x)
{
x=0;
int signum=1;
char t=getchar();
while(!isdigit(t))
{
if(t=='-')
signum=-1;
t=getchar();
}
while(isdigit(t))
{
x=x*10+t-'0';
t=getchar();
}
x*=signum;
return;
}
inline void pushup(int now)
{
BST[now].size=BST[BST[now].child[0]].size+BST[BST[now].child[1]].size+1;
BST[now].sum=BST[BST[now].child[0]].sum+BST[now].value+BST[BST[now].child[1]].sum;
return;
}
void pushdown(int now)
{
if(!BST[now].reversed)
return;
for(int i=0;i<2;++i)
if(BST[now].child[i])
{
BST[++total]=BST[BST[now].child[i]];
BST[now].child[i]=total;
BST[total].reversed^=1;
}
swap(BST[now].child[0],BST[now].child[1]);
BST[now].reversed=false;
return;
}
int merge(int x,int y)
{
if(!x||!y)
return x+y;
int now=0;
pushdown(x);
pushdown(y);
if(BST[x].priority<BST[y].priority)
BST[now=x].child[1]=merge(BST[x].child[1],y);
else BST[now=y].child[0]=merge(x,BST[y].child[0]);
pushup(now);
return now;
}
void split(int now,int &x,int &y,int value)
{
if(!now)
{
x=y=0;
return;
}
pushdown(now);
if(BST[BST[now].child[0]].size<value)
{
BST[x=++total]=BST[now];
split(BST[x].child[1],BST[x].child[1],y,value-BST[BST[now].child[0]].size-1);
pushup(x);
}
else
{
BST[y=++total]=BST[now];
split(BST[y].child[0],x,BST[y].child[0],value);
pushup(y);
}
return;
}
void insert(int pos,int target,int value)
{
int x=0,y=0,now=++total;
BST[now]=node(value,0,0,rand(),1);
split(root[pos],x,y,target);
root[pos]=merge(x,merge(now,y));
return;
}
void erase(int pos,int target)
{
int x=0,y=0,z=0;
split(root[pos],y,z,target);
split(y,x,y,target-1);
root[pos]=merge(x,z);
return;
}
void reverse(int pos,int l,int r)
{
int x=0,y=0,z=0;
split(root[pos],y,z,r);
split(y,x,y,l-1);
BST[y].reversed^=1;
root[pos]=merge(x,merge(y,z));
return;
}
long long query(int pos,int l,int r)
{
int x=0,y=0,z=0;
split(root[pos],y,z,r);
split(y,x,y,l-1);
long long res=BST[y].sum;
root[pos]=merge(x,merge(y,z));
return res;
}
signed main()
{
read(n);
for(int i=1;i<=n;++i)
{
read(ver);
read(opt);
read(x);
root[i]=root[ver];
x^=lastans;
if(opt!=2)
{
read(y);
y^=lastans;
}
if(opt==1)
insert(i,x,y);
else if(opt==2)
erase(i,x);
else if(opt==3)
reverse(i,x,y);
else if(opt==4)
printf("%lld\n",lastans=query(i,x,y));
}
return 0;
}
下了两次数据。第一次是第一个点,本地测试无误,交上去挂了;后来把 long long
换成 int
开大空间解决。第二次是第八个点,本地测试依然无误。不知道怎么解决。
求教。