【BZOJ1208】宠物收养所(平衡树,splay)
题意:见题面
思路:因为每个时刻要么全是人要么全是宠物,所以可以一棵splay解决
维护单点插入,单点删除,求前驱,求后继即可
var t:array[..,..]of longint;
num,fa:array[..]of longint;
n,cnt,t1,t2,i,root,p,x,y:longint;
ans:int64; procedure rotate(x:longint;var k:longint);
var y,z,l,r:longint;
begin
y:=fa[x]; z:=fa[y];
if t[y,]=x then l:=
else l:=;
r:=-l;
if y=k then k:=x
else
if t[z,]=y then t[z,]:=x
else t[z,]:=x;
fa[x]:=z; fa[y]:=x; fa[t[x,r]]:=y;
t[y,l]:=t[x,r]; t[x,r]:=y;
end; procedure splay(x:longint;var k:longint);
var y,z:longint;
begin
while x<>k do
begin
y:=fa[x]; z:=fa[y];
if y<>k then
begin
if (t[y,]=x)xor(t[z,]=y) then rotate(x,k)
else rotate(y,k);
end;
rotate(x,k);
end;
end; procedure ins(var k:longint;x,last:longint);
begin
if k= then
begin
inc(cnt);
k:=cnt; num[k]:=x;
fa[k]:=last; splay(k,root);
exit;
end;
if x<num[k] then ins(t[k,],x,k)
else ins(t[k,],x,k);
end; procedure del(x:longint);
var k:longint;
begin
splay(x,root);
if t[x,]*t[x,]= then root:=t[x,]+t[x,]
else
begin
k:=t[x,];
while t[k,]> do k:=t[k,];
t[k,]:=t[x,]; fa[t[x,]]:=k;
root:=t[x,];
end;
fa[root]:=;
end; procedure pred(k,x:longint);
begin
if k= then exit;
if num[k]<=x then
begin
t1:=k; pred(t[k,],x);
end
else pred(t[k,],x);
end; procedure succ(k,x:longint);
begin
if k= then exit;
if num[k]>=x then
begin
t2:=k; succ(t[k,],x);
end
else succ(t[k,],x);
end; begin
assign(input,'bzoj1208.in'); reset(input);
assign(output,'bzoj1208.out'); rewrite(output);
readln(n);
for i:= to n do
begin
readln(x,y);
if root= then begin p:=x; ins(root,y,); end
else if p=x then ins(root,y,)
else
begin
t1:=-; t2:=-;
pred(root,y); succ(root,y);
if t1=- then
begin
ans:=ans+num[t2]-y; ans:=ans mod ; del(t2);
end
else if t2=- then
begin
ans:=ans+y-num[t1]; ans:=ans mod ; del(t1);
end
else
if y-num[t1]>num[t2]-y then
begin
ans:=ans+num[t2]-y; ans:=ans mod ; del(t2);
end
else
begin
ans:=ans+y-num[t1]; ans:=ans mod ; del(t1);
end;
end;
end;
writeln(ans);
close(input);
close(output);
end.
【BZOJ1208】宠物收养所(平衡树,splay)的更多相关文章
- [BZOJ1208]宠物收养所(Splay)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- [bzoj1208][HNOI2004][宠物收养所] (平衡树)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- BZOJ1208 宠物收养所
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- 【HNOI2004】宠物收养所(splay)
题面 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的 ...
- BZOJ 1208 宠物收养所 | 平衡树模板题
BZOJ 1208 宠物收养所 我犯过的错误:删除一个节点后没有update新的根节点,导致size错了! #include <cstdio> #include <cmath> ...
- BZOJ 1208 [HNOI2004]宠物收养所:Splay(伸展树)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 题意: 有一个宠物收养所,在接下来一段时间内会陆续有一些宠物进到店里,或是一些人来领 ...
- HYSBZ 1208 宠物收养所 (Splay树)
题意:一家宠物收养所负责处理领养者与遗弃宠物业务,有人来领宠物,则领一只最理想的.若没有宠物了,领养者们就得等到宠物来,宠物一来立刻送给其中一个等待者.如果有两个理想的选择,则选择那个值较小的.收养所 ...
- B1208 [HNOI2004]宠物收养所 平衡树||set (滑稽)
这个题是一道splay裸题,但是我不太会写,所以用set直接水过去!!!哈哈哈哈,美滋滋. set总结: set是一个集合,然后里面没用重复的元素.里面有一些函数: begin() ,返回se ...
- 【BZOJ1208】宠物收养所(splay)
题意:见题面 思路:因为每个时刻要么全是人要么全是宠物,所以可以一棵splay解决 维护单点插入,单点删除,求前驱,求后继即可 ..,..]of longint; num,fa:..]of longi ...
- fjutacm 2492 宠物收养所 : Splay 模板 O(nlogn)
/** problem: http://www.fjutacm.com/Problem.jsp?pid=2492 Splay blog: https://tiger0132.blog.luogu.or ...
随机推荐
- mvc4 发布,遇到 403.14 问题,并且iis提示打开目录浏览。。。解决办法
经测试,只需要在web.config的system.web的同级节点,添加如下代码,即可完美运行.原理参考文章:一.<validation validateIntegratedModeConfi ...
- 慕课网-Java入门第一季-6-10 练习题
来源:http://www.imooc.com/ceping/1596 以下关于二维数组的定义和访问正确的是() A int[ ][ ] num = new int[ ][ ]; B int[ ][ ...
- C# 访问MongoDB 通用方法类
using MongoDB.Driver; using System; namespace MongoDBDemo { public class MongoDb { public MongoDb(st ...
- backup1
string _imgpath1 = WebConfigurationManager.AppSettings["IMGPATH1"].ToString(); string outp ...
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- td的所有style
td.style.clear= td.style.posRight=0 td.style.backgroundRepeat= td.style.borderTopStyle= td.style.mar ...
- C语言运算符优先级和口诀(转)
一共有十五个优先级: 1 () [] . -> 2 ! ~ -(负号) ++ -- &(取变量地址)* (type)(强制类型) sizeof 3 ...
- 使用Windows上的Eclipse 远程调试 linux下的Tomcat
1:修改Linux上Tomcat的catalina.sh,第一行添加declare -x CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_sock ...
- Nodejs学习(四)- express目录的分析
好久不来了,最近挺忙,就写一写下目录的情况吧. 我就说主要的目录,也就是我们经常用到的 public 用于存放一些js,css. routes 路由目录,如果你学过MVC应该不默生. views ...
- Des加解密算法
class DesHelper { /// <summary> /// DES加密方法 /// </summary> ...