码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ3223】文艺平衡树(splay)

时间:2017-03-15 20:44:25      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:pre   wap   tput   pushd   input   logs   push   put   rewrite   

题意:维护数列的翻转

n<=100000

思路:裸splay,仅维护rever

  1 var t:array[0..200000,0..1]of longint;
  2     size,rev,b,fa,a:array[0..200000]of longint;
  3     n,m,i,x,y,root:longint;
  4 
  5 procedure pushup(p:longint);
  6 begin
  7  size[p]:=size[t[p,0]]+size[t[p,1]]+1;
  8 end;
  9 
 10 procedure swap(var x,y:longint);
 11 var t:longint;
 12 begin
 13  t:=x; x:=y; y:=t;
 14 end;
 15 
 16 procedure pushdown(p:longint);
 17 var l,r:longint;
 18 begin
 19  l:=t[p,0]; r:=t[p,1];
 20  if rev[p]=1 then
 21  begin
 22   rev[p]:=rev[p] xor 1; rev[l]:=rev[l] xor 1; rev[r]:=rev[r] xor 1;
 23   swap(t[p,0],t[p,1]);
 24  end;
 25 end;
 26 
 27 procedure rotate(x:longint;var k:longint);
 28 var y,z,l,r:longint;
 29 begin
 30  y:=fa[x]; z:=fa[y];
 31  if t[y,0]=x then l:=0
 32   else l:=1;
 33  r:=l xor 1;
 34  if y<>k then
 35  begin
 36   if t[z,0]=y then t[z,0]:=x
 37    else t[z,1]:=x;
 38  end
 39   else k:=x;
 40  fa[x]:=z; fa[y]:=x; fa[t[x,r]]:=y;
 41  t[y,l]:=t[x,r]; t[x,r]:=y;
 42  pushup(y);
 43  pushup(x);
 44 end;
 45 
 46 procedure splay(x:longint;var k:longint);
 47 var y,z:longint;
 48 begin
 49  while x<>k do
 50  begin
 51   y:=fa[x]; z:=fa[y];
 52   if y<>k then
 53   begin
 54    if (t[y,0]=x)xor(t[z,0]=y) then rotate(x,k)
 55     else rotate(y,k)
 56   end
 57    else k:=x;
 58   rotate(x,k);
 59  end;
 60 end;
 61 
 62 procedure build(l,r,x:longint);
 63 var mid,now:longint;
 64 begin
 65  if l>r then exit;
 66  mid:=(l+r)>>1; now:=mid;
 67  if l=r then
 68  begin
 69   size[now]:=1;
 70  end
 71   else
 72   begin
 73    build(l,mid-1,mid);
 74    build(mid+1,r,mid);
 75   end;
 76  b[now]:=a[mid]; fa[now]:=x;
 77  pushup(now);
 78  if mid>=x then t[x,1]:=now
 79   else t[x,0]:=now;
 80 end;
 81 
 82 function findkth(x:longint):longint;
 83 var k,tmp:longint;
 84 begin
 85  k:=root;
 86  while k<>0 do
 87  begin
 88   pushdown(k);
 89   tmp:=size[t[k,0]]+1;
 90   if tmp=x then exit(k)
 91    else if tmp>x then k:=t[k,0]
 92     else
 93     begin
 94      x:=x-tmp;
 95      k:=t[k,1];
 96     end;
 97  end;
 98 end;
 99 
100 procedure rever(x,y:longint);
101 var i,j:longint;
102 begin
103  i:=findkth(x); j:=findkth(y+2);
104  splay(i,root);
105  splay(j,t[i,1]);
106  x:=t[j,0];
107  rev[x]:=rev[x] xor 1;
108 end;
109 
110 begin
111  assign(input,bzoj3223.in); reset(input);
112  assign(output,bzoj3223.out); rewrite(output);
113  readln(n,m);
114  a[1]:=-maxlongint; a[n+2]:=maxlongint;
115  for i:=2 to n+1 do a[i]:=i;
116  build(1,n+2,0); root:=(n+3)>>1;
117  for i:=1 to m do
118  begin
119   readln(x,y);
120   rever(x,y);
121  end;
122  for i:=2 to n+1 do
123   write(b[findkth(i)]-1, );
124  close(input);
125  close(output);
126 end.

 

【BZOJ3223】文艺平衡树(splay)

标签:pre   wap   tput   pushd   input   logs   push   put   rewrite   

原文地址:http://www.cnblogs.com/myx12345/p/6556088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!