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 ...
随机推荐
- python 可变参数函数定义* args和**kwargs的用法
python函数可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法.其中,*args是可变的positional arguments列表,**kwargs是 ...
- Sitemesh3的使用心得
项目中用到了sitemesh3,就把使用心得记下来,至于配置之类的,官方网站都有,这里只是写下自己对它的理解,方便再次理解, sitemesh是基于过滤器的原理,拦截到符合配置文件中配置的路径,然后会 ...
- Window系统下MongoDB安装及远程访问
1.编辑mongodb 安装文件夹bin\mongod.cfg 把bindIP 改为 127.0.0.1, 192.168.1.180(局域网IP) 可以参考https://blog.csdn.net ...
- 低秩近似 low-rank approximation
- php记录百度等搜索引擎蜘蛛的来访记录
<?php function is_robot() { $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); if (strpos($use ...
- 去掉标题栏/ActionBar后点击menu键时应用崩溃
MainActivity 继承了 ActionBarActivity后,想要去掉标题栏(ActionBar),在程序中加上requestWindowFeature(Window.FEATURE_NO_ ...
- Django模型系统——ORM
一.概论 1.ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描 ...
- [原创]Scala学习:编写Scala脚本
scala支持脚本 1)在/opt/scala-script下创建一个文件hello.scala 编辑内容如下: $ hello ,this is the first scala script 2)运 ...
- Hadoop集群初始化启动
hadoop集群初始化启动 启动zookeeper ./zkServer.sh start 启动journalnode ./hadoop-daemon.sh start journalnode 格式化 ...
- 关于ansible变量的一个问题
ansible-playbook 使用with_items 时 items中 如果有变量 {} 外面可以用 “” items中 如果都是固定值,没有用到变量,{}最外面不要加 “” ,不然报错,mmp