[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本书的编号

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.

你可能感兴趣的:([BZOJ1861] [Zjoi2006]Book 书架)