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 ...
随机推荐
- List和Set排序的实现
List.Set.Map的区别 List和Set继承了Collection接口. List以特定索引来存取元素,可以有重复元素.Set不能存放重复元素(用对象的equals()方法来区分元素是否重复) ...
- hdu3579(线性同余方程组)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Lorenzo Von Matterhorn(STL_map的应用)
Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...
- ResfulApi规范
序号 方法 描述 1 GET 请求指定的页面信息,并返回实体主体. 2 HEAD 类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头 3 POST 向指定资源提交数据进行处理请求(例如提 ...
- appium报'Command 'D\:\\android-sdk-windows\\platform-tools\\adb.exe -P 5037 -s “adb device” shell pm clear appPackage' exited with code 1'
解决方法:是因为手机开发者模式没有允许USB调试(安全模式),打开即可
- Spring 拦截器——HandlerInterceptor
采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...
- OpenCV编程->RGB直方图统计
我们在处理彩色图像时.特别是在做局部图像的阈值切割时,须要一个直观的RGB统计图. 接下来開始实现. 代码: void CalcHistRGB() { IplImage* img_sou ...
- linux 5-sort,uniq,tar,split
十二. 行的排序命令sort: 1. sort命令行选项: 选项 描述 -t 字段之间的分隔符 -f 基于字符排序时忽略大小写 -k 定义排序的域字段,或者是基于域字段的部分数据进行排序 - ...
- WebApp页面开发小结
一 背景 公司需要开发一个web页面,需要支持主流android和ios手机,采用web页面好处是一个页面,在不同平台之间都可以用,节省成本,基本html.js和css大家也都熟悉.但是对 ...
- Android app与PC端交互
app提交信息到PC端mysql数据库 新建名为SignActivity package com.example.administrator.success; import android.app.A ...