「HNOI2004」宠物收养场
「HNOI2004」宠物收养场
传送门
对宠物和人各维护一棵平衡树,每次 \(\text{split}\) 的时候记得判一下子树是否为空,然后模拟就好了。
参考代码:
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T & s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 8e4 + 5, mod = 1e6;
int n, rt1, rt2, tot, siz[_], val[_], pri[_], ch[2][_];
inline int Newnode(int v)
{ return siz[++tot] = 1, val[tot] = v, pri[tot] = rand(), tot; }
inline void pushup(int p) { siz[p] = siz[ch[0][p]] + siz[ch[1][p]] + 1; }
inline int merge(int x, int y) {
if (!x || !y) return x + y;
if (pri[x] > pri[y])
return ch[1][x] = merge(ch[1][x], y), pushup(x), x;
else
return ch[0][y] = merge(x, ch[0][y]), pushup(y), y;
}
inline void split(int p, int v, int& x, int& y) {
if (!p) { x = y = 0; return ; }
if (val[p] <= v)
return x = p, split(ch[1][p], v, ch[1][x], y), pushup(p);
else
return y = p, split(ch[0][p], v, x, ch[0][y]), pushup(p);
}
inline int kth(int p, int k) {
if (siz[ch[0][p]] + 1 > k) return kth(ch[0][p], k);
if (siz[ch[0][p]] + 1 == k) return val[p];
if (siz[ch[0][p]] + 1 < k) return kth(ch[1][p], k - siz[ch[0][p]] - 1);
}
inline void insert(int& rt, int item) {
int a, b;
split(rt, item, a, b);
rt = merge(a, merge(Newnode(item), b));
}
inline void erase(int& rt, int item) {
int a, b, c;
split(rt, item, a, c);
split(a, item - 1, a, b);
b = merge(ch[0][b], ch[1][b]);
rt = merge(a, merge(b, c));
}
inline int match(int& rt, int item) {
int a, b, x, y, res;
split(rt, item, a, b);
if (a == 0 && b != 0) res = kth(b, 1);
if (a != 0 && b == 0) res = kth(a, siz[a]);
if (a != 0 && b != 0)
x = kth(a, siz[a]), y = kth(b, 1), res = item - x <= y - item ? x : y;
rt = merge(a, b);
return res;
}
int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(n);
int ans = 0;
for (rg int a, b, x, i = 1; i <= n; ++i) {
read(a), read(b);
if (a == 0) {
if (rt2 == 0) insert(rt1, b);
else x = match(rt2, b), erase(rt2, x), ans = (ans + abs(x - b)) % mod;
} else {
if (rt1 == 0) insert(rt2, b);
else x = match(rt1, b), erase(rt1, x), ans = (ans + abs(x - b)) % mod;
}
}
printf("%d\n", ans);
return 0;
}
「HNOI2004」宠物收养场的更多相关文章
- [HNOI2004]宠物收养场 Treap前驱后继
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- 洛谷P2286 [HNOI2004]宠物收养场【Treap】题解+AC代码
题目传送门啦~啦~啦~ 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的 ...
- 洛谷P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- BZOJ1208[HNOI2004]宠物收养场——treap
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- 洛谷 P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- [HNOI2004]宠物收养场 BZOJ1208 splay tree
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- LG_2286_[HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- 宠物收养场 Treap
宠物收养场 时间限制: 1 Sec 内存限制: 128 MB 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠 ...
随机推荐
- Cosmetic Airless Bottles To Meet Practical Requirements
Today, people use cosmetic bottles, many of which are in cosmetic airless bottles. We can use them, ...
- linux配置放火墙开放端口
vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火 ...
- 攻防世界 robots题
来自攻防世界 robots [原理] robots.txt是搜索引擎中访问网站的时候要查看的第一个文件.当一个搜索蜘蛛访问一个站点时,它会首先检查该站点根目录下是否存在robots.txt,如果存在, ...
- canvas的描述
// 1.找到DOM节点 const canvas = document.getElementById('canvas'); // 2.画笔 --- canvas的上下文对象 const ctx = ...
- java中路径的问题
在java中,涉及路径的问题有很多,不管在windows还是linux系统中,不要纠结”/“分隔符的使用,在windows系统中,资源加载器会自动的将”/“转换成”\“. 在java中获取资源的方式有 ...
- mac系统,docker下载安装
1. docker安装文档 2. Docker下安装SQL Server
- 消息队列(五)--- RocketMQ-消息存储2
概述 RocketMQ存储中主要用到以下知识点: mmap 文件映射 内存池 异步刷盘 consumeQueue 同时本节将介绍各个重要的类,本篇文章将介绍 mmap 文件映射的相关方法和内存池相关知 ...
- TM1638控制
原理图图纸: 显示控制与读按键与寄存器的对应 驱动代码:码云:
- jquery Ajax标准规范写法
$.ajax({ url:"http://www.xxx",//请求的url地址 dataType:"json",//返回的格式为json async:true ...
- IDEA 设置 自动编译
转载自:https://www.cnblogs.com/eyesfree/p/9321795.html 设置 File ->Setting ->Compile: 勾选"Make ...