题目链接

思路

比较裸的一道平衡树的题。用一个变量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. HTML5经典案例学习-----新元素添加文档结构

    直接上代码了,大家如果发现问题了,记得提醒我哦,谢谢啦,嘻嘻 <!DOCTYPE html> <!-- 不区分大小写 --> <html lang="en&qu ...

  2. JMeter 连接 sql server

    1.安装驱动 http://www.microsoft.com/zh-CN/download/details.aspx?id=11774 下载后解压后复制sqljdbc.jar到 “jmeter的安装 ...

  3. delphi 怎么实现主窗口退出时,有一个提示框?

    无论点窗口上的[按钮]还是[右上角的叉],能出现一个提示窗口,“是”-退出窗口,“否”-重新登录(调出登录窗口),“取消”-返回.MessageBox能实现吗?还是要调用新窗口(我调用窗口,有些错误) ...

  4. vue 思維導圖

    vue概念:vue是一個輕量級的javascript庫:是一種漸進式的框架:vue可以實現數據視圖雙向綁定. vue基礎語法:實例化.條件.循環 vue重頭戲:動畫.組件.過濾.ajax.自定義組件. ...

  5. DFI LP DK P45 T2RS PLUS BIOS SETTING

    standard cmos features date (mm:dd:yy) mon,oct 11 2016 time (hh:mm:ss) 10 : 10 : 26 ide channel 0 sa ...

  6. Error:Failed to resolve: com.android.support:appcompat-v7

    repositories { maven { url "https://maven.google.com" } } implementation 'com.android.supp ...

  7. How to blog on Github

    git clone https://github.com/test/test.github.io.git cd ~/test.github.io git config --global push.de ...

  8. Stack Pointer Tracker

    在Intel 64与IA-32架构中,存在一类用于跳转到以及跳出程序段的指令:PUSH.POP.CALL.LEAVE与RET.这些指令可以在没有其余指令的干预下隐式地更新栈寄存器(ESP),维护栈内的 ...

  9. 测试md

    一级标题 table class="top_ta" width="100%" border="0" cellspacing="0& ...

  10. raise missingsectionheadererror:file containe no section headers问题解决

    本人亲测,遇到这个问题,就换到管理员方式运行命令 因为太小白,所以这次重新装包的时候切换到D盘了,想着省一点儿C盘内存,结果,每次pip install安装的时候都是这个问题,中间还有什么反序列失败, ...