我当时脑子一抽,用map查一个数值的编号,多一个log,貌似不影响AC。
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
map<int,int> bh;
int fa[80005],ch[80005][2];
int sz[80005],key[80005];
int t1,t2;
int inf=214748364,root=0,tot=0,k=1000000;
void up(int x){sz[0]=0;sz[x]=sz[ch[x][1]]+sz[ch[x][0]]+1;}
bool dir(int x){return x==ch[fa[x]][1];}
void rotate(int x){
int y=fa[x];int z=fa[y];
bool b=dir(x);int a=ch[x][!b];
if(z==0) root=x;
else ch[z][dir(y)]=x;
fa[x]=z;fa[y]=x;
ch[x][!b]=y;ch[y][b]=a;
if(a) fa[a]=y;
up(y);up(x);
if(z) up(z);
}
void splay(int x,int i){
bool b,c;int y,z;
while(fa[x]!=i){
y=fa[x];z=fa[y];
if(z==i)
rotate(x);
else{
b=dir(x);c=dir(y);
if(b^c){rotate(x);rotate(x);}
else {rotate(y);rotate(x);}
}
}
if(!i) root=x;
}
void find_pre(int x,int y){
if(x==0)
return ;
if(key[x]<y){
t1=key[x];
find_pre(ch[x][1],y);
}
else
find_pre(ch[x][0],y);
}
void find_nxt(int x,int y){
if(x==0)
return ;
if(key[x]>y){
t2=key[x];
find_nxt(ch[x][0],y);
}
else
find_nxt(ch[x][1],y);
}
void insert(int x,int y,int last){
if(x==0){
x=++tot;
key[x]=y;bh[y]=x;sz[x]=1;
fa[x]=last;ch[x][1]=0;
ch[x][0]=0;
if(y<key[last])
ch[last][0]=x;
else
ch[last][1]=x;
splay(x,0);
return ;
}
if(key[x]>y)
insert(ch[x][0],y,x);
else
insert(ch[x][1],y,x);
}
void del(int y){
int x=bh[y];
splay(x,0);
sz[0]=0;
if(sz[ch[x][0]]==0){
root=ch[x][1];
fa[ch[x][1]]=0;
return ;
}
if(sz[ch[x][1]]==0){
root=ch[x][0];
fa[ch[x][0]]=0;
return ;
}
t1=-inf;
find_pre(root,y);
int q=bh[t1];
splay(q,root);
root=q;
fa[q]=0;
ch[q][1]=ch[x][1];
fa[ch[x][1]]=q;
}
int main()
{
int n;scanf("%d",&n);
int x,y,ans=0;
int h;
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
if(sz[root]==0){
insert(root,y,0);
h=x;
}
else{
if(x==h)
insert(root,y,0);
else{
t1=-inf;t2=inf;
find_pre(root,y+1);
find_nxt(root,y-1);
if(t2-y<y-t1){
ans+=t2-y;
ans%=k;
del(t2);
}
else{
ans+=y-t1;
ans%=k;
del(t1);
}
}
}
}
printf("%d",ans);
return 0;
}