bzoj 1861 splay
就是裸地splay,然后自己写的不是特别好,tle了,最近时间比较紧迫,有时间了改下,在此记录
另附转载pascal AC代码最下面
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Time_Limit_Exceed
****************************************************************/ //By BLADEVIL
const
sroot =-; var
num, adress :array[-..] of longint;
son :array[-..,..] of longint;
father, size, tree :array[-..] of longint;
root :longint;
n, m :longint; procedure init;
var
i :longint;
begin
read(n,m);
for i:= to n do
begin
read(num[i]);
adress[num[i]]:=i;
end;
readln;
end; procedure update(x:longint);
begin
size[x]:=size[son[x,]]+size[son[x,]]+;
end; function find(x:longint):longint;
var
t :longint;
begin
t:=root;
while true do
begin
if size[son[t,]]+=x then exit(t);
if size[son[t,]]+>x then t:=son[t,] else
begin
dec(x,size[son[t,]]+);
t:=son[t,];
end;
end;
end; procedure rotate(x,y:longint);
var
f :longint;
begin
f:=father[x];
father[son[x,y xor ]]:=f;
son[f,y]:=son[x,y xor ];
if f=root then root:=x
else
if f=son[father[f],] then
son[father[f],]:=x else
son[father[f],]:=x;
father[x]:=father[f];
father[f]:=x;
son[x,y xor ]:=f;
update(f);
update(x);
end; procedure splay(x,y:longint);
var
u, v :longint;
begin
while father[x]<>y do
if father[father[x]]=y then
rotate(x,ord(x=son[father[x],])) else
begin
if x=son[father[x],] then u:= else u:=-;
if father[x]=son[father[father[x]],] then v:= else v:=-;
if u*v= then
begin
rotate(father[x],ord(x=son[father[x],]));
rotate(x,ord(x=son[father[x],]));
end else
begin
rotate(x,ord(x=son[father[x],]));
rotate(x,ord(x=son[father[x],]));
end;
end;
update(x);
end; function build(l,r:longint):longint;
var
mid :longint;
begin
mid:=(l+r) div ;
build:=mid;
tree[mid]:=num[mid];
if l<=mid- then
begin
son[mid,]:=build(l,mid-);
father[son[mid,]]:=mid;
end;
if r>=mid+ then
begin
son[mid,]:=build(mid+,r);
father[son[mid,]]:=mid;
end;
update(mid);
end; procedure top;
var
x :longint;
q, p :longint;
begin
readln(x);
x:=adress[x];
splay(x,sroot);
q:=size[son[root,]];
p:=find(q); splay(p,sroot);
p:=find(q+); splay(p,root);
p:=son[son[root,],];
son[son[root,],]:=-;
update(son[root,]);
update(root);
father[p]:=-;
q:=find(); splay(q,sroot);
q:=find(); splay(q,root);
son[son[root,],]:=p;
father[p]:=son[root,];
update(son[root,]);
update(root);
end; procedure bottom;
var
x :longint;
q, p :longint;
begin
readln(x);
x:=adress[x];
splay(x,sroot);
q:=size[son[x,]];
p:=find(q); splay(p,sroot);
p:=find(q+); splay(p,root);
q:=son[son[root,],];
son[son[root,],]:=-;
father[q]:=-;
update(son[root,]);
update(root);
p:=find(n-); splay(p,sroot);
p:=find(n); splay(p,root);
son[son[root,],]:=q;
father[q]:=son[root,];
update(son[root,]);
update(root);
end; procedure insert;
var
x, y :longint;
q, p :longint; begin
readln(x,y);
x:=adress[x];
if y=- then
begin
splay(x,sroot);
p:=size[son[root,]];
q:=find(p); splay(q,sroot);
q:=find(p+); splay(q,root);
x:=son[son[root,],];
son[son[root,],]:=-;
q:=find(p-); splay(q,sroot);
q:=find(p); splay(q,root);
son[son[root,],]:=x;
father[x]:=son[son[root,],];
end else
begin splay(x,sroot);
p:=size[son[root,]];
q:=find(p); splay(q,sroot);
q:=find(p+); splay(q,root);
x:=son[son[root,],];
son[son[root,],]:=-;
q:=find(p); splay(q,sroot);
q:=find(p+); splay(q,root);
son[son[root,],]:=x;
father[x]:=son[son[root,],];
end;
end; procedure ask;
var
x :longint;
begin
readln(x);
x:=adress[x];
splay(x,sroot);
writeln(size[son[root,]]-);
end; procedure query;
var
x :longint;
p :longint;
begin
readln(x);
p:=find(x+);
splay(p,sroot);
writeln(tree[root]);
end; procedure main;
var
i :longint;
ch :char;
s :ansistring; begin
fillchar(son,sizeof(son),);
inc(n);
root:=build(,n);
father[root]:=sroot;
for i:= to m do
begin
s:='';
read(ch);
while ch<>' ' do
begin
s:=s+ch;
read(ch);
end;
if s='Top' then top;
if s='Bottom' then bottom;
if s='Insert' then insert;
if s='Ask' then ask;
if s='Query' then query;
end; end; begin
init;
main;
end.
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ {$inline on}
program zjoi__day2_book;
var fa,pi,po,son:array[..] of longint;
c:array[..,..] of longint;
root,n,m:longint;
//=====================================================================
procedure rotate(var root:longint; x:longint); inline;
var y,z,p,q:longint;
begin
y:=fa[x]; z:=fa[y];
if c[y,]=x then p:= else p:=;
q:=p xor ;
if y=root then root:=x else
if c[z,]=y then c[z,]:=x else c[z,]:=x;
if c[x,q]> then fa[c[x,q]]:=y;
fa[x]:=z; fa[y]:=x;
c[y,p]:=c[x,q]; c[x,q]:=y;
son[y]:=son[c[y,]]+son[c[y,]]+;
son[x]:=son[y]+son[c[x,p]]+; //一开始这里没加1
end;
//=====================================================================
procedure splay(var root:longint; x:longint); inline;
var y,z:longint;
begin
while x<>root do
begin
y:=fa[x]; z:=fa[y];
if y<>root then
if (c[y,]=x) xor (c[z,]=y) then
rotate(root,x) else rotate(root,y);
rotate(root,x);
end;
end; //经过多次实践,表示敲出splay和rotate的代码可以选择跳过调试了、、如果没有更新附加域的话。。
//=====================================================================
procedure init;
var i,x:longint;
begin
readln(n,m);
for i:= to n do
begin
read(x); pi[x]:=i;
po[i]:=x; son[i]:=n-i+;
end; readln; //这里没有readln的错误还是第一次碰到。。。
for i:= to n- do c[i,]:=i+;
for i:= to n do fa[i]:=i-;
root:=; splay(root,n);
end;
//=====================================================================
function delete(x:longint):longint; inline;
var left,right:longint;
begin
splay(root,x);
left:=c[root,]; right:=c[root,];
while c[left,]> do left:=c[left,];
while c[right,]> do right:=c[right,];
if left> then splay(root,left);
if right> then
if left> then splay(c[root,],right) else
splay(root,right);
if right= then
begin delete:=n;
c[left,]:=;
if left> then dec(son[left]);
end else
begin
if left> then
delete:=son[left]-son[right]+ else
delete:=;
c[right,]:=; dec(son[right]);
if left> then dec(son[left]);
end; fa[x]:=; son[x]:=;
end;
//=====================================================================
function find(x:longint):longint; inline;
var i:longint;
begin
i:=root;
while i<> do
begin
if son[c[i,]]+=x then exit(i);
if son[c[i,]]>=x then i:=c[i,] else
begin
dec(x,son[c[i,]]+); i:=c[i,];
end;
end; exit(i);
end;
//=====================================================================
procedure ins(x,y:longint); inline;
var left,right:longint;
begin
left:=find(y-); right:=find(y);
if left> then splay(root,left);
if right> then
if left> then splay(c[root,],right) else
splay(root,right);
if right= then
begin
fa[x]:=left; c[left,]:=x; inc(son[left]);
end else
begin
inc(son[right]); fa[x]:=right; c[right,]:=x;
if left> then inc(son[left]);
end;
end;
//=====================================================================
procedure main;
var ch,ch1:char;
i,s,t,pos:longint;
begin
for i:= to m do
begin
repeat
ch1:=ch; read(ch);
until ch=' '; ch:=ch1; //如果init里面没有readln这里就会直接跳掉。读不到指令。然后就215了。。。
if ch='p' then
begin readln(s);
pos:=delete(pi[s]); ins(pi[s],);
end else
if ch='m' then
begin readln(s);
pos:=delete(pi[s]); ins(pi[s],n);
end else
if ch='t' then
begin readln(s,t);
if t= then continue;
pos:=delete(pi[s]); ins(pi[s],pos+t);
end else
if ch='k' then
begin readln(s);
splay(root,pi[s]);
writeln(son[c[root,]]);
end else
begin readln(s);
pos:=find(s); splay(root,pos);
writeln(po[pos]);
end;
end;
end;
//=====================================================================
begin
//assign(input,'book.in'); reset(input);
//assign(output,'book.out'); rewrite(output);
init;
main;
// close(input); close(output);
end.
bzoj 1861 splay的更多相关文章
- [题解]bzoj 1861 Book 书架 - Splay
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1396 Solved: 803[Submit][Stat ...
- BZOJ 1861: [Zjoi2006]Book 书架 splay
1861: [Zjoi2006]Book 书架 Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书 ...
- BZOJ 1861: [Zjoi2006]Book 书架 (splay)
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1453 Solved: 822[Submit][Stat ...
- BZOJ 1861: [Zjoi2006]Book 书架 | SPlay 板题
#include<cstdio> #include<algorithm> #include<cstring> #define N 80010 #define whi ...
- BZOJ 1861 [Zjoi2006]Book 书架 ——Splay
[题目分析] 模板题目. 首尾两个虚拟结点,十分方便操作. [代码] #include <cstdio> #include <cstring> #include <cma ...
- BZOJ 1861: [Zjoi2006]Book 书架
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1290 Solved: 740[Submit][Stat ...
- bzoj 1269 bzoj 1507 Splay处理文本信息
bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...
- bzoj 3506 && bzoj 1552 splay
查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...
- bzoj 1014 splay维护hash值
被后缀三人组虐了一下午,写道水题愉悦身心. 题很裸,求lcq时二分下答案就行了,写的不优美会被卡时. (写题时精神恍惚,不知不觉写了快两百行...竟然调都没调就A了...我还是继续看后缀自动机吧... ...
随机推荐
- iOS笔记058 - IOS之多线程
IOS开发中多线程 主线程 一个iOS程序运行后,默认会开启1条线程,称为"主线程"或"UI线程" 作用 显示和刷新界面 处理UI事件(点击.滚动.拖拽等) 注 ...
- safari 移动下开启 滚定回弹
-webkit-overflow-scrolling : touch;
- log报错: Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
报错: 解决方式: 1.登录数据库查看错误原因 结果发现账号无法正常登录出现账号被锁定的错误. 2.如何账号解锁? 用sys系统管理员账号登录数据库 SQL> alter user 用户名 ac ...
- CPU拓扑结构
本篇旨在认识一下以下三种CPU拓扑结构分别是什么: Symmetric multiprocessing (SMP) Non-uniform memory access (NUMA) Simultane ...
- JavaSE复习(五)网络编程
客户端:java.net.Socket 类表示.创建Socket对象,向服务端发出连接请求,服务端响应请求,两者建立连接开始通信 服务端:java.net.ServerSocket 类表示.创建Ser ...
- coredump分析
首先通过命令 gdb freeswitch core.60954进入gdb. 这里freeswitch 是产生coredump的可执行应用,core.60954是应用产生的coredump文件. 然后 ...
- 【bzoj2152】聪聪可可 树的点分治
题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...
- P2483 【模板】k短路([SDOI2010]魔法猪学院)
题目背景 感谢@kczno1 @X_o_r 提供hack数据 题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界 ...
- 2017 Multi-University Training Contest - Team 4 phone call(树+lca+并查集)
题解: (并查集处理往上跳的时候,一定要先让u,v往上跳到并查集的祖先,不然会wa掉) 代码如下: #include <iostream> #include <algorithm&g ...
- MySQL触发器写法
触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/dele ...