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了...我还是继续看后缀自动机吧... ...
随机推荐
- ibatis常用sql
(1) 输入参数为单个值 <delete id="com.fashionfree.stat.accesslog.deleteMemberAccessLogsBefore" p ...
- 小程序如何去掉button组件的边框
小程序获取用户授权不再支持wx.getUserInfo方法,改为用button获取,格式如下 <button class="btn btn" open-type=" ...
- cocos2d-x 中菜单类
菜单相关类包含:菜单类和菜单项类,菜单类图,从类图可见Menu类继承于Layer. 菜单项类图,从图中可见所有的菜单项都是从BaseMenuItem继承而来的,BaseMenuItem是抽象类,具体使 ...
- Wannafly挑战赛21:C - 大水题
链接:Wannafly挑战赛21:C - 大水题 题意: 现在给你N个正整数ai,每个数给出一“好数程度” gi(数值相同但位置不同的数之间可能有不同的好数程度).对于在 i 位置的数,如果有一在j位 ...
- (转)基于CUDA的GPU光线追踪
作者:Asixa 链接:https://zhuanlan.zhihu.com/p/55855479 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 替STL. ...
- linux备忘录-系统服务daemon
服务(daemon)及其分类 Linux中的服务被称为daemon(daemon是守护神,恶鬼的意思哦).这些daemon会常驻在内存当中,从而对我们的系统和任务等进行一些辅助性的工作.实际上,dae ...
- 字面值常量&&转义序列
字面值常量举例: 字面值常量的分类 示例 备注 整型 42.024(八进制数).0x23(十六进制) short类型没有对应的字面值 浮点型 3.14.3.14E2(指数) 默认类型是double 字 ...
- Week8 Teamework from Z.XML-Z.XML游戏功能说明
我们小组的游戏终于新鲜出炉了,好开心~ 快来看看有什么功能吧. 游戏目标::=打倒最多的敌人,获得积分,放松心情,获取快乐. 游戏菜单::= 关于+设置+帮助+积分榜+开始游戏吧 (截图还在路上..) ...
- XML中的DTD语法
DTD(Document Type Definition),全称为文档类型定义. 文件清单:book.xml <?xml version="1.0" ?> <!D ...
- Python 类和对象-上
#类和对象 class Human: #属性 -> 成员属性(变量) ear = 2 mouth = 1 sex = 'man' age = 28 name = 'zhangwang' marr ...