[BZOJ1861] [Zjoi2006]Book 书架
2016-01-11 20:14
441 查看
传送门
http://www.lydsy.com/JudgeOnline/problem.php?id=1861题目大意
支持操作1.Top S——表示把编号为S的书放在最上面
2.Bottom S——表示把编号为S的书放在最下面
3.Insert S T——T∈{-1,0,1},若编号为S的书上面有X本书,则这条命令表示把这本书放回去后它的上面有X+T本书
4.Ask S——询问编号为S的书的上面目前有多少本书
5.Query S——询问从上面数起的第S本书的编号
const maxn=100005; var w:array[-1..maxn,1..5]of longint; x:array[-1..maxn]of longint; i,j,k:longint; n,m,a,b,sum,root:longint; cha,cha1:char; procedure print(a:longint); begin if w[a,1]<>-1 then print(w[a,1]); write(w[a,5],' '); if w[a,2]<>-1 then print(w[a,2]); if a=root then writeln; end; procedure rotate(a,kind:longint); var b,unkind:longint; begin b:=w[a,3]; unkind:=kind xor 3; w[a,4]:=w[b,4]; dec(w[b,4],1+w[w[a,kind],4]); w[w[a,unkind],3]:=b; w[b,kind]:=w[a,unkind]; w[a,unkind]:=b; w[a,3]:=w[b,3]; w[b,3]:=a; if w[a,3]<>-1 then if w[w[a,3],1]=b then w[w[a,3],1]:=a else w[w[a,3],2]:=a; end; procedure splay(a,goal:longint); var b,kind,unkind:longint; begin while w[a,3]<>goal do begin b:=w[a,3]; if w[b,1]=a then kind:=1 else kind:=2; unkind:=kind xor 3; if w[b,3]=goal then rotate(a,kind) else if w[w[b,3],kind]=b then begin rotate(b,kind); rotate(a,kind); end else begin rotate(a,kind); rotate(a,unkind); end; end; if goal=-1 then root:=a; end; procedure init(a:longint); begin inc(sum); x[a]:=sum; w[sum,1]:=-1; w[sum,2]:=-1; w[sum,3]:=root; w[sum,4]:=1; w[sum,5]:=a; w[root,2]:=sum; inc(w[root,4]); if a=n+1 then w[sum,5]:=-maxlongint; splay(sum,-1); end; function getmax(a:longint):longint; var tt:longint; begin tt:=a; while w[tt,2]<>-1 do tt:=w[tt,2]; exit(tt); end; procedure top(a:longint); var tt,fa:longint; begin splay(x[a],-1); splay(getmax(w[root,1]),root); w[w[root,1],2]:=w[root,2]; root:=w[root,1]; w[w[root,2],3]:=root; w[root,3]:=-1; w[root,4]:=w[w[root,1],4]+1+w[w[root,2],4]; splay(x[0],-1); tt:=w[root,2]; inc(w[root,4]); while tt<>-1 do begin inc(w[tt,4]); fa:=tt; tt:=w[tt,1]; end; w[x[a],1]:=-1; w[x[a],2]:=-1; w[x[a],3]:=fa; w[x[a],4]:=1; w[x[a],5]:=a; w[fa,1]:=x[a]; splay(x[a],-1); end; procedure bottom(a:longint); var tt,fa:longint; begin splay(x[a],-1); splay(getmax(w[root,1]),root); w[w[root,1],2]:=w[root,2]; root:=w[root,1]; w[w[root,2],3]:=root; w[root,3]:=-1; w[root,4]:=w[w[root,1],4]+1+w[w[root,2],4]; splay(x[n+1],-1); tt:=w[root,1]; inc(w[root,4]); while tt<>-1 do begin inc(w[tt,4]); fa:=tt; tt:=w[tt,2]; end; w[x[a],1]:=-1; w[x[a],2]:=-1; w[x[a],3]:=fa; w[x[a],4]:=1; w[x[a],5]:=a; w[fa,2]:=x[a]; splay(x[a],-1); end; procedure ask(a:longint); begin splay(x[a],-1); writeln(w[w[root,1],4]-1); end; procedure query(a:longint); var tt:longint; begin inc(a); tt:=root; while (w[w[tt,1],4]>=a)or(a>=w[w[tt,1],4]+2) do if a<=w[w[tt,1],4] then tt:=w[tt,1] else begin dec(a,w[w[tt,1],4]+1); tt:=w[tt,2]; end; writeln(w[tt,5]); end; procedure insert(a,b:longint); var tt,fa,c:longint; begin if b=0 then exit; splay(x[a],-1); c:=w[w[root,1],4]+b+1; splay(getmax(w[root,1]),root); w[w[root,1],2]:=w[root,2]; root:=w[root,1]; w[w[root,2],3]:=root; w[root,3]:=-1; w[root,4]:=w[w[root,1],4]+1+w[w[root,2],4]; tt:=root; while (w[w[tt,1],4]>=c-1)or(c-1>=w[w[tt,1],4]+2) do if c-1<=w[w[tt,1],4] then tt:=w[tt,1] else begin dec(c,w[w[tt,1],4]+1); tt:=w[tt,2]; end; splay(tt,-1); tt:=w[root,2]; inc(w[root,4]); while tt<>-1 do begin inc(w[tt,4]); fa:=tt; tt:=w[tt,1]; end; w[x[a],1]:=-1; w[x[a],2]:=-1; w[x[a],3]:=fa; w[x[a],4]:=1; w[x[a],5]:=a; w[fa,1]:=x[a]; end; begin readln(n,m); sum:=1; root:=1; w[1,1]:=-1; w[1,2]:=-1; w[1,3]:=-1; w[1,4]:=1; w[1,5]:=-maxlongint; x[0]:=1; for i:=1 to n do begin read(a); init(a); end; init(n+1); readln; //print(root); for i:=1 to m do begin read(cha); if (cha='B')or(cha='I') then for j:=1 to 5 do read(cha1) else if (cha='Q') then for j:=1 to 4 do read(cha1) else for j:=1 to 2 do read(cha1); case cha of 'T':begin readln(a); top(a); end; 'B':begin readln(a); bottom(a); end; 'I':begin readln(a,b); insert(a,b); end; 'A':begin readln(a); ask(a); end; 'Q':begin readln(a); query(a); end; end; //print(root); end; end.
相关文章推荐
- 地球坐标,火星坐标,百度坐标转换
- python note
- 使用dom4j 读写xml文件
- Flume 安装与配置
- IOS上 关于状态栏的相关设置(UIStatusBar)
- iStat Menus注册码
- XILINX MCB DDR3读写操作笔记
- Action返回自定义类的List集合通过JSP中的Struct2标签显示
- 安装nodejs搭建ghost个人博客
- osmocom-bb中用osmocon刷入固件命令那些参数你都弄懂了吗?
- 重拾编程之路--快速排序算法
- 1071. Speech Patterns (25)
- 如何配置RadosGW支持多个placement target / data pools
- 巧用XRename批量删除所有文件的文件名中的数字
- HDU_P2586 How far away?(LCA+Tarjan算法)
- redhat使用163的yum源
- ZooKeeper 集群的安装与配置
- Java中的异常体系
- 蓝懿IOS学习字典(NSDictionary)&JSON
- Android回调事件传播-android学习之旅(四十五)