bzoj 3223 splay模板 【pascal】

来源:互联网 发布:angela baby 知乎 编辑:程序博客网 时间:2024/04/30 12:09
const        sroot=-1;var        n,m,x,y, root   :longint;        i               :longint;        a               :array[-1..100010] of longint;        father,size,tree:array[-1..100010] of longint;        son             :array[-1..100010,0..1] of longint;        flag            :array[-1..100010] of boolean;procedure swap(var a,b:longint);var        c:longint;begin   c:=a;a:=b;b:=c;end;procedure update(x:longint);begin   size[x]:=size[son[x,1]]+size[son[x,0]]+1;end;function build(l,r:longint):longint;var        mid:longint;begin   mid:=(l+r)>>1;   build:=mid;   tree[mid]:=a[mid];   if (l<=mid-1) then   begin      son[mid,0]:=build(l,mid-1);      father[son[mid,0]]:=mid;   end;   if (mid+1<=r) then   begin      son[mid,1]:=build(mid+1,r);      father[son[mid,1]]:=mid;   end;   update(mid);end;procedure renew_reverse(x:longint);begin   swap(son[x,0],son[x,1]);   flag[x]:=not flag[x];end;procedure push_down(x:longint);var        l,r:longint;begin   l:=son[x,0];r:=son[x,1];   if flag[x] then   begin      if (l<>-1) then renew_reverse(l);      if (r<>-1) then renew_reverse(r);      flag[x]:=false;   end;end;function find(x:longint):longint;var        t:longint;begin   t:=root;   while true do   begin      push_down(t);      if size[son[t,0]]+1=x then exit(t);      if size[son[t,0]]+1>x then t:=son[t,0] else      begin         dec(x,size[son[t,0]]+1);         t:=son[t,1];      end;   end;end;procedure ro(x,y:longint);var        f:longint;begin   push_down(x);   f:=father[x];   son[f,y]:=son[x,y xor 1];   father[son[x,y xor 1]]:=f;   if f=root then root:=x else    if f=son[father[f],0] then son[father[f],0]:=x     else son[father[f],1]:=x;   //   father[x]:=father[f];   father[f]:=x;   son[x,y xor 1]:=f;   update(f);   update(x);end;procedure splay(x,y:longint);var        u,v:longint;begin   while father[x]<>y do   begin      if father[father[x]]=y then ro(x,ord(x=son[father[x],1])) else      begin         if x=son[father[x],0] then u:=1 else u:=-1;         if father[x]=son[father[father[x]],0] then v:=1 else v:=-1;         if u*v=1 then         begin            ro(father[x],ord(x=son[father[x],1]));            ro(x,ord(x=son[father[x],1]));         end else         begin            ro(x,ord(x=son[father[x],1]));            ro(x,ord(x=son[father[x],1]));         end;      end;   end;   update(x);end;procedure reverse(l,r:longint);var        p:longint;begin   p:=find(l); splay(p,sroot);   p:=find(r+2); splay(p,root);   p:=son[son[root,1],0];   renew_reverse(p);end;begin   read(n,m);   fillchar(son,sizeof(son),255);   for i:=1 to n do a[i]:=i;   inc(n);   root:=build(0,n);   father[root]:=sroot;   //   for i:=1 to m do   begin      read(x,y);      reverse(x,y);   end;   for i:=2 to n do write(find(i),' ');writeln;end.

——by Eirlys


0 0