...强迫症终于A了这道题  bzoj前30道全A指日可待

splay维护这个结点控制的字符串的hash值

每次旋转重新算一遍就可以了

查询的时候跑一个二分

讲起来很简单但是还是调了1h才调对了splay

把main写完饺子都凉了

没写过几次平衡树 常数写的奇大 能递归的我都递归了。。。

字符串哈希因为过于Naive只会用自然溢出。。。

但是1A了

家里的电脑有写好的read和write

我竟然忘了。。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#define ll unsigned long long
#define pi 3.14
#define eps 1e-9
#define inf 2147483233
#define m(a) memset(a,0,sizeof(a))
#define M(a) memset(a,127,sizeof(a))
#define REP(i,m,n) for(int i=1;i<=n;i++)
#define DWN(i,n,m) for(int i=n;i>=1;i++)
#define lowbit(x) x&(-x)
using namespace std;
int n;
inline int read()
{
int x=,f=;
char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=*x+ch-'';ch=getchar();}
return x*f;
}
inline void write(int x)
{
int num=;
char buf[];
while(x)buf[++num]=(x%)+'',x/=;
while(num)putchar(buf[num--]);
putchar('\n');
}
ll powe[];
void makepow()
{
int i;
for(powe[]=,i=;i<;i++)powe[i]=powe[i-]*;
}
const int maxn=;
char str[maxn];
struct SplayTree
{
int rt,Size;
int son[maxn][],f[maxn],size[maxn];
ll val[maxn],hsh[maxn];
char s[maxn];
inline void pushup(int x){size[x]=size[son[x][]]+size[son[x][]]+;hsh[x]=hsh[son[x][]]+powe[size[son[x][]]]*s[x]+powe[size[son[x][]]+]*hsh[son[x][]];}
inline void Rotate(int x,int type)
{
int y=f[x];
son[y][!type]=son[x][type];
f[son[x][type]]=y;
f[x]=f[y];
if(f[x])son[f[y]][son[f[y]][]==y]=x;
son[x][type]=y;
f[y]=x;
size[x]=size[y],hsh[x]=hsh[y];
pushup(y);
}
inline void splay(int x,int goal)
{
while(f[x]!=goal)
{
if(f[f[x]]==goal)
{
if(son[f[x]][]==x)Rotate(x,);
else Rotate(x,);
}
else
{
int y=f[x],z=f[y];
if(son[z][]==y)
{
if(son[y][]==x)Rotate(y,),Rotate(x,);
else Rotate(x,),Rotate(x,);
}
else
{
if(son[y][]==x)Rotate(y,),Rotate(x,);
else Rotate(x,),Rotate(x,);
}
}
}
if(goal==) rt=x;
}
inline int rank(int x,int k)
{
if(k<=size[son[x][]]) return rank(son[x][],k);
else if(k==size[son[x][]]+) return x;
else return rank(son[x][],k-size[son[x][]]-);
}
inline int build(int l,int r,int id)
{
if(l>r)return ;
int x=++Size,mid=(l+r)>>;
f[x]=id;
s[x]=str[mid];
son[x][]=build(l,mid-,x);
son[x][]=build(mid+,r,x);
pushup(x);
return x;
}
inline void insert(int k,char val)
{
int x=rank(rt,k),y=rank(rt,k+);
splay(x,),splay(y,x);
s[++Size]=val;
f[Size]=y,son[y][]=Size;
pushup(Size);
pushup(y);
pushup(x);
}
inline void change(int k,int val)
{
int x=rank(rt,k);
splay(x,);
s[x]=val;
pushup(x);
}
inline int bisearch(int kx,int ky)
{
int l=,r=n,mid;
while(l<=r)
{
mid=l+r>>;
if(ky+mid>n+)
{
r=mid-;
continue;
}
int x=rank(rt,kx-),y=rank(rt,kx+mid);
splay(x,),splay(y,x);
ll temp=hsh[son[y][]];
x=rank(rt,ky-),y=rank(rt,ky+mid);
splay(x,),splay(y,x);
if(temp==hsh[son[y][]])l=mid+;
else r=mid-;
}
return r;
}
}splay;
int main()
{
int m,i,j,k,x,y;
char op[],val[];
scanf("%s%d",str+,&m);
makepow();
n=strlen(str+);
splay.rt=splay.build(,n+,);
for(i=;i<=m;i++)
{
scanf("%s",op);
if(op[]=='I')
{
scanf("%d%s",&x,val);
splay.insert(x+,val[]);
n++;
}
else if(op[]=='R')
{
scanf("%d%s",&x,val);
splay.change(x+,val[]);
}
else
{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
if(x!=y)
printf("%d\n",splay.bisearch(x+,y+));
else
printf("%d\n",n-x+);
}
}
return ;
}

bzoj1014火星人的更多相关文章

  1. 无旋Treap - BZOJ1014火星人 & 可持久化版文艺平衡树

    !前置技能&概念! 二叉搜索树 一棵二叉树,对于任意子树,满足左子树中的任意节点对应元素小于根的对应元素,右子树中的任意节点对应元素大于根对应元素.换言之,就是满足中序遍历为依次访问节点对应元 ...

  2. BZOJ1014火星人prefix Splay維護序列 + 字符串哈希

    @[Splay, 哈希] Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:\(madamimadam\), 我们将这个字符串的各个字符予以标号 ...

  3. BZOJ1014 火星人的prefix

    火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字 ...

  4. bzoj1014 火星人 (hash+splay+二分答案)

    求公共前缀的问题可以用hash+二分来解决,但这个是动态的,所以我们用平衡树来维护区间的hash值 复杂度$O(mlog^2n)$ #include<bits/stdc++.h> #def ...

  5. bzoj1000~1025

    以后还是这样 25道题一起发 看着爽 noip失利之后发粪涂墙 刷了一波bzoj 题解: bzoj1000 A+B问题 这题不同的人有不同的写法,我写了个线段树套Treap,应该还是挺简单的 但是看别 ...

  6. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

  7. 【BZOJ1014】火星人(Splay,哈希)

    [BZOJ1014]火星人(Splay,哈希) 题面 BZOJ 题解 要动态维护这个串,一脸的平衡树. 那么用\(Splay\)维护这个哈希值就好了. 每次计算答案的时候二分+Splay计算区间哈希值 ...

  8. BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*

    BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...

  9. 【bzoj1014】: [JSOI2008]火星人prefix 平衡树-字符串-hash-二分

    [bzoj1014]: [JSOI2008]火星人 用平衡树维护字符串的hash 然后询问的时候二分一下就好了 /* http://www.cnblogs.com/karl07/ */ #includ ...

随机推荐

  1. 【BZOJ】2186 沙拉公主的困惑

    一道很有价值的题. [解析1]欧几里德算法求乘法逆元,前缀和 [Analysis]O(T n log n). [Sum] ①int运算.假设会超出界,第一个数前要加上(LL)即类型转换. ②gcd不变 ...

  2. 【php】global的使用与php的全局变量

    php的全局变量和其余编程语言是不同的,在大多数的编程语言中,全局变量在其下的函数.类中自己主动生效.除非被局部变量覆盖,或者根本就不同意再声明同样名称与类型的局部变量.可是php中的全局变量不是默认 ...

  3. Latex中參考文献排序

    \bibliographystyle{unsrt}:依照引用的先后排序 \bibliographystyle{plain}:按字母的顺序排列,比較次序为作者.年度和标题.当中作者中姓氏字母优先. 关于 ...

  4. 弹窗:popwindow 4部分

    弹窗:popwindow 四部分 ①windows.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  5. 函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积

    http://graphics.stanford.edu/courses/cs178/applets/convolution.html Convolution is an operation on t ...

  6. 第一个Spring Boot程序启动报错了

    创建完成第一个Spring Boot项目后,准备运行,尝一下胜利的果实. 启动日志如下 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ...

  7. JVM性能优化, Part 3 垃圾回收

    ImportNew注:本文是JVM性能优化 系列-第3篇-<JVM性能优化, Part 3 —— 垃圾回收> 第一篇 <JVM性能优化, Part 1 ―― JVM简介 > 第 ...

  8. 坑爹的Hibernate 映射文件错误提示org.xml.sax.SAXParseException

    今天整整一个上午都在和hibernate做斗争,早上一来,继续昨天的项目开发,发现spring项目不能启动,从错误中看是hibernate错误,多半是hibernate配置有错误,关键是错误提示中显示 ...

  9. Java 线程的终止-interrupt

    Java线程的终止——interrupt 取消/关闭的场景 我们知道,通过线程的start方法启动一个线程后,线程开始执行run方法,run方法运行结束后线程退出,那为什么还需要结束一个线程呢?有多种 ...

  10. eclipse revert resources 很慢的解决办法

    eclipse启动无响应,停留在Loading workbench状态,或老是加载不了revert resources 做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应 ...