就是裸地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的更多相关文章

  1. [题解]bzoj 1861 Book 书架 - Splay

    1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1396  Solved: 803[Submit][Stat ...

  2. BZOJ 1861: [Zjoi2006]Book 书架 splay

    1861: [Zjoi2006]Book 书架 Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书 ...

  3. BZOJ 1861: [Zjoi2006]Book 书架 (splay)

    1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1453  Solved: 822[Submit][Stat ...

  4. BZOJ 1861: [Zjoi2006]Book 书架 | SPlay 板题

    #include<cstdio> #include<algorithm> #include<cstring> #define N 80010 #define whi ...

  5. BZOJ 1861 [Zjoi2006]Book 书架 ——Splay

    [题目分析] 模板题目. 首尾两个虚拟结点,十分方便操作. [代码] #include <cstdio> #include <cstring> #include <cma ...

  6. BZOJ 1861: [Zjoi2006]Book 书架

    1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1290  Solved: 740[Submit][Stat ...

  7. bzoj 1269 bzoj 1507 Splay处理文本信息

    bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...

  8. bzoj 3506 && bzoj 1552 splay

    查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...

  9. bzoj 1014 splay维护hash值

    被后缀三人组虐了一下午,写道水题愉悦身心. 题很裸,求lcq时二分下答案就行了,写的不优美会被卡时. (写题时精神恍惚,不知不觉写了快两百行...竟然调都没调就A了...我还是继续看后缀自动机吧... ...

随机推荐

  1. Android TextView 单行文本的坑

    这是android系统的一个bug,描述如下:https://code.google.com/p/android/issues/detail?id=33868 具体来说就是当一个TextView设置了 ...

  2. 源码-集合:ArrayList

    只是文章摘录,还未研究 JAVA ArrayList详细介绍(示例) http://www.jb51.net/article/42764.htm Jdk1.6 JUC源码解析汇总 - 永远保持敬畏之心 ...

  3. zabbix 2.2 调小监控值

    zabbix_agent默认disk下小于0%告警 调小到5% 组态 > 模板 选择需要的模板的触发器 例如 Template OS Windows 选择触发器 - 探索规则 - 触发器类型  ...

  4. 装机、UEFI双系统安装

    装机设置 设置默认中图标显示查看-选项-查看-应用到文件夹 控制面板-语言-管理输入法 word-选项-取消输入法设置处于活动状态word-字体-设置默认值 高DPI的显示屏,需要使用125%的缩放, ...

  5. 【iOS开发】iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 150)]; label.backgroundColor ...

  6. 软工实践 - 第二十九次作业 Beta 冲刺(7/7)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10159251.html 作业博客:[班级博客本次作业的链接] (https://edu.cnb ...

  7. C++中范围for语句

    如果想对string对象中的每个字符做点什么操作,目前最好的办法是使用C++11新标准提供的一种语句:范围for(range for)语句. 示例代码: #include<iostream> ...

  8. CodeForces Round #521 (Div.3) D. Cutting Out

    http://codeforces.com/contest/1077/problem/D You are given an array ss consisting of nn integers. Yo ...

  9. struts2 下载文件

    作者:禅楼望月 当下载的文件名字中不含有汉字,或者下载的文件不需要考虑用户的权限问题时.直接让超链接的href属性为所要下载的文件名即可.否则最好使用struts2的文件下载机制. 以下载图片为例 完 ...

  10. VS 附加进程调试

    1.在IIS 上新建一个项目,制定目录到 项目根目录. 2.制定IIS上指定 主机名称如: vk.com 3. 修改主机HOST 文件:C:\Windows\System32\drivers\etc ...