懒得复制,戳我戳我

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. action类型的按钮和object按钮的用法

    <div class="oe_right oe_button_box" name="buttons"> <button class=" ...

  2. BZOJ2427: [HAOI2010]软件安装 tarjan+树形背包

    分析: 一开始我以为是裸的树形背包...之后被告知这东西...可能有环...什么!有环! 有环就搞掉就就可以了...tarjan缩点...建图记得建立从i到d[i]之后跑tarjan,因为这样才能判断 ...

  3. English_word_learning

    这次报名参加了学院的21天打卡活动,说实话,也是想给自己一个积累的平台. 毕竟,真的有时候感觉挺弱的 有的人用了一年考完了四六级,而有人却用四年还未考完. 听到有一位学长因为自己的四级成绩没有达到48 ...

  4. 使用redis

    通过 Nuget获取包StackExchange.Redis 写数据: ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(&quo ...

  5. 20155234 昝昕明 《网络对抗技术》实验一 PC平台逆向破解

    实践内容: 手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数. 利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发getShell函数. 注入一个自己制作的sh ...

  6. Hiberante持久化对象的3种状态

        近日一同事问我关于Hibernate中对象的3种状态的问题,因此特意总结一下.在Hibernate中对象是有以下3中状态: 瞬时态(transient object): 没有OID值,没有被s ...

  7. WPF 简易进度条效果

    最近做一个项目,看到以前同事写的进度条效果不错,所以,拿来简化了下,不炫,但是项目中还是够用的. 还是,先来看下调用以后的效果 1.因为ProgressbBar的Foreground显示不得不一样,所 ...

  8. 洛咕 P4491 [HAOI2018]染色

    显然颜色数量不会超过\(lim=\min(m,n/S)\) 考虑容斥,计算恰好出现了\(S\)次的颜色有至少\(i\)种的方案数\(f[i]\),钦定\(i\)种颜色正好放\(S\)种 有\(m\)种 ...

  9. Neo4j 第四篇:使用C#更新和查询Neo4j

    本文使用的IDE是Visual Studio 2015 ,驱动程序是Neo4j官方的最新版本:Neo4j Driver 1.3.0 ,创建的类库工程(Project)要求安装 .NET Framewo ...

  10. ElasticSearch查询 第一篇:搜索API

    <ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...