最近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。 1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。 2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。 一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。
第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)
仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。
5
0 2
0 4
1 3
1 2
1 5
3
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
Splay
两棵splay,找一下前驱后继即可。
要注意一下没有前驱后继的情况。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int SZ = 1000010;
const int INF = 1000000010;
const int mod = 1000000;
struct node{
node *ch[2],*f;
int sz,cnt,v;
void maintain()
{
sz = ch[0] -> sz + ch[1] -> sz + cnt;
}
int dir()
{
return f -> ch[1] == this;
}
int cmp(int x)
{
if(x == v) return -1;
return x < v ? 0 : 1;
}
void setc(node *p,int d)
{
(ch[d] = p) -> f = this;
}
}T[SZ], *root[2], *null;
int Tcnt = 0;
node* newnode(int x,node *f)
{
node *k = T + (Tcnt ++);
k -> v = x;
k -> ch[0] = k -> ch[1] = null;
k -> f = f;
k -> sz = k -> cnt = 1;
return k;
}
void rotate(node *p,int dd)
{
node *fa = p -> f;
int d = p -> dir();
fa -> f -> setc(p,fa -> dir());
fa -> setc(p -> ch[d ^ 1],d); fa -> maintain();
p -> setc(fa,d ^ 1); p -> maintain();
if(fa == root[dd]) root[dd] = p;
}
void splay(node *p,int dd,node *rt = null)
{
while(p -> f != rt)
{
if(p -> f -> f == rt) rotate(p,dd);
else
{
if(p -> dir() == p -> f -> dir()) rotate(p -> f,dd),rotate(p,dd);
else rotate(p,dd),rotate(p,dd);
}
}
}
void insert(node *p,int x,int dd)
{
if(root[dd] == null) { root[dd] = newnode(x,null); return ; }
while(p != null)
{
p -> sz ++;
int d = p -> cmp(x);
if(d == -1) { p -> cnt ++; break; }
if(p -> ch[d] == null)
{
p -> ch[d] = newnode(x,p);
p = p -> ch[d];
break;
}
p = p -> ch[d];
}
splay(p,dd);
}
void erase(node *p,int x,int dd)
{
while(p != null)
{
p -> sz --;
int d = p -> cmp(x);
if(d == -1) {p -> cnt --; break; }
p = p -> ch[d];
}
if(p -> cnt) return ;
splay(p,dd);
if(p -> ch[0] == null) { root[dd] = p -> ch[1]; root[dd] -> f = null; return; }
if(p -> ch[1] == null) { root[dd] = p -> ch[0]; root[dd] -> f = null; return; }
p = p -> ch[0];
while(p -> ch[1] != null) p = p -> ch[1];
splay(p,dd,root[dd]);
p -> ch[1] = root[dd] -> ch[1]; p -> ch[1] -> f = p;
p -> f = null; p -> maintain();
root[dd] = p;
}
int ask_pre(node *p,int x)
{
int ans = INF;
while(p != null)
{
if(p -> v < x)
ans = p -> v,p = p -> ch[1];
else
p = p -> ch[0];
}
return ans;
}
int ask_suf(node *p,int x)
{
int ans = INF;
while(p != null)
{
if(p -> v > x)
ans = p -> v,p = p -> ch[0];
else
p = p -> ch[1];
}
return ans;
}
void init()
{
null = newnode(-INF,null);
null -> cnt = null -> sz = 0;
root[1] = root[0] = null;
}
int main()
{
init();
int m;
scanf("%d",&m);
int ans = 0;
while(m --)
{
int d,x;
scanf("%d%d",&d,&x);
if(!root[d ^ 1] -> sz)
insert(root[d],x,d);
else
{
int pre = ask_pre(root[d ^ 1],x);
int suf = ask_suf(root[d ^ 1],x);
if(abs(pre - x) <= abs(suf - x))
{
erase(root[d ^ 1],pre,d ^ 1);
ans = (ans % mod + abs(pre - x) % mod) % mod;
}
else
{
erase(root[d ^ 1],suf,d ^ 1);
ans = (ans % mod + abs(suf - x) % mod) % mod;
}
}
}
printf("%d",ans);
return 0;
}