BZOJ_1208_[HNOI2004]宠物收养所_SPLAY

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

第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

Output

仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

Sample Input

5
0 2
0 4
1 3
1 2
1 5

Sample Output

3
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)

分析:

我们记录一下当前的店里是人还是宠物,对于每个匹配操作,我们从根find下去,记录一下最接近它的点,然后删除就行。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 100050
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
int ch[N][2], val[N], siz[N], f[N], sz, cnt[N], tot, n, ans, mod = 1000000, rt;
void clr(int p) {
f[p] = ls = rs = siz[p] = val[p] = cnt[p] = 0;
}
void pushup(int p) {
if(!p) return ;
siz[p] = cnt[p];
if(ls) siz[p] += siz[ls];
if(rs) siz[p] += siz[rs];
}
void rotate(int x) {
int y = f[x], z = f[y], k = get(x);
ch[y][k] = ch[x][!k];
f[ch[y][k]] = y;
ch[x][!k] = y;
f[y] = x;
f[x] = z;
if(z) ch[z][ch[z][1] == y] = x;
pushup(y); pushup(x);
if(rt == y) rt = x;
}
void splay(int x,int y) {
for(int fa;(fa = f[x]) != y;rotate(x))
if(f[fa] != y)
rotate(get(x) == get(fa) ? fa : x);
}
void insert(int x) {
int p = rt, fa = 0;
if(!rt) { p = ++ tot; cnt[p] = siz[p] = 1; f[p] = ls = rs = 0; val[p] = x; rt = p; return ;}
while(1) {
if(val[p] == x) {
cnt[p] ++ ; pushup(p); pushup(fa); splay(p, 0); return ;
}
fa = p;
p = ch[p][val[p] < x];
if(!p) {
p = ++ tot;
ls = rs = 0;
val[p] = x;
cnt[p] = siz[p] = 1;
f[p] = fa;
ch[fa][val[fa] < x] = p;
pushup(p); pushup(fa); splay(p, 0); return ;
}
}
}
int pre() {
int p = ch[rt][0];
while(rs) p = rs;
return p;
}
void del(int p) {
splay(p, 0);
if(cnt[p] > 1) {
cnt[p] -- ;
pushup(p);
return ;
}
if(!ls && !rs) {
clr(rt);
rt = 0;
return ;
}
if(!ls) {
int tmp = rt; rt = rs; f[rs] = 0; clr(tmp);
return ;
}
if(!rs) {
int tmp = rt; rt = ls; f[ls] = 0; clr(tmp);
return ;
}
int pr = pre();
int tmp = rt;
splay(pr, 0);
rt = pr;
ch[pr][1] = ch[tmp][1];
f[ch[tmp][1]] = pr;
clr(tmp); pushup(rt);
}
int Abs(int x) {return x>0?x:-x;}
void solve(int x) {
int p = rt, re = rt, de = Abs(x - val[rt]), flg = x < val[rt] ? 0 : 1;
while(1) {
if(x == val[p]) {
del(p);
return ;
}
int tmp = ch[p][val[p] < x];
if(tmp) {
p = tmp;
int now = Abs(val[p] - x);
if(now < de || (now == de && val[p] < x && !flg)) {
re = p;
de = now;
flg = x < val[p] ? 0 : 1;
}
}
else {
ans = (ans + de) % mod;
del(re);
return ;
}
}
}
void print(int p) {
if(!p) return ;
print(ls);
printf("p = %d, val[p] = %d, siz[p] = %d, cnt[p] = %d\n",p,val[p],siz[p],cnt[p]);
print(rs);
}
int main() {
scanf("%d", &n);
int i, x, y, now = 0;
for(i = 1;i <= n; ++ i) {
scanf("%d%d", &x, &y);
if(!x) x--;
if(now == 0) {
now += x;
insert(y);
// print(rt);
}else if(now > 0) {
if(x > 0) {
insert(y);
now ++;
}else {
solve(y);
// printf("ans = %d\n",ans);
now --;
}
// print(rt);
}else {
if(x < 0) {
insert(y);
now --;
}else {
solve(y);
// printf("ans = %d\n",ans);
now ++;
}
// print(rt);
}
}
printf("%d\n",ans);
}

BZOJ_1208_[HNOI2004]宠物收养所_SPLAY的更多相关文章

  1. BZOJ 1208: [HNOI2004]宠物收养所

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7514  Solved: 2982[Submit][Sta ...

  2. bzoj 1208: [HNOI2004]宠物收养所 set

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7328  Solved: 2892[Submit][Sta ...

  3. 数据结构(set):COGS 62. [HNOI2004] 宠物收养所

    62. [HNOI2004] 宠物收养所 ★★★   输入文件:pet.in   输出文件:pet.out   简单对比时间限制:1 s   内存限制:128 MB 最近,阿Q开了一间宠物收养所.收养 ...

  4. bzoj1208 [HNOI2004]宠物收养所(STL,Treap)

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5956  Solved: 2317[Submit][Sta ...

  5. BZOJ 1208: [HNOI2004]宠物收养所(BST)

    本来想先用set写一遍,再自己写个splay或treap,不过用set过了之后就懒得去写了....以后有空再来写吧..(不会有空的吧= = ------------------------------ ...

  6. bzoj 1208: [HNOI2004]宠物收养所 (Treap)

    链接:  https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题面: 1208: [HNOI2004]宠物收养所 Time Limit: 10 ...

  7. 2018.07.06 BZOJ1208: HNOI2004宠物收养所(非旋treap)

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...

  8. BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4902  Solved: 1879 题目连接 http:/ ...

  9. cogs62 [HNOI2004] 宠物收养所

    cogs62 [HNOI2004] 宠物收养所 啦啦啦啦 不维护区间的平衡树题都是树状数组+二分练手题! 不会的参考我的普通平衡树的多种神奇解法之BIT+二分答案 // It is made by X ...

随机推荐

  1. Space Golf~物理题目

    Description You surely have never heard of this new planet surface exploration scheme, as it is bein ...

  2. (译) JSON-RPC 2.0 规范(中文版)

    http://wiki.geekdream.com/Specification/json-rpc_2.0.html 起源时间: 2010-03-26(基于2009-05-24版本) 更新: 2013- ...

  3. [ Java面试题 ]基础篇之一

    1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 2.Java有 ...

  4. oracle数据库的备份与还原(本地及远程操作)

    数据的导出 exp qh/qh@qh  file='d:\backup\qh\qh20060526.dmp'  grants=y  full=n  1 将数据库TEST完全导出,用户名system 密 ...

  5. 下载Github上某个项目的子文件夹和单个文件

    preface Github下的项目可能很大,里面有很多的子文件夹,我们可能只需要使用某个子目录下的资源,可以不用下载完整的repo就能使用. 例如,我想下载这个repo中的字典文件:https:// ...

  6. Linux 命令行输入

    这几天刚刚接触到Linux,在windows上安装的VMWare虚拟机,Centos7.安装什么都是贾爷和办公室的同事帮忙搞定的. 在虚拟机界面,按快捷键Ctrl+Alt+Enter,可以全屏显示Li ...

  7. gradle 将依赖打入Jar包的方法

    使用的是IDEA,直接引入 plugins { id 'com.github.johnrengelman.shadow' version '1.2.3' } 放在build.gradle的最上面,然后 ...

  8. c++11线程池

    #pragma once #include <future> #include <vector> #include <atomic> #include <qu ...

  9. python都能做什么

    一.python: Python具有丰富和强大的库.它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起.常见的一种应用情形是,使用Python快速生成程序的原型 ...

  10. 提高Maven下载jar包的速度

    1.提高Maven下载jar包的速度 打开项目所配置的maven包下conf目录下的settings.xml 找到  <mirrors>标签添加一下内容: 1 <!-- 阿里云仓库 ...