懒得复制,戳我戳我

Solution:

  • \(Splay\)板子,注意交换的地方,然后就是注意不要越界node[x],应该是\(node[now]\),其次就是数组可以开大点

Code:

//It is coded by Ning_Mew on 4.10
#include<bits/stdc++.h>
#define ls(x) node[x].ch[0]
#define rs(x) node[x].ch[1]
#define fa(x) node[x].fa
#define root node[0].ch[1]
using namespace std; const int maxn=200000+10,INF=1e9+7,MOD=1e6; int n,ans=0,tot=0;
struct Node {
int fa,ch[2],cnt,val,size;
} node[maxn]; void update(int x) {node[x].size=node[ls(x)].size+node[rs(x)].size+node[x].cnt;}
void connect(int x,int fa,int how) {node[x].fa=fa;node[fa].ch[how]=x;}
int ident(int x) {return x==node[fa(x)].ch[0]?0:1;}
void rorate(int x) {
int Y=fa(x),R=fa(Y); int Yson=ident(x),Rson=ident(Y);
connect(node[x].ch[Yson^1],Y,Yson);
connect(Y,x,Yson^1);
connect(x,R,Rson);
update(Y);update(x);
}
void Splay(int x,int goal) {
int to=fa(goal);
while(fa(x)!=to) {
if(fa(fa(x))==to)rorate(x);
else if(ident(x)==ident(fa(x)))rorate(fa(x)),rorate(x);
else rorate(x),rorate(x);
}
}
int newnode(int x,int fa) {node[++tot].val=x;node[tot].cnt=node[tot].size=1;node[tot].fa=fa;return tot;}
void ins(int x) {
int now=root;
if(!root) {newnode(x,0);root=tot;return;
} else {
while(1) {
node[now].size++;
if(node[now].val==x) {node[now].cnt++;Splay(now,root);return;}
int nxt=x<node[now].val?0:1;
if(!node[now].ch[nxt]) {
int p=newnode(x,now);
node[now].ch[nxt]=p; Splay(p,root);
return;
}
now=node[now].ch[nxt];
}
}
}
int find(int x) {
int now=root;
while(1) {
if(!now)return 0;
if(node[now].val==x) {Splay(now,root);return now;}
int nxt=x<node[now].val?0:1;
now=node[now].ch[nxt];
}
}
void del(int x) {
int pos=find(x);
if(!pos)return;
if(node[pos].cnt>1) {node[pos].cnt--;node[pos].size--;return;}
if(!node[pos].ch[0]&&!node[pos].ch[1]) {root=0;return;}
if(!node[pos].ch[0]) {root=node[pos].ch[1];node[root].fa=0;return;
} else {
int left=ls(pos);///
while(rs(left))left=rs(left);
Splay(left,node[pos].ch[0]);
connect(node[pos].ch[1],left,1);
connect(left,0,1);
update(left);return;
}
}
int lower(int x) {
int now=root,ans=-INF;
while(now) {
if(node[now].val<=x)ans=max(ans,node[now].val);
int nxt=x<=node[now].val?0:1;
now=node[now].ch[nxt];
}
return ans;
}
int upper(int x) {
int now=root,ans=INF;
while(now) {
if(node[now].val>=x)ans=min(ans,node[now].val);
int nxt=x<node[now].val?0:1;
now=node[now].ch[nxt];
}
return ans;
}
int num[5];
int main() {
scanf("%d",&n);
int x,opt,stat=0;
for(int i=1; i<=n; i++) {
scanf("%d%d",&opt,&x);
if(num[stat]==0) {
ins(x);
num[opt]++;
stat=opt;/*cout<<"ins finished"<<endl;*/
} else {
if(opt==stat)ins(x),num[opt]++/*,cout<<"ins finished"<<endl*/;
else {
num[stat]--;
int up=upper(x),low=lower(x),box=INF;
if(up!=INF)box=min(box,abs(x-up));
if(low!=-INF)box=min(box,abs(x-low));
//cout<<up<<' '<<low<<' '<<box<<endl;
ans=(ans+box)%MOD;
if(box==abs(x-low))del(low);
else del(up);
//cout<<"del finished"<<endl;
}
}
//cout<<"--"<<stat<<endl;
}
printf("%d\n",ans);
return 0;
}

【题解】 [HNOI2004]宠物收养场(Splay)的更多相关文章

  1. 题解 [HNOI2004]宠物收养场

    解析 这题似乎是裸的平衡树\(+\)模拟...于是用\(treap\)写了个板子. 看上去,我们似乎要维护两颗树(宠物和顾客), 然而,注意到,同一时间宠物点只有一类人(或物qwq), 所以,只要判断 ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. LG_2286_[HNOI2004]宠物收养场

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

  9. P2286 [HNOI2004]宠物收养场

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

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

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

随机推荐

  1. php删除文件夹和其下的内容

    原文地址:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/05/13/2045158.html <?php function del ...

  2. 写个定时任务更新svn

    最近学了点shell编程,寻思锻炼下写一个.平常你学习或者看别人讲,自己不练习肯定不行,基本上一动手准出错哈哈.等自己去实践,才会知道哪里有问题,哪里容易出错,哪里要注意什么的. 因为我们每个人有自己 ...

  3. 数字IC设计入门书单

    首发于观芯志 写文章     数字IC设计入门书单 Forever snow   1 年前 作者:Forever snow链接:你所在领域的入门书单? - 知乎用户的回答来源:知乎著作权归作者所有,转 ...

  4. 【webstorm】免费使用

    http://idea.imsxm.com/       测试过ok 后期追加(20180316更新为) http://idea.codebeta.cn/ 后期追加(20180502更新为) http ...

  5. iscsi target IET架构

    IET(iSCSI Enterprise Target)是内核态实现的iscsi target,相比于用户态实现的target(比如tgt),iet比较稳定,并且也算是历史悠久,io都直接经过内核态, ...

  6. 20155206 Exp2 后门原理与实践

    20155206 Exp2 后门原理与实践 1.Windows获得Linux Shell 在windows下,打开CMD,使用ipconfig指令查看本机IP 然后使用ncat.exe程序,ncat. ...

  7. CLR回收非托管资源

    一.非托管资源 在<垃圾回收算法之引用计数算法>.<垃圾回收算法之引用跟踪算法>和<垃圾回收算法之引用跟踪算法>这3篇文章中,我们介绍了垃圾回收的一些基本概念和原理 ...

  8. Centos 定时任务发送smtp邮件

    接着上一篇文章...... 1.首先创建一个sheel的脚本命令,我是在home文件夹下面创建的命令: touch a.sh 2.编辑a.sh脚本 vim a.sh ,键入键盘   i  键 准备插入 ...

  9. mfc 嵌套类

    嵌套类 一. 嵌套类 嵌套类的定义 将某个类的定义放在另一个类的内部,这样的类定义,叫嵌套类. class AAA { int aaa; class BBB { int bbb; //其它成员或者函数 ...

  10. [C#]使用Label标签控件模拟窗体标题的移动及窗体颜色不断变换

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以 ...