bzoj 3223 splay模板 【pascal】

61 篇文章 0 订阅
5 篇文章 0 订阅
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值