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 ...
随机推荐
- elasticsearch从入门到出门-08-Elasticsearch容错机制:master选举,replica容错,数据恢复
假如: 9 shard,3 node Elasticsearch容错机制:master选举,replica容错,数据恢复 最佳分配情况: 这样分配之后,不管其中哪个node 宕机这个es 依然可以提供 ...
- 6.2.3-AbstractBeanFactory
AbstractBeanFactory体系: 这个抽象类中很重要的实现了BeanFactory中得一个方法,doGetBean(); @SuppressWarnings("unchecked ...
- Visual studio 创建文件时自动添加备注
Visual studio 创建文件时自动添加备注 描述 要求每回添加一个类,普通类或单元测试类文件头自动添加备注, 比如:Copyright, FileName, Author and so on. ...
- eclipse svn 分支合并到主干
最近公司产品上线,整个系统架构包含有七八个子系统,并且子系统都是集群部署.所以每次升级维护都要确保尽可能不出问题.因为整个系统刚上线不久,意味着新系统不定期有BUG需修复,但新功能模块也在持续的开发中 ...
- RLearning第2弹:创建数据集
任何一门语言,数据类型和数据结构是最基础,也是最重要的,必须要学好!1.产生向量 a<-c(1,2,5,3,6,-2,4) b<-c("one","two&q ...
- Python赋值原理:Python无变量,万物皆对象
有几个和以前的常见语言,比如c语言不同 改变变量数据不覆盖原来的 name = '苍老师' print(id(name)) name = '志玲' print(id(name)) 运行结果 73955 ...
- cmd - - - 随笔
dxdiag DirectX诊断工具 calc 计算机 cmd里面不想一点点输入冗长的文件路径?直接把这个文件拖到CMD窗口吧!你会发现 路径自己补上去了.有空格的还会自动加引号哟.
- jquery跟DOM转换
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- UI组件之Label
Use Core Data 接口,链接数据库 Portrait 肖像模式 LandScape(Left, Right) 风景模式 1.程序启动后,从main接口进入, main函数会调用UIAppli ...
- ubuntu14.04搭建gitlab
以下内容来自:https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/ (清华大学开源软件镜像站)可以直接移步上面的网站.这里做个笔记,也是为了记录一下 ...