# 洛谷P2286 [HNOI2004]宠物收养场

## 输入输出样例

5
0 2
0 4
1 3
1 2
1 5


3



splay裸题

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int MAXN=1e6+10;
const int mod=1000000;
const int INF=2*0x7ffffff;
inline char nc()
{
static char buf[MAXN],*p1=buf,*p2=buf;
}
{
char c=nc();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}
while(c>='0'&&c<='9'){x=x*10+c-'0',c=nc();}
return x*f;
}
int PetNum;
#define root tree[0].ch[1]
struct node
{
int v,fa,ch[2],rec;
}tree[MAXN];

int tot,point;
bool ident(int x)
{
return tree[tree[x].fa].ch[0]==x?0:1;
}
void connect(int x,int fa,int how)
{
tree[x].fa=fa;
tree[fa].ch[how]=x;
}
void rotate(int x)
{
int Y=tree[x].fa;
int R=tree[Y].fa;
int Yson=ident(x);
int Rson=ident(Y);
int B=tree[x].ch[Yson^1];
connect(B,Y,Yson);
connect(Y,x,Yson^1);
connect(x,R,Rson);
}
void splay(int x,int to)
{
to=tree[to].fa;
while(tree[x].fa!=to)
{
if(tree[tree[x].fa].fa==to) rotate(x);
else if(ident(x)==ident(tree[x].fa)) rotate(tree[x].fa),rotate(x);
else rotate(x),rotate(x);
}
}
void newpoint(int x,int fa)
{
tree[++tot].v=x;
tree[tot].fa=fa;
tree[tot].rec=1;
}
void insert(int x)
{
point++;
if(tot==0){newpoint(x,0);root=tot;return ;}
int now=root;
while(1)
{
if(tree[now].v==x)
{
tree[now].rec++;
splay(now,root);
return ;
}
int nxt=x<tree[now].v?0:1;
if(!tree[now].ch[nxt])
{
newpoint(x,now);
tree[now].ch[nxt]=tot;
splay(tot,root);
return ;
}
now=tree[now].ch[nxt];
}
}
int lower(int x)
{
int ans=-INF;
int now=root;
while(now)
{
if(tree[now].v<x)   ans=max(ans,tree[now].v);
int nxt=x<tree[now].v?0:1;
if(tree[now].ch[nxt]==0)    return ans;
now=tree[now].ch[nxt];
}
return ans;
}
int upper(int x)
{
int ans=INF;
int now=root;
while(now)
{
if(tree[now].v>x)   ans=min(ans,tree[now].v);
int nxt=x<tree[now].v?0:1;
if(tree[now].ch[nxt]==0)    return ans;
now=tree[now].ch[nxt];
}
}
int find(int x)
{
int now=root;
while(1)
{
if(tree[now].v==x)  {splay(now,root);return now;}
int nxt=x<tree[now].v?0:1;
if(!tree[now].ch[nxt])    return 0;
now=tree[now].ch[nxt];
}
}
void des(int x)
{
tree[x].ch[0]=tree[x].ch[1]=tree[x].fa=tree[x].rec=tree[x].v=0;
if(x==tot) tot--;
}
void dele(int x)
{
int deal=find(x);
if(!deal)   return ;
point--;
if(tree[deal].rec>1){tree[deal].rec--;return ;}
if(!tree[deal].ch[0]) root=tree[deal].ch[1],tree[root].fa=0;
else
{
int mxson=tree[deal].ch[0];
while(tree[mxson].ch[1])    mxson=tree[mxson].ch[1];
splay(mxson,tree[deal].ch[0]);
connect(tree[deal].ch[1],mxson,1);
connect(mxson,0,1);
}
des(deal);
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
while(n--)
{
if(PetNum==0)
{
insert(x);
if(opt==0)    PetNum++;
else PetNum--;
}
else if(PetNum>0)
{
if(opt==0)  insert(x),PetNum++;
else
{
int pre=lower(x);
int lat=upper(x);
if(abs(pre-x)<=abs(x-lat))    dele(pre),ans=(ans+abs(pre-x))%mod;
else    dele(lat),ans=(ans+abs(lat-x))%mod;
PetNum--;
}
}
else
{
if(opt==1)  insert(x),PetNum--;
else
{
int pre=lower(x);
int lat=upper(x);
if((abs(pre-x))<abs(x-lat))    dele(pre),ans=(ans+abs(pre-x))%mod;
//这里写小于等于和小于一样
else    dele(lat),ans=(ans+abs(lat-x))%mod;
PetNum++;
}
}
}
printf("%d",ans);
}

posted @ 2017-11-25 17:07  自为风月马前卒  阅读(296)  评论(0编辑  收藏

……