HNOI2004宠物收养所(splay维护二叉搜索树模板题)
描述
输入格式
输出格式
测试样例
输入
5
0 4
1 3
1 2
1 5
输出
3
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define mod 1000000
#define M 80010
using namespace std;
LL read(){
LL nm=,oe=;char cw=getchar();
while(!isdigit(cw)) oe=cw=='-'?-oe:oe,cw=getchar();
while(isdigit(cw)) nm=nm*+(cw-''),cw=getchar();
return nm*oe;
}
LL n,m,ans,T,tpe,t,w;
struct binary_search_tree{
LL ace,l[M],r[M],p[M],cnt,tp[M],sz;
void init(){l[]=r[]=cnt=ace=sz=,p[]=tp[];}
void rotate(LL x){
if(ace==x) return;
LL top=tp[x];
if(ace==top) tp[ace]=x,ace=x;
else{
if(l[tp[top]]==top) l[tp[top]]=x;
else r[tp[top]]=x;
tp[x]=tp[top],tp[top]=x;
}
if(l[top]==x) l[top]=r[x],tp[r[x]]=top,r[x]=top;
else r[top]=l[x],tp[l[x]]=top,l[x]=top;
}
void splay(LL x){
if(x==) return;
while(ace!=x){
LL k=tp[x];
if(k==ace) rotate(x);
else if(l[l[tp[k]]]==x||r[r[tp[k]]]==x) rotate(k);
else rotate(x);
}
return;
}
void del(LL x){
sz--;
if(x==){return;}
splay(x);
if(l[x]==&&r[x]==){init();return;}
if(l[x]==) ace=r[x];
else if(r[x]==) ace=l[x];
else{
LL k=l[x];
if(r[k]==){l[ace]=l[k],tp[l[k]]=ace,p[ace]=p[k];return;}
while(r[k]!=) k=r[k];
r[tp[k]]=l[k],tp[l[k]]=tp[k],p[ace]=p[k];
}
}
void insert(LL x){
sz++;
if(ace==){p[++cnt]=x,ace=cnt,l[cnt]=r[cnt]=;return;}
LL pos=ace,big=;
while(pos!=){
if(p[pos]<=x){
if(r[pos]==){r[pos]=++cnt,p[cnt]=x,tp[cnt]=pos;break;}
pos=r[pos];
}
else{
if(l[pos]==){l[pos]=++cnt,p[cnt]=x,tp[cnt]=pos;break;}
pos=l[pos];
}
big++;
}
l[cnt]=r[cnt]=;
if(big>=) splay(cnt);
}
LL find(LL x){
LL k=ace,len=99999999999999ll,num=ace;
while(true){
if(k==) break;
if(p[k]==x) return k;
if(p[k]>x){
if(len>p[k]-x) len=p[k]-x,num=k;
k=l[k];
}
else{
if(len>=x-p[k]) len=x-p[k],num=k;
k=r[k];
}
}
return num;
}
}pet,hum;
LL ab(LL x){return x>?x:-x;}
int main(){
pet.init(),hum.init();
T=read();
while(T--){
tpe=read(),t=read();
if(tpe==){
if(hum.ace==) pet.insert(t);
else{
w=hum.find(t);
ans+=ab(t-hum.p[w]);
hum.del(w);
}
}
else{
if(pet.ace==) hum.insert(t);
else{
w=pet.find(t);
ans+=ab(t-pet.p[w]);
pet.del(w);
}
}
}
printf("%lld\n",ans%mod);
return ;
}
HNOI2004宠物收养所(splay维护二叉搜索树模板题)的更多相关文章
- hdoj-3791-二叉搜索树(二叉搜索树模板题)
#include <cstring> #include <cstdio> #include <iostream> using namespace std; type ...
- Bzoj 1208: [HNOI2004]宠物收养所(splay)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...
- [bzoj1208][HNOI2004]宠物收养所——splay
题目大意 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发 ...
- 【BZOJ1208】[HNOI2004]宠物收养所 Splay
还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...
- BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题
题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...
- poj 3253 初涉二叉堆 模板题
这道题很久以前就做过了 当时是百度学习了优先队列 后来发现其实还有个用sort的办法 就是默认sort排序后 a[i]+=a[i-1] 然后sort(a+i,a+i+n) (大概可以这样...答案忘了 ...
- BZOJ1208 [HNOI2004]宠物收养所 splay
原文链接http://www.cnblogs.com/zhouzhendong/p/8085803.html 题目传送门 - BZOJ1208 题意概括 有两种数,依次加入. 规则为下: 如果当前剩余 ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
随机推荐
- ios开发:如何加载大量图片 相册示例
本文转载至 http://www.cnblogs.com/xiongqiangcs/archive/2013/06/13/3134486.html 1. Create a NSOperationQ ...
- TP的分页加查询
1.查询显示数据库的内容 控制器里的内容 public function shouye() { $n = M("car"); $arr = $n->select(); $th ...
- Educational Codeforces Round 1 (C) (atan2 + long double | 大数)
这题只能呵呵了. 东搞西搞,折腾快一天,最后用了一个800多行的代码AC了. 好好的题目你卡这种精度干啥. 还有要卡您就多卡点行不,为什么long double 又可以过... 废了N长时间写个了不管 ...
- Angular中的$cacheFactory的作用和用法
1.Angular中的$cacheFactory的作用: (1)put(key,value); 在缓存对象中插入一个键值对(key,value). (2)get(key); 在缓存对象中通过指定 ...
- rm -rf 删除文件找回
一个不小心rm掉文件了吧? 后悔莫及了吧! 把这段代码加入你的home目录的.bashrc或者.zshrc就可以了! 工作原理: 在你的home目录会创建一个.trash文件夹 里面会按照删除时间 年 ...
- CSS图片居中,多余隐藏
/*外层DIV*/ div {position: relative;overflow:hidden;width: 显示宽度px;} /*left=50%刚好在中间,margin-left=往前移动图片 ...
- Python 面试题(下)
接上篇. 网络 1 三次握手 客户端通过向服务器端发送一个SYN来创建一个主动打开,作为三路握手的一部分.客户端把这段连接的序号设定为随机数 A. 服务器端应当为一个合法的SYN回送一个SYN/ACK ...
- 如何在官网下载java JDK的历史版本
如何在官网下载java JDK的历史版本 http://jingyan.baidu.com/article/9989c746064d46f648ecfe9a.html 如何在官网下载java JDK的 ...
- 1django 视图与网址
创建一个项目,名字叫mysite django-admin startproject mysite(项目名) 成功后,看到如下样式 mysite ├── manage.py └── mysite ├─ ...
- iOS base64编码 MD5 加密
//创建一个Base64编码的NSString对象 //字符串 转二进制 NSData *nsdata = [@"iOS Developer Tips encoded in Base64&q ...