类似于1014,用splay维护这个序列,维护每个节点为根的子树的hash值,对于一个询问二分答案判断就行了。

  反思:询问的时候因为是原序列的x,y,所以开始的时候直接splay(x-1)了,后来发现这是不对的,因为可能在x前插入一些东西,所以需要麻烦些,先splay(x),然后提出来右端点为size[son[rot][0]]+1+len,然后再splay(find(size[son[rot][0]]+1))。

  1. /**************************************************************
  2.     Problem: 2258
  3.     User: BLADEVIL
  4.     Language: C++
  5.     Result: Accepted
  6.     Time:7904 ms
  7.     Memory:3640 kb
  8. ****************************************************************/
  9.  
  10. //By BLADEVIL
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <algorithm>
  14. #define maxn 100010
  15.  
  16. using namespace std;
  17.  
  18. char s[maxn];
  19. int fac[maxn],key[maxn],num,rot,son[maxn][],father[maxn],size[maxn],hash[maxn];
  20.  
  21. void update(int x) {
  22.     if (!x) return ;
  23.     hash[x]=hash[son[x][]]+(key[x]+hash[son[x][]]*)*fac[size[son[x][]]];
  24.     size[x]=size[son[x][]]+size[son[x][]]+;
  25. }
  26.  
  27. int build(int l,int r) {
  28.     int mid=l+r>>,left=,right=;
  29.     if (mid<r) right=build(mid+,r);
  30.     if (mid>l) left=build(l,mid-);
  31.     father[left]=father[right]=mid;
  32.     son[mid][]=left; son[mid][]=right;
  33.     update(mid);
  34.     return mid;
  35. }
  36.  
  37. void rotate(int x,int &rot) {
  38.     int y=father[x],z=father[y];
  39.     int p=(son[y][]==x),q=p^;
  40.     if (y==rot) rot=x; else if (son[z][]==y) son[z][]=x; else son[z][]=x;
  41.     father[x]=z; father[y]=x; father[son[x][q]]=y;
  42.     son[y][p]=son[x][q]; son[x][q]=y;
  43.     update(y);
  44. }
  45.  
  46. void splay(int x,int &rot) {
  47.     while (x!=rot) {
  48.         int y=father[x],z=father[y];
  49.         if (y!=rot)
  50.             if ((son[y][]==x)^(son[z][]==y)) rotate(x,rot); else rotate(y,rot);
  51.         rotate(x,rot);
  52.     }
  53.     update(x);
  54. }
  55.  
  56. int find(int x) {
  57.     int t=rot;
  58.     while () {
  59.         if (size[son[t][]]+==x) return t; else
  60.         if (size[son[t][]]+>x) t=son[t][]; else
  61.         if (size[son[t][]]+<x) x-=size[son[t][]]+,t=son[t][];
  62.     }
  63. }
  64.  
  65. bool judge(int x,int y,int len) {
  66.     if (len==) return key[x+]==key[y+];
  67.     int p=x+; splay(p,rot);
  68.     int q=find(size[son[rot][]]++len);
  69.     splay(find(size[son[rot][]]),rot); splay(q,son[rot][]);
  70.     int a1=hash[son[q][]];
  71.     p=y+; splay(p,rot);
  72.     q=find(size[son[rot][]]++len);
  73.     splay(find(size[son[rot][]]),rot); splay(q,son[rot][]);
  74.     int a2=hash[son[q][]];
  75.     return a1==a2;
  76. }
  77.  
  78. int main() {
  79.     scanf("%s",s); num=strlen(s);
  80.     fac[]=; for (int i=;i<maxn;i++) fac[i]=fac[i-]*;
  81.     for (int i=;i<=num+;i++) key[i]=s[i-]-'a'+; num+=;
  82.     rot=build(,num);
  83.     int task; scanf("%d",&task);
  84.     while (task--) {
  85.         int x,y;
  86.         scanf("%s",s);
  87.         if (s[]=='Q') {
  88.             scanf("%d%d",&x,&y);
  89.             if (x>y) swap(x,y);
  90.             splay(y+,rot);
  91.             int l=,r=size[son[rot][]],mid,ans=;
  92.             while (l<=r) {
  93.                 mid=l+r>>;
  94.                 if (judge(x,y,mid)) ans=mid, l=mid+; else r=mid-;
  95.             }
  96.             printf("%d\n",ans);
  97.         } else
  98.         if (s[]=='I') {
  99.             scanf("%s%d",s,&x);
  100.             x=(x>num-)?num:x;
  101.             key[++num]=s[]-'a'+;
  102.             int p=find(x); splay(p,rot);
  103.             int q=find(x+); splay(q,son[rot][]);
  104.             father[num]=q; son[q][]=num;
  105.             splay(num,rot);
  106.         }
  107.     }
  108.     return ;  
  109. }

bzoj 2258 splay的更多相关文章

  1. bzoj 1269 bzoj 1507 Splay处理文本信息

    bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...

  2. bzoj 3506 && bzoj 1552 splay

    查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...

  3. bzoj 1014 splay维护hash值

    被后缀三人组虐了一下午,写道水题愉悦身心. 题很裸,求lcq时二分下答案就行了,写的不优美会被卡时. (写题时精神恍惚,不知不觉写了快两百行...竟然调都没调就A了...我还是继续看后缀自动机吧... ...

  4. bzoj 1503 splay

    因为是整体加减,所以直接记录在外面. #include<iostream> #include<cstdio> #include<cstring> #include& ...

  5. bzoj 3224 splay模板题4

    再刷水题我就废了... #include<iostream> #include<cstdio> #include<algorithm> #include<cs ...

  6. bzoj 3223 splay模板题3

    水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...

  7. bzoj 1208 splay模板题2

    自己yy了找前驱和后继,学了学怎么删除...(反正就是练模板) #include<iostream> #include<cstdio> #include<cstring& ...

  8. bzoj 1588 splay模板题

    用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度... #include<iostream> #include<cstdio> ...

  9. BZOJ 2733 & splay的合并

    题意: 带权联通块,添边与查询联通块中第k大. SOL: splay合并+并查集. 我以为splay可以用奇技淫巧来简单合并...调了一下午终于幡然醒悟...于是就只好一个一个慢慢插...什么启发式合 ...

随机推荐

  1. Jenkins系列-Jenkins通过Publish over SSH插件实现远程部署

    配置ssh免秘钥登录 安装Publish over SSH插件 插件使用官网:https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plug ...

  2. python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

    #vi /etc/ld.so.conf.d/python2.7.conf   加入/usr/local/python27/lib   保存退出后执行 #ldconfig

  3. Binding自动侦听

    WPF的强大之一就是数据绑定,Binding是数据桥梁,它的两端是分别是源(Source)和目标(Target),一个简单的类的属性值发生变化,会自动反映在UI界面上,这个属性就是Binding的Pa ...

  4. RT-thread 设备驱动组件之SPI设备

    本文主要介绍RT-thread中的SPI设备驱动,涉及到的文件主要有:驱动框架文件(spi_dev.c,spi_core.c,spi.h),底层硬件驱动文件(spi_hard.c,spi_hard.h ...

  5. 路由分发原则 get最终传递给get post最终传递给post

  6. BZOJ4000 TJOI2015棋盘(状压dp+矩阵快速幂)

    显然每一行棋子的某种放法是否合法只与上一行有关,状压起来即可.然后n稍微有点大,矩阵快速幂即可. #include<iostream> #include<cstdio> #in ...

  7. Android 4.0源码结构

    Android 4.0 |-- Makefile |-- bionic (bionic C库) |-- bootable (启动引导相关代码) |-- build (存放系统编译规则及generic等 ...

  8. 转:浅谈深度学习(Deep Learning)的基本思想和方法

    浅谈深度学习(Deep Learning)的基本思想和方法  参考:http://blog.csdn.net/xianlingmao/article/details/8478562 深度学习(Deep ...

  9. [bzoj4391] [Usaco2015 dec]High Card Low Card 贪心 线段树

    ---题面--- 题解: 观察到以决策点为分界线,以点数大的赢为比较方式的游戏都是它的前缀,反之以点数小的赢为比较方式的都是它的后缀,也就是答案是由两段答案拼凑起来的. 如果不考虑判断胜负的条件的变化 ...

  10. POJ2728:Desert King——题解

    http://poj.org/problem?id=2728 题目大意:求一棵生成树使得路费用和/路长之和最小(路的费用是两端点的高度差) 最小比率生成树. 我们还是01分数规划的思想将边权变为路费用 ...