题目链接

思路

比较裸的一道平衡树的题。用一个变量S来表示当前树的情况,当S为负数时树内为宠物,当S为正数时树内为人。然后每次分情况讨论一下。如果树为空或者是与来的东西(人或宠物)与树内存的相同。那么就无法领养,直接将这个东西扔到树里。否则就从树里面找一个与当前值最接近的数字,然后统计进答案。

一开始把INF设的太小了影响了统计答案。

代码

#include<cstdlib>
#include<ctime>
#include<cstdio>
#include<iostream>
#define ls TR[cur].ch[0]
#define rs TR[cur].ch[1]
using namespace std;
typedef long long ll;
const int N = 100000,mod = 1000000;
const ll INF = 1e17 + 10;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
struct node {
int ch[2],id;
ll val;
}TR[N];
void rotate(int &cur,int f) {
int son = TR[cur].ch[f];
TR[cur].ch[f] = TR[son].ch[f ^ 1];
TR[son].ch[f ^ 1] = cur;
cur = son;
}
int tot;
void insert(int &cur,int val) {
if(!cur) {
cur = ++tot;
TR[cur].val = val;
TR[cur].id = rand();
return;
}
int d = val > TR[cur].val;
insert(TR[cur].ch[d],val);
if(TR[TR[cur].ch[d]].id < TR[cur].id) rotate(cur,d);
}
void del(int &cur,int val) {
if(!cur) return;
if(val == TR[cur].val) {
if(!ls || !rs) {cur = ls + rs;return;}
int d = TR[ls].id > TR[rs].val;
rotate(cur,d);
del(cur,val);
}
del(TR[cur].ch[val > TR[cur].val],val);
}
ll pred(int cur,int val) {
if(!cur) return -INF;
if(val <= TR[cur].val) return pred(ls,val);
return max(TR[cur].val,pred(rs,val));
}
ll nex(int cur,int val) {
if(!cur) return INF;
if(val >= TR[cur].val) return nex(rs,val);
return min(TR[cur].val,nex(ls,val));
}
int S;
ll ans;
int main() {
srand(time(0));
int n = read(),rt = 0;
while(n--) {
int bz = read(),x = read();
if(bz == 0) {
if(S <= 0) insert(rt,x);
else {
int k1 = pred(rt,x),k2 = nex(rt,x);
int dele = k1;
if(k2 - x < x - k1) dele = k2;
ans += abs(dele - x);
ans %= mod;
del(rt,dele);
}
S--;
}
if(bz == 1) {
if(S >= 0) insert(rt,x);
else {
int k1 = pred(rt,x),k2 = nex(rt,x);
int dele = k1;
if(k2 - x < x - k1) dele = k2;
ans += abs(dele - x);
ans %= mod;
del(rt,dele);
}
S++;
}
}
cout<<ans<<endl;
}

一言

无论做什么,记得为自己而做,那就毫无怨言。 ——流金岁月

[luogu2286][宠物收养所]的更多相关文章

  1. Bzoj1208 [HNOI2004]宠物收养所

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7457  Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...

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

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

  3. 宠物收养所(bzoj1208)

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

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

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

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

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

  6. BZOJ1208 宠物收养所

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

  7. C++之路进阶——codevs1285(宠物收养所)

    1285 宠物收养所  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服 ...

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

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

  9. BZOJ_1208_&_Codevs_1258_[HNOI2004]_宠物收养所_(平衡树/set)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1208 (据说codevs要更新?就不放codevs的地址了吧...) 有宠物和人,每个单位都有 ...

随机推荐

  1. python爬虫之初始Selenium

    1.初始 Selenium[1]  是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Moz ...

  2. 老男孩python学习自修第十二天【常用模块之生成随机数】

    常用函数 import random random.random() 生成0到1之间的小数 random.randint(begin, end) 生成[begin, end]之间的整数 random. ...

  3. idea 通过命令操作git

    关于如何把git(远程)端项目拉取到idea端的操作可以观看:https://blog.csdn.net/autfish/article/details/52513465 在本地向远程提交文件git ...

  4. 启用chacha20和salsa20等加密方法

    wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz tar zxf LATEST.tar.gz cd libsod ...

  5. 搭建Hexo博客(二)-连接github

    没有github账号先需要创建账号,地址:https://github.com/join?source=header 有账号的看下面: 1.创建repo 创建一个repo,名称为yourname.gi ...

  6. 简单聊聊Linux学习经历

    学习,是我们一生中都规避不了的一个话题,人的一生中都是在不断的学习,无论是功成名就的人士,还是一无是处的小混混,始终都处在一个不断学习的环境中,只是学习的内容千差万别,有的人是为了提升自己各方面的能力 ...

  7. mysql严格模式的开启、关闭

    关于mysql严格模式的开启.关闭 由于项目中对一些默认值设置问题,以及种种原因,mysql数据库需要使用非严格模式开发(mysql最近的版本默认是开启严格模式的). linux下mysql服务下操作 ...

  8. Spring 使用介绍(十一)—— Spring事件

    一.简介 spring事件是观察者设计模式的实现,主要有三个元素: 事件 spring事件由ApplicationEvent定义 监听者 由ApplicationListener定义 发布者 由App ...

  9. BZOJ3196二逼平衡树——线段树套平衡树(treap)

    此为平衡树系列最后一道:二逼平衡树您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询 ...

  10. android打电话方法(直接拨通)

    新建了CallPhone方法,如下: private void CallPhone() { String number = et_number.getText().toString(); if (Te ...