zoukankan      html  css  js  c++  java
  • 【BZOJ3223】文艺平衡树(splay)

    题意:维护数列的翻转

    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.
  • 相关阅读:
    3、第3课CSS块级、行内元素、绝对定位、相对定位、固定位置20150922
    Easyui 正则表达式
    struts 标签
    源码详解Java的反射机制
    MyEclipse8.6安装 spket 插件
    有些路,只能一个人走
    HG8245获取超级管理员(telecomadmin)密码的方法
    easyui doc
    oracle建立自动增长字段
    Java实现二维码QRCode的编码和解码
  • 原文地址:https://www.cnblogs.com/myx12345/p/6556088.html
Copyright © 2011-2022 走看看