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了...我还是继续看后缀自动机吧... ...
随机推荐
- formValidator 不支持jquery1.9以上的解决办法
不支持的原因是因为jquery1.9以上版本剔除了$.browser 在formValidator 源码最顶层.或者jquery源码里加上如下代码 $.browser = {};$.browser.m ...
- 为什么mysqld启动报错
在一台ubuntu测试机器上启动一个mysql实例,本来应该是一件很简单的事情, 启动的时候却报错了: mysqld_safe --defaults-file=/etc/mysql/my3307. ...
- Linux 进程--父进程查询子进程的退出状态
僵尸进程 当一个子进程先于父进程结束运行时,它与其父进程之间的关联还会保持到父进程也正常地结束运行,或者父进程调用了wait才告终止. 子进程退出时,内核将子进程置为僵尸状态,这个进程称为僵尸进程,它 ...
- ubuntu中tensorflow安装
首先加入了CUDA_HOM环境变量,然后下载了一个分析工具库,结果E: 无法定位软件包 libcupti-dev,在etc/apt 的sources.list 添加镜像源 deb http://arc ...
- 自定义Json格式
老铁们都知道,一般的json格式就是键值对格式,在一些特定的框架或者系统中,会用到自定义格式的json文件,假设我们要得到的特定格式json格式如下: {"A":"2&q ...
- lintcode-137-克隆图
137-克隆图 克隆一张无向图,图中的每个节点包含一个 label 和一个列表 neighbors. 数据中如何表示一个无向图?http://www.lintcode.com/help/graph/ ...
- php获取微信token和ticket并返回签名
jssdk.php页,主要获取token和ticket <?php /* * 微信公众号后台里获取appId和appSecret,并在公众号后台=>安全中心=>IP白名单中设置当前页 ...
- Currying & 柯里化
Currying & 柯里化 函数式编程 https://www.cnblogs.com/xgqfrms/p/5730527.html https://en.wikipedia.org/wik ...
- zTree删除节点
zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree删除节点. <!DOCTYPE html> &l ...
- 算法学习——kruskal重构树
kruskal重构树是一个比较冷门的数据结构. 其实可以看做一种最小生成树的表现形式. 在普通的kruskal中,如果一条边连接了在2个不同集合中的点的话,我们将合并这2个点所在集合. 而在krusk ...