本文主要是介绍[BZOJ1861] [Zjoi2006]Book 书架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门
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本书的编号
constmaxn=100005;
varw: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);
beginif 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;
beginb:=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]<>-1thenif w[w[a,3],1]=bthen w[w[a,3],1]:=aelse w[w[a,3],2]:=a;
end;procedure splay(a,goal:longint);
var b,kind,unkind:longint;
beginwhile w[a,3]<>goal dobeginb:=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)elseif w[w[b,3],kind]=bthen begin rotate(b,kind); rotate(a,kind); endelse begin rotate(a,kind); rotate(a,unkind); end;end;if goal=-1 then root:=a;
end;procedure init(a:longint);
begininc(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;
begintt:=a;while w[tt,2]<>-1 dott:=w[tt,2];exit(tt);
end;procedure top(a:longint);
var tt,fa:longint;
beginsplay(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 dobegin 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;
beginsplay(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 dobegin 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);
beginsplay(x[a],-1);writeln(w[w[root,1],4]-1);
end;procedure query(a:longint);
var tt:longint;
begininc(a); tt:=root;while (w[w[tt,1],4]>=a)or(a>=w[w[tt,1],4]+2) doif 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;
beginif 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) doif 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 dobegin 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;beginreadln(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 dobegin read(a); init(a); end;init(n+1);readln;//print(root);for i:=1 to m dobeginread(cha);if (cha='B')or(cha='I')then for j:=1 to 5 do read(cha1)elseif (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.
这篇关于[BZOJ1861] [Zjoi2006]Book 书架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!