这道题用oi-wiki上面的splay试试发现读到样例第五行的时候也会挂,所以能不能问一问这种splay的写法究竟为什么会挂
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#define ls(x) c[x][0]
#define rs(x) c[x][1]
using namespace std;
const int maxn=500100;
const int modp=1000000;
int f[maxn],sz[maxn],cnt[maxn],c[maxn][2],val[maxn];
int rt,tot;
int col=-1,n;
struct ssplay{
inline void pushup(int x){sz[x]=sz[ls(x)]+sz[rs(x)]+cnt[x];}
inline int pd(int x){return c[f[x]][1]==x;}
inline void clear(int x){ls(x)=rs(x)=cnt[x]=sz[x]=val[x]=f[x]=0;}
inline void rotate(int x)
{
int y=f[x],z=f[y],k=pd(x),m=c[x][!k];
c[x][!k]=y; c[y][k]=m; f[m]=y; f[y]=x; f[x]=z;
if(z) c[z][rs(z)==y]=x;
pushup(y); pushup(x);
}
inline void splay(int x)
{
for(int y=f[x];y=f[x];rotate(x))
{
if(f[y]) rotate(pd(x)^pd(y)?x:y);
}
rt=x;
}
inline void ins(int target)
{
int nowp=rt,fat=0;
if(!rt)
{
cnt[++tot]++;
val[tot]=target;
rt=tot;
pushup(rt);
return ;
}
while(1)
{
if(val[nowp]==target)
{
cnt[nowp]++;
pushup(nowp);
pushup(fat);
splay(nowp);
return ;
}
fat=nowp; nowp=c[nowp][val[nowp]<target];
if(!nowp)
{
val[++tot]=target;
cnt[tot]++;
f[tot]=fat;
c[fat][val[fat]<target]=tot;
pushup(tot);
pushup(fat);
splay(tot);
return ;
}
}
}
inline int rk(int target)
{
int nowp=rt,ans=0;
while(1)
{
if(target==val[nowp]) { ans+=sz[ls(nowp)]; splay(nowp); return ans+1;}
ans+=val[nowp]<target?sz[ls(nowp)]+cnt[nowp]:0;
nowp=c[nowp][val[nowp]<target];
}
}
inline int kth(int k)
{
int nowp=rt;
while(1)
{
if(ls(nowp)&&k<=sz[ls(nowp)]) nowp=ls(nowp);
else
{
k-=sz[ls(nowp)]+cnt[nowp];
if(k<=0)
{
splay(nowp);
return val[nowp];
}
nowp=rs(nowp);
}
}
}
inline int pre()
{
int nowp=ls(rt);
while(rs(nowp)) nowp=rs(nowp);
splay(nowp);
return nowp;
}
inline int nxt()
{
int nowp=rs(rt);
while(ls(nowp)) nowp=ls(nowp);
splay(nowp);
return nowp;
}
inline void del(int target)
{
rk(target);
int looker;
int nowp=rt;
if(cnt[rt]>1)
{
cnt[rt]--;
pushup(rt);
return ;
}
if(!ls(rt)&&!rs(rt))
{
clear(rt);
rt=0;
return ;
}
if(!ls(rt))
{
looker=rt;
rt=rs(rt);
clear(looker);
f[rt]=0;
pushup(rt);
return ;
}
if(!rs(rt))
{
looker=rt;
rt=ls(rt);
clear(looker);
f[rt]=0;
pushup(rt);
return ;
}
looker=rt;
nowp=pre();
splay(nowp);
f[rs(looker)]=nowp;
rs(nowp)=rs(looker);
clear(looker);
pushup(rt);
return ;
}
}spl;
int ans;
int cntt;
int main()
{
ios::sync_with_stdio(false);
register int i,j;
cin>>n;
int cc,x;
spl.ins(0x3f3f3f3f);
spl.ins(-0x3f3f3f3f);
for(i=1;i<=n;i++)
{
cin>>cc>>x;
if(cntt==0) spl.ins(x);
else
{
if(cc==(cntt<0)) spl.ins(x);
else
{
int a,b;
spl.ins(x);
a=spl.pre();
spl.del(x);
spl.ins(x);
b=spl.nxt();
spl.del(x);
if(abs(val[b]-x)<=abs(x-val[a])) ans+=abs(val[b]-x),spl.del(val[b]);
else ans+=abs(val[a]-x),spl.del(val[a]);
}
}
cntt+=cc==0?1:-1;
ans%=modp;
}
cout<<ans%modp<<endl;
}