「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」宠物收养场的更多相关文章

  1. [HNOI2004]宠物收养场 Treap前驱后继

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

  2. P2286 [HNOI2004]宠物收养场

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

  3. 洛谷P2286 [HNOI2004]宠物收养场【Treap】题解+AC代码

    题目传送门啦~啦~啦~ 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的 ...

  4. 洛谷P2286 [HNOI2004]宠物收养场

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

  5. BZOJ1208[HNOI2004]宠物收养场——treap

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

  6. 洛谷 P2286 [HNOI2004]宠物收养场

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

  7. [HNOI2004]宠物收养场 BZOJ1208 splay tree

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

  8. LG_2286_[HNOI2004]宠物收养场

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

  9. 宠物收养场 Treap

    宠物收养场 时间限制: 1 Sec  内存限制: 128 MB 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠 ...

随机推荐

  1. BZOJ-1563-郁闷的出纳员(权值线段树)

    偏移量要考虑清楚. #include <bits/stdc++.h> using namespace std; const int N=4e5+10; const int BASE=1e5 ...

  2. Shiro入门学习之shi.ini实现授权(三)

    一.Shiro授权 前提:需要认证通过才会有授权一说 1.授权过程 2.相关方法说明 ①subject.hasRole("role1"):判断是否有该角色 ②subject.has ...

  3. next_permutation的使用-Hdu1027

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  4. centos 6.5 防火墙通过 80 和 3306 端口

    vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPU ...

  5. mybatis用mybatis-generator-core-1.3.5.jar自动生成实体类

    原文出处:https://blog.csdn.net/shuoshuo_12345/article/details/80626241,本文只是个人总结而已! 方法1:在pom文件中添加依赖 只需在搭建 ...

  6. byte的取值范围

    byte b = Byte.MAX_VALUE;        b+=1;        System.out.println(b); //输出为-128 取值范围为[-128 -  127] 解析: ...

  7. httpClient简单封装

    package com.Interface.util; import java.util.Iterator; import java.util.Map; import org.apache.commo ...

  8. SqlCacheDependency 缓存数据库依赖

    启用SQL SERVER 通知 aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d N ...

  9. Tensorflow机器学习入门——ModuleNotFoundError: No module named 'tensorflow.keras'

    这个bug的解决办法: # from tensorflow.keras import datasets, layers, models from tensorflow.python.keras imp ...

  10. Web--Utils

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...