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 ...
随机推荐
- 开启貌似已经过时很久的新坑:SharePoint服务器端对象模型
5年前(嗯,是5年前),SharePoint 2010刚发布的时候,曾经和kaneboy试图一起写一本关于SharePoint 2010开发的书,名字叫<SharePoint 2010 应用开发 ...
- Entity Framework 4.1:复杂类型
这篇文章将讨论复杂类型. 默认情况下,EF4.1 将类映射到表,这是约定,但是有时候,我们需要模型比表的粒度更细一些. 地址是一个典型的例子,看一下下面的客户类. )] publicstring St ...
- 九度OJ 1343:城际公路网 (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:445 解决:178 题目描述: 为了加快城市之间的通行和物资流动速度,A国政府决定在其境内的N个大中型城市之间,增加修建K条公路.已知这N个 ...
- 九度OJ 1204:农夫、羊、菜和狼的故事 (遍历、BFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:744 解决:502 题目描述: 有一个农夫带一只羊.一筐菜和一只狼过河. 果没有农夫看管,则狼要吃羊,羊要吃菜. 但是船很小,只够农夫带一样 ...
- 小程序发起post请求回调成功没有数据
get请求时为 header:{ "content-type":'application/json' },POST 请求会将data的值放在Request Payload里面,而不 ...
- data standardization
import random import numpy as np l, num, gen_min_, gen_max_ = [], 100, 1, 200 l = [random.randint(ge ...
- phpstorm+xdebug, 实现断点调试: xdebug如何配置
[XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" xdebug.trace_output_dir=" ...
- 【Android】开发优化之——调优工具:dump hprof file 查看内存情况,找到内存泄露
虽说知道一般性的开发android应用须要注意的问题,但是也有水平參差不齐的情况.特别是维护代码,假设内存占用大,内存溢出严重,又怎么解决呢? -- 通过DDMS把heap抓出来分析 1.打开DD ...
- SAP后台作业记录操作
[转http://blog.163.com/liang_ce_521@126/blog/static/709202152013073376596/]后台作业信息存储在透明表TBTCP(批作业步骤概述) ...
- eclipse安装Activiti Designer插件(转载:http://blog.csdn.net/qq_33547950/article/details/54926435)
为了完成毕业设计,需要学习Activiti.万事开头难,果然刚开始就遇到了问题.<Activiti实战>和视频教程里提供的安装Activiti Designer插件方法(即下文方法一)不能 ...