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 ...
随机推荐
- PHP fsockopen模拟POST/GET方法
原文链接:http://www.nowamagic.net/academy/detail/12220214 fsockopen 除了前面小节的模拟生成 HTTP 连接之外,还能实现很多功能,比如模拟 ...
- Angular关于$anchorScroll的定位滚动
以下是实现定位滚动的代码: <!DOCTYPE html> <html lang="en" ng-app="app"> <head ...
- POJ 2187 Beauty Contest【凸包周长】
题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- zoj 3356 Football Gambling II【枚举+精度问题】
题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3356 http://acm.hust.edu.cn/vjudge/ ...
- rpc接口和http接口的区别和联系
1 什么是http接口 http接口是基于http协议的post和get接口. 2 什么是rpc接口 rpc接口就相当于调用本地接口一样调用远程服务的接口. 3 常用的rpc框架 thrift 自动代 ...
- 我的Android进阶之旅------>FastJson的简介
在最近的工作中,在客户端和服务器通信中,需要采用JSON的方式进行数据传输.简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON字符串,出错率就非常大了.并且工作效率也 ...
- oracle 查询重复数据并且删除, 只保留一条数据重复数据
最近面试中都遇到了这样一个数据库题: 删除表中的重复数据,有且只保留一条重复数据. 思路: 1)这个题需要用到rowid,首先找到重复数据的rowid,并找出rowid最大或最小值,作为删除的条件: ...
- python 局部local和全局global变量
global和local变量 虽然简单,但是还是记录一下,主要是转载 转载自:http://blog.sina.com.cn/s/blog_436992740102ux8z.html 先看一段代码 ...
- 在vi或vim上查找字符串
从开头搜索 在命令模式下,输入/你要查找的字符 按下回车,可以看到vim把光标移动到该字符处 再按n(小写)查看下一个匹配 按N(大写)查看上一个匹配, capslock切换大小写,也可以在小写状态下 ...
- DNS 原理入门 (转)
DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...