zoukankan      html  css  js  c++  java
  • 【BZOJ1014】火星人prefix(splay,Hash)

    题意:

    思路:

      1 const mo=9000011;
      2 var t:array[0..200000,0..1]of longint;
      3     sum,size,fa,a,b,id,mi:array[0..200000]of longint;
      4     n,m,i,x,y,s,k,j,cnt,root:longint;
      5     ch:ansistring;
      6  
      7 procedure swap(var x,y:longint);
      8 var t:longint;
      9 begin
     10  t:=x; x:=y; y:=t;
     11 end;
     12  
     13 procedure pushup(x:longint);
     14 var l,r:longint;
     15 begin
     16  l:=t[x,0]; r:=t[x,1];
     17  size[x]:=size[l]+size[r]+1;
     18  sum[x]:=(sum[l]+int64(mi[size[l]])*b[x]+int64(mi[size[l]+1])*sum[r]) mod mo;
     19  // sum[x]:=(sum[r]+int64(mi[size[r]])*b[x]+int64(mi[size[r]+1])*sum[l]) mod mo; 也可以
     20 end;
     21  
     22 function min(x,y:longint):longint;
     23 begin
     24  if x<y then exit(x);
     25  exit(y);
     26 end;
     27  
     28 procedure rotate(x:longint;var k:longint);
     29 var y,z,l,r:longint;
     30 begin
     31  y:=fa[x]; z:=fa[y];
     32  if t[y,0]=x then l:=0
     33   else l:=1;
     34  r:=l xor 1;
     35  if y<>k then
     36  begin
     37   if t[z,0]=y then t[z,0]:=x
     38    else t[z,1]:=x;
     39  end
     40   else k:=x;
     41  fa[x]:=z; fa[y]:=x; fa[t[x,r]]:=y;
     42  t[y,l]:=t[x,r]; t[x,r]:=y;
     43  pushup(y);
     44  pushup(x);
     45 end;
     46  
     47 procedure splay(x:longint;var k:longint);
     48 var y,z:longint;
     49 begin
     50  while x<>k do
     51  begin
     52   y:=fa[x]; z:=fa[y];
     53   if y<>k then
     54   begin
     55    if (t[y,0]=x)xor(t[z,0]=y) then rotate(x,k)
     56     else rotate(y,k);
     57   end
     58    else k:=x;
     59   rotate(x,k);
     60  end;
     61 end;
     62  
     63 procedure build(l,r,x:longint);
     64 var mid,now,last:longint;
     65 begin
     66  if l>r then exit;
     67  now:=id[l]; last:=id[x];
     68  if l=r then
     69  begin
     70   sum[now]:=a[now]; b[now]:=a[now];
     71   fa[now]:=last; size[now]:=1;
     72   if l<x then t[last,0]:=now
     73    else t[last,1]:=now;
     74   exit;
     75  end;
     76  mid:=(l+r)>>1; now:=id[mid];
     77  build(l,mid-1,mid);
     78  build(mid+1,r,mid);
     79  b[now]:=a[mid]; fa[now]:=last;
     80  pushup(now);
     81  if mid<x then t[last,0]:=now
     82   else t[last,1]:=now;
     83 end;
     84  
     85 function kth(x:longint):longint;
     86 var k,tmp:longint;
     87 begin
     88  k:=root;
     89  while k<>0 do
     90  begin
     91   tmp:=size[t[k,0]]+1;
     92   if tmp=x then exit(k);
     93   if tmp>x then k:=t[k,0]
     94    else
     95    begin
     96     x:=x-tmp;
     97     k:=t[k,1];
     98    end;
     99  end;
    100 end;
    101  
    102 function query(k,tot:longint):longint;
    103 var x,y:longint;
    104 begin
    105  x:=kth(k); y:=kth(k+tot+1);
    106  splay(x,root);
    107  splay(y,t[x,1]);
    108  exit(sum[t[y,0]]);
    109 end;
    110  
    111 function ask(x,y:longint):longint;
    112 var l,r,mid,last:longint;
    113 begin
    114  l:=1; r:=min(cnt-x,cnt-y)-1; last:=0;
    115  while l<=r do
    116  begin
    117   mid:=(l+r)>>1;
    118   if query(x,mid)=query(y,mid) then begin last:=mid; l:=mid+1; end
    119    else r:=mid-1;
    120  end;
    121  exit(last);
    122 end;
    123  
    124 procedure ins(x,y:longint);
    125 var p,q,z:longint;
    126 begin
    127  p:=kth(x+1);
    128  q:=kth(x+2);
    129  splay(p,root);
    130  splay(q,t[p,1]);
    131  inc(cnt); z:=cnt; t[q,0]:=z;
    132  fa[z]:=q; b[z]:=y;
    133  pushup(z);
    134  pushup(q);
    135  pushup(p);
    136 end;
    137  
    138 begin
    139  
    140  readln(ch);
    141  n:=length(ch);
    142  for i:=2 to n+1 do a[i]:=ord(ch[i-1])-ord('a')+1;
    143  mi[0]:=1;
    144  for i:=1 to 150000 do mi[i]:=mi[i-1]*27 mod mo;
    145  for i:=1 to n+2 do id[i]:=i;
    146  build(1,n+2,0); root:=(n+3)>>1; cnt:=n+2;
    147  readln(m);
    148  for i:=1 to m do
    149  begin
    150   readln(ch); k:=length(ch); s:=0; x:=0; y:=0;
    151   for j:=3 to k do
    152   begin
    153    if ch[j]=' ' then begin inc(s); continue; end;
    154    if s=0 then x:=x*10+ord(ch[j])-ord('0');
    155    if s=1 then y:=y*10+ord(ch[j])-ord('0');
    156   end;
    157   if ch[1]='Q' then writeln(ask(x,y));
    158   if ch[1]='R' then
    159   begin
    160    y:=y+ord('0')-ord('a')+1;
    161    k:=kth(x+1); splay(k,root);
    162    b[k]:=y; pushup(root);
    163   end;
    164   if ch[1]='I' then
    165   begin
    166    y:=y+ord('0')-ord('a')+1;
    167    ins(x,y);
    168   end;
    169  end;
    170  
    171 end.
  • 相关阅读:
    hdu 5269 ZYB loves Xor I &amp;&amp; BestCoder Round #44
    Hibernate之关于多对多单向关联映射
    測试CPU支持指令集AVX,AVX2,SSE情况的代码【VS2010调试通过】
    联想教育应用使用说明(7.6版本号)——第5章 常见的问题与解答
    java.lang.ClassNotFoundException: org.springframework.web.content.ContextLoaderListener
    IETF和W3C的区别
    [ppurl]从”皮皮书屋”下载电子书的姿势
    javaEE面试重点
    HDU-1095-A+B for Input-Output Practice (VII)(多一个空格?)
    WinCC7.3 Win764位系统安装教程
  • 原文地址:https://www.cnblogs.com/myx12345/p/6695048.html
Copyright © 2011-2022 走看看