打了一道可持久化平衡树的板子,WA了最后五个点,但下载数据下来测又和答案完全一样???萌新调了很久也不知道这是怎么回事,希望大佬能够指点迷津...qwq
这是记录:
这是本地:(下载数据竟然和样例一模一样)
这是代码:
#include<bits/stdc++.h>
#define N 500005
using namespace std;
int m,tot,n,a[N],rt[N],Root;
inline long long rd()
{
char c;long long sign=1;
while((c=getchar())<'0'||c>'9')
if(c=='-')sign=-1;
long long res=c-'0';
while((c=getchar())>='0'&&c<='9')
res=res*10+c-'0';
return res*sign;
}
struct treap{
int pos[N],size[N],w[N],son[N][2];
bool f[N];
void update(int x)
{
size[x]=size[son[x][0]]+size[son[x][1]]+1;
}
int build(int x)
{
w[++tot]=x,size[tot]=1,pos[tot]=rand();
return tot;
}
int merge(int x,int y)
{
if(!x||!y)return x+y;
if(pos[x]>=pos[y])
{
son[x][1]=merge(son[x][1],y);
update(x);
return x;
}
son[y][0]=merge(x,son[y][0]);
update(y);
return y;
}
void split(int i,int k,int &x,int &y)
{//权值分裂:以i为根 k为目标大小 x,y即分裂出的两棵树
if(!i)
{
x=y=0;return;
}
if(w[i]<=k)
{
x=i;
split(son[x][1],k,son[x][1],y);//x为小于等于k的树
}
else
{
y=i;
split(son[y][0],k,x,son[y][0]);//y为大于k的树
}
update(i);
}
void Delete(int &root,int w)
{
int x=0,y=0,z=0;
split(root,w,x,z);
split(x,w-1,x,y);
y=merge(son[y][0],son[y][1]);
root=merge(merge(x,y),z);
}
void Insert(int &root,int w)
{
int x=0,y=0,z=0;
split(root,w-1,x,y);
z=build(w);
root=merge(merge(x,z),y);
}
int getkth(int &root,int w)
{
int x,y,ans;
split(root,w-1,x,y);
ans=size[x]+1;
root=merge(x,y);
return ans;
}
int getval(int &root,int k)
{
int rt=root;
while(1)
{
if(size[son[rt][0]]+1==k)break;
else if(size[son[rt][0]]+1>k)rt=son[rt][0];
else k-=size[son[rt][0]]+1,rt=son[rt][1];
}
return w[rt];
}
int pre(int &root,int w)
{
int x,y,k,ans;
split(root,w-1,x,y);
if(!x)return -2147483647;
k=size[x];
ans=getval(x,k);
root=merge(x,y);
return ans;
}
int nex(int &root,int w)
{
int x,y,ans;
split(root,w,x,y);
if(!y)return 2147483647;
ans=getval(y,1);
root=merge(x,y);
return ans;
}
}tree;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
int a=rd(),b=rd(),c=rd();
rt[i]=rt[a];
if(b==1)tree.Insert(rt[i],c);
if(b==2)tree.Delete(rt[i],c);
if(b==3)printf("%d\n",tree.getkth(rt[i],c));
if(b==4)printf("%d\n",tree.getval(rt[i],c));
if(b==5)printf("%d\n",tree.pre(rt[i],c));
if(b==6)printf("%d\n",tree.nex(rt[i],c));
}
}
若能指出,实在感谢!