http://codevs.cn/problem/1285/
1285 宠物收养所
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
题解
题目描述 Description
最近,阿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)。
【任务描述】
你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。
输入描述 Input Description
第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)
输出描述 Output Description
仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。
样例输入 Sample Input
5
0 2
0 4
1 3
1 2
1 5
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
主要是学了splay树 用来验板子的
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct value ///节点内数据
{
int flag,key;
};
struct node///ch是左右子树,f是父亲节点,cnt是这个数据出现的次数,size是表示这个树的大小
{
int ch[2],f,cnt,size;
value v;
}t[maxn * 2];
int sz,root;
void init() ///初始化
{
sz = 0;root = 0;
memset(t,0,sizeof(t));
}
void clear(int x) ///清空一个节点
{
t[x].ch[0] = t[x].ch[1] = t[x].f = t[x].size = 0;
t[x].v.flag = t[x].v.key = 0;
}
int get(int x) ///获得这个节点是其父亲节点的哪个子树
{
return t[ t[x].f ].ch[1] == x;
}
void update(int x) ///更新节点大小
{
if(x)
{
t[x].size = t[x].cnt;
if(t[x].ch[0]) t[x].size += t[t[x].ch[0]].size;
if(t[x].ch[1]) t[x].size += t[t[x].ch[1]].size;
}
}
void rotate(int x) ///x节点旋转操作
{
int y = t[x].f; ///y为x的父节点
int k = t[y].f; ///k为y节点的父节点
int son = get(x); ///获得x是左节点 还是右节点
if(k) ///如果爷爷节点存在
{
t[k].ch[ t[k].ch[1] == y ] = x; ///儿子节点变成x 节点
}
t[x].f = k; ///x的父节点变成爷爷节点
t[y].f = x; /// y的父节点变成x
t[y].ch[son] = t[x].ch[son ^ 1]; ///y的 son节点 变成 x 的 son^1 节点
t[t[y]. ch[son]].f = y; /// son^1 节点的父节点变成y节点
t[x].ch[son ^ 1] = y;///x的son^1节点变成y节点
update(y);
update(x);
return ;
}
void splay(int x) ///将x节点旋转至根节点
{
for(int fa = t[x].f; fa ;rotate(x),fa = t[x].f)
{
if(t[fa].f)///如果爷爷节点 父亲节点 和自身 是在一条直线上 就先rotate父亲节点
{
rotate(get(fa) == get(x)?fa:x);
}
}
root = x;
}
void insert(value x) ///将值x 插入树中
{
///根节点不存在时
if(root == 0){sz ++; t[sz].ch[0] = t[sz].ch[1] = t[sz].f = 0; t[sz].cnt = 1; t[sz].v = x ; root = sz; return ;}
int now = root,fa = 0;
while(1)
{
if(t[now].v.key == x.key) ///如果找到值相同的节点
{
t[now].cnt ++;update(now);update(fa);splay(now);break ;
}
fa = now;
now = t[now].ch[ t[now].v.key < x.key]; ///如果插入值小于当前值 就往左子树插 不然就往右子树插
if(now == 0) ///新开辟一个节点
{
sz ++;
t[sz].ch[0] = t[sz].ch[1] = 0;
t[fa].ch[t[fa].v.key < x.key] = sz;
t[sz].f = fa;
t[sz].cnt = t[sz].size = 1;
t[sz].v = x;
update(fa);
splay(sz);
break;
}
}
}
int find(value x)///查找值为 x在树中的排名
{
int now = root,ans = 0;
while(now)
{
if(x.key < t[now].v.key) ///如果当前值大于查找值 就往左子树查
{
now = t[now].ch[0];
}
else
{
if(t[now].ch[0])
{
ans += t[ t[now].ch[0]].size;
}
if(t[now].v.key == x.key) ///如果是是当前节点就SPALY当前节点
{
splay(now);
return ans + 1;
}
ans += t[now].cnt;
now = t[now].ch[1]; ///不然就往右子树继续查找
}
}
return 0;
}
int findx(int x) ///查找排名为x的值
{
int now = root;
while(1)
{
if( t[now].ch[0] && x <= t[t[now].ch[0]].size ) ///如果左子树的个数大于k 就往左子树走
{
now = t[now].ch[0];
}
else
{
int temp = (t[now].ch[0]?t[ t[now].ch[0]].size:0) + t[now].cnt; ///如果左子树+自身节点个数 ≤k 那就是这个节点 不然就往右子树走
if(x <= temp)
{
return t[now].v.key;
}
now = t[now].ch[1];
x -= temp;
}
}
}
int pre() ///查找根节点的前驱节点 (小于x的最大值)
{
int now = t[root].ch[0];
while(t[now].ch[1]) now = t[now].ch[1];
return now;
}
int next() ///查找根节点的后驱节点 (大于x的最小值)
{
int now = t[root].ch[1];
while(t[now].ch[0]) now = t[now].ch[0];
return now;
}
void del(value x) ///删除一个值为x的节点
{
int k = find(x); ///先将那个节点旋转至根节点
if(t[root].cnt > 1){t[root].cnt --;update(root);return ;}//如果右多个相同的节点 却只需要删除一个
if(!t[root].ch[0] && !t[root].ch[1]) { ///如果两个子节点都不存在 就直接删除根节点
clear(root);root = 0;return ;
}
if(!t[root].ch[0]) ///如果只有单个子节点 就让那个子节点当根节点
{
int old = root;
root = t[root].ch[1];
t[root].f = 0;
clear(old);
return ;
}
else if(!t[root].ch[1])
{
int old = root;
root = t[root].ch[0];
t[root].f = 0;
clear(old);
return ;
}
int left = pre(),old = root; ///如果有两个子节点,那就把前驱节点splay到跟节点 然后让原根节点的右子树 接到当前跟节点
splay(left);
t[root].ch[1] = t[old].ch[1];
t[t[old].ch[1] ].f = root;
clear(old);
update(root);
}
void watch(int x) ///前序遍历当前子树
{
if(t[x].ch[0])
watch(t[x].ch[0]);
printf("- %d -\n",t[x].v.key);
if(t[x].ch[1])
watch(t[x].ch[1]);
}
int main()
{
init();
int q;
scanf("%d",&q);
value temp;
long long ans = 0;
while(q --)
{
scanf("%d %d",&temp.flag,&temp.key);
if(root != 0 && t[root].v.flag != temp.flag )
{
temp.flag = t[root].v.flag;
if(find(temp) != 0)
{
del(temp);
continue;
}
insert(temp);
int pr = t[pre()].v.key;
int ne = t[next()].v.key;
del(temp);
if(pr == 0)
pr = ne + 1;
if(ne == 0)
ne = pr;
///printf("pr = %d ne = %d\n",pr,ne);
if( abs(pr - temp.key) <= abs(ne - temp.key))
{
ans += (long long ) abs((pr - temp.key));
temp.key = pr;
del(temp);
}
else
{
ans += (long long)abs((ne - temp.key));
temp.key = ne;
del(temp);
}
///printf("root = %d\n",root);
}
else
{
insert(temp);
}
///watch(root);
}
cout<<ans%1000000<<endl;
return 0;
}