传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1208

以后在空间限制允许的情况下我绝对不纠结内存占用问题啦!就因为不舍得用long long而用unsigned,爆掉了我好几个小时。

#include <cstdio>

const int maxn = 80005, delta[2] = {-1, 1}, mod = 1000000;

int n, t1, t2, s, ans;
int ch[maxn][2], fa[maxn], root, cnt = 2;
long long key[maxn], smaller, larger; inline void rotate(int x) {
int y = fa[x];
if (y == ch[fa[y]][0]) {
ch[fa[y]][0] = x;
}
else {
ch[fa[y]][1] = x;
}
fa[x] = fa[y];
int dir = x == ch[y][1];
ch[y][dir] = ch[x][dir ^ 1];
fa[ch[x][dir ^ 1]] = y;
ch[x][dir ^ 1] = y;
fa[y] = x;
}
inline void splay(int x) {
int p;
while (fa[x]) {
p = fa[x];
if (!fa[p]) {
rotate(x);
}
else {
if ((p == ch[fa[p]][1]) ^ (x == ch[p][1])) {
rotate(x);
}
else {
rotate(p);
}
rotate(x);
}
}
root = x;
}
inline void ist(int x, long long val) {
int p = 0, dir = 0;
while (x) {
p = x;
dir = val > key[x];
x = ch[x][dir];
}
++cnt;
ch[p][dir] = cnt;
fa[cnt] = p;
key[cnt] = val;
splay(cnt);
}
inline void del(long long val) {
int x = root;
while (key[x] != val) {
if (val < key[x]) {
x = ch[x][0];
}
else {
x = ch[x][1];
}
}
splay(x);
//注意:此题splay完这个x后,x绝对有两个孩子。
fa[ch[x][0]] = fa[ch[x][1]] = 0;
int i;
for (i = ch[x][0]; ch[i][1]; i = ch[i][1]);
splay(i);
ch[i][1] = ch[x][1];
fa[ch[x][1]] = i;
}
inline long long dayudengyu(long long val) {
int x = root;
long long rt = 0;
while (x && key[x] != val) {
if (val < key[x]) {
rt = key[x];
x = ch[x][0];
}
else {
x = ch[x][1];
}
}
return x? val: rt;
}
inline long long xiaoyu(long long val) {
int x = root;
long long rt = 0;
while (x) {
if (val <= key[x]) {
x = ch[x][0];
}
else {
rt = key[x];
x = ch[x][1];
}
}
return rt;
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d", &n);
key[1] = -999999999999999LL;
ch[1][1] = 2;
key[2] = 999999999999999LL;
fa[2] = 1;
root = 1;
while (n--) {
scanf("%d%d", &t1, &t2);
if (!s) {
ist(root, t2);
}
else {
if (t1 ^ (s < 0)) {
ist(root, t2);
}
else {
larger = dayudengyu(t2);
smaller = xiaoyu(t2);
if (larger - (long long)t2 < (long long)t2 - smaller) {
ans = (ans + ((larger - (long long)t2) % mod)) % mod;
del(larger);
}
else {
ans = (ans + (((long long)t2 - smaller) % mod)) % mod;
del(smaller);
}
}
}
s += delta[t1];
}
printf("%d\n", ans);
return 0;
}

  

_bzoj1208 [HNOI2004]宠物收养所【Splay】的更多相关文章

  1. Bzoj 1208: [HNOI2004]宠物收养所(splay)

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

  2. 【BZOJ1208】[HNOI2004]宠物收养所 Splay

    还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...

  3. [bzoj1208][HNOI2004]宠物收养所——splay

    题目大意 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发 ...

  4. HNOI2004宠物收养所(splay维护二叉搜索树模板题)

    描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

  5. BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题

    题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...

  6. BZOJ1208 [HNOI2004]宠物收养所 splay

    原文链接http://www.cnblogs.com/zhouzhendong/p/8085803.html 题目传送门 - BZOJ1208 题意概括 有两种数,依次加入. 规则为下: 如果当前剩余 ...

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

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

  8. 【BZOJ-1208】宠物收养所 Splay

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

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

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

随机推荐

  1. 学习swift从青铜到王者之Swift语言函数05

    1.定义一个函数以及调用 //一,定义一个无参无返回值函数 func fun1(){ print("this is first function") } fun1() 2.定义一个 ...

  2. C/C++实现bmp文件读写

    之前知道点bmp图的格式,然后对8位操作过,然后今天弄了一下24位真彩色的. C++读取.旋转和保存bmp图像文件编程实现 主要是理解bmp文件的格式8/24位的区别 8位图有调色板,24位在文件头和 ...

  3. DRBD+Heratbeat+NFS高可用文件共享存储

    一.概述 .通过ha-log日志可以看出主释放资源,备接管资源. 来自为知笔记(Wiz)

  4. Centos5设置静态IP地址

    1.设置静态IP地址,修改/etc/sysconfig/network-scripts/ifcfg-eth0的内容: DEVICE=eth0 #网卡对应的设备别名 BOOTPROTO=static # ...

  5. 解决cell切割线不是全屏问题

    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) { [_tableView setSeparatorInset:U ...

  6. 广东IP段列表

    广东IP段列表219.137.240.0 219.137.240.255219.137.148.0 219.137.150.255 广东省广州市 电信ADSL219.137.144.0 219.137 ...

  7. 分享:APK高级保护方法解析(三)

    刷朋友圈.玩游戏.看新闻,智能手机正在以我们无法想象的速度飞快发展,可是随之而来的安全问题也越来越引人关注,APP二次打包.反编译.盗版的现象屡见不鲜.因此须要对APK进行加固保护. 眼下市面上常见的 ...

  8. ==和equals的差别

    == 和 Equals 的差别 1. == 是一个运算符. 2.Equals则是string对象的方法.能够.(点)出来. 我们比較无非就是这两种 1.基本数据类型比較 2.引用对象比較 1.基本数据 ...

  9. makefile redefinition or previous definition

    operation.h:4: error: redefinition of 'class operation' operation.h:5: error: previous definition of ...

  10. 2016/4/2 json:js和jquery中轻量级数据交换格式 例: 窗口弹出 popwindow

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...