http://www.lydsy.com/JudgeOnline/problem.php?id=1014

题意:支持插入一个字符、修改一个字符,查询lcp。(总长度<=100000, 操作<=150000)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=100005;
const ll M=9875321;
int P[N];
struct node *null;
struct node {
node *f, *c[2];
int k, s, h;
node(int _k=0) { k=_k; s=1; h=_k; f=c[0]=c[1]=null; }
void setc(node *x, bool d) { c[d]=x; x->f=this; }
bool d() { return f->c[1]==this; }
void pushup() {
s=c[0]->s+c[1]->s+1;
h=(c[0]->h+((ll)k*P[c[0]->s])%M+((ll)c[1]->h*P[c[0]->s+1])%M)%M;
}
}*root;
void rot(node *x) {
node *f=x->f; bool d=x->d();
f->f->setc(x, f->d());
f->setc(x->c[!d], d);
x->setc(f, !d);
f->pushup();
if(f==root) root=x;
}
void splay(node *x, node *f=null) {
while(x->f!=f)
if(x->f->f==f) rot(x);
else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
x->pushup();
}
node *sel(node *x, int k) {
int s=x->c[0]->s;
if(s==k) return x;
if(s<k) return sel(x->c[1], k-s-1); return sel(x->c[0], k);
}
node *getrange(int l, int r) {
splay(sel(root, l-1));
splay(sel(root, r+1), root);
return root->c[1];
}
void ins(int k, int pos) {
node *f=getrange(pos+1, pos);
f->setc(new node(k), 0); splay(f->c[0]);
}
void fix(int k, int pos) {
node *f=getrange(pos, pos);
f->c[0]->k=k; f->c[0]->pushup();
splay(f->c[0]);
}
int gethash(int l, int r) { return getrange(l, r)->c[0]->h; }
int ask(int x, int y) {
int s=root->s-2;
int l=1, r=min(s-x, s-y)+1, mid;
while(l<=r) {
mid=(l+r)>>1;
if(gethash(x, x+mid-1)==gethash(y, y+mid-1)) l=mid+1;
else r=mid-1;
}
return l-1;
}
void U(node *x) { if(x==null) return; U(x->c[0]); U(x->c[1]); x->pushup(); }
void Pr(node *x) { if(x==null) return; Pr(x->c[0]); printf("%c", x->k+'a'); Pr(x->c[1]); }
void D(node *x=root) { Pr(x); puts(""); U(x); Pr(x); } char s[N];
int n;
void build(int l, int r, node *&x) {
if(l>r) return;
int mid=(l+r)>>1;
x=new node(s[mid]-'a');
if(l==r) return;
build(l, mid-1, x->c[0]);
build(mid+1, r, x->c[1]);
if(l<=mid-1) x->c[0]->f=x;
if(mid+1<=r) x->c[1]->f=x;
x->pushup();
}
void init() {
P[0]=1;
for1(i, 1, 100000) P[i]=(P[i-1]*26)%M;
null=new node; null->s=0; null->f=null->c[0]=null->c[1]=null;
root=new node; root->setc(new node, 1);
node *x;
build(1, n, x);
root->c[1]->setc(x, 0); root->c[1]->pushup(); root->pushup();
//D();
}
char rdchar() {
char ret=getchar();
while(ret<'a'||ret>'z') ret=getchar();
return ret;
}
int main() {
scanf("%s", s+1); n=strlen(s+1);
init();
int m=getint();
while(m--) {
char c=getchar(); while(c!='Q'&&c!='R'&&c!='I') c=getchar();
if(c=='Q') { int x=getint(), y=getint(); printf("%d\n", ask(x, y)); }
else if(c=='R') { int x=getint(); fix(rdchar()-'a', x); }
else if(c=='I') { int x=getint(); ins(rdchar()-'a', x); }
}
return 0;
} 

被sb错调哭了QAQ。。。insert那里。。插入到第x个后边。。。我。。。。。。写成了第x个前面。。。。。。。。。。还调了!好!久!

QAQ

本题神lcp做法。。。。表示只会sa的height的离线。。。。。。。这种在线的我就QAQ做个忧伤的表情。。。

然后膜拜了题解。。。。好神。。splay维护区间。。。hash+二分维护lcp。。。。QAQ似乎是白书上说的么。。。

但是这种有概率的题这样搞真的好么。。。。。

因此取模那里直接抄人家的。。。因为不保证自己取的mod正确QAQ。。。。

然后就是在区间里乘上hash的权,因为乘法分配率,所以是可以区间维护的。

。。

【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)的更多相关文章

  1. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  2. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

  3. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  4. BZOJ 1014: [JSOI2008]火星人prefix Splay+二分

    1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...

  5. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

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

  6. BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)

    题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...

  7. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

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

  8. bzoj 1014 [JSOI2008]火星人prefix——splay+哈希

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...

  9. BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值

    题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...

  10. bzoj 1014: [JSOI2008]火星人prefix hash && splay

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3154  Solved: 948[Submit][ ...

随机推荐

  1. 我常用的delphi 第三方控件

    转载:http://www.cnblogs.com/xalion/archive/2012/01/09/2317246.html 有网友问我常用的控件及功能.我先大概整理一下,以后会在文章里面碰到时再 ...

  2. linux下cp目录时排除一个或者多个目录的方法

    说明:/home目录里面有data目录,data目录里面有a.b.c.d.e五个目录,现在要把data目录里面除过e目录之外的所有目录拷贝到/bak目录中 系统运维 www.osyunwei.com ...

  3. jQuery mobile 开发问题记录

    一.动态加载页面问题 1.存在这样一个页面布局: main.html 为主界面A,B为该页面中的三个page,其中A为splitview左部分页面,B为右半部页面 a1.html 为一个独立的页面 a ...

  4. Ubuntu系统如何查看硬件配置信息

    查看ubuntu硬件信息 1, 主板信息 .查看主板的序列号 -------------------------------------------------- #使用命令 dmidecode | ...

  5. iOS UIDatePicker frame改变问题

    这种方法不行: pickerCtl = UIDatePicker(frame:pickerFrame) 但是这种却行 pickerCtl = UIDatePicker() pickerCtl!.fra ...

  6. ubuntu命令行相关命令使用心得

    一.Ubuntu解压缩zip,tar,tar.gz,tar.bz2 ZIP zip可能是目前使用得最多的文档压缩格式.它最大的优点就是在不同的操作系统平台,比如Linux, Windows以及Mac ...

  7. 【python】Python标准库defaultdict模块

    来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...

  8. Android配置----DDMS 连接真机(己ROOT),用file explore看不到data/data文件夹的解决办法

    Android DDMS 连接真机(己ROOT),用file explore看不到data/data文件夹,问题在于data文件夹没有权限,用360手机助手或豌豆荚也是看不见的. 有以下两种解决方法: ...

  9. KMP模式匹配练习题

    使用KMP算法在文本串S中找模式串P是一种常见的方法.假设S=P={xyxyyxxyx},亦即将S对自己进行匹配,匹配过程中正确的next数组是____. 1.首先求最大相同前缀后缀长度 模式串的各个 ...

  10. Linux下可以替代windows的软件汇总:(不断完善中)

    http://www.ubuntukylin.com/ukylin/forum.php?mod=viewthread&tid=9530 原则:不求全面,只求实用.主要针对桌面级应用.网购:  ...