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. tmux简单使用指南

    一, ubuntu安装tmux sudo apt-get install tmux 二, tmux使用 1)打开shell,输入命令tmux,,shell下边会出现绿色状态条 2)然后同时按下ctrl ...

  2. SpringMVC请求处理流程

    从web.xml中 servlet的配置开始, 根据servlet拦截的url-parttern,来进行请求转发   Spring MVC工作流程图   图一   图二    Spring工作流程描述 ...

  3. mybatis处理集合、循环、数组和in查询等语句的使用

    在Mybatis的xml配置中使用集合,主要是用到了foreach动态语句. foreach的参数: foreach元素的属性主要有 item,index,collection,open,separa ...

  4. Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. 如何提高cocos2d-x-spine骨骼动画加载速度

    下面分2点来说: 1. 时间消耗点:io和现场解析 解决方案:加载过的骨骼动画就不要每次重新加载,不要每次都去加载json文件和atlas,我推荐使用 static CCSkeletonAnimati ...

  6. codeforces B.Maximum Absurdity 解题报告

    题目链接:http://codeforces.com/contest/332/problem/B 题意:在一个序列中,在所有长度为k的区间里找出两个不重叠的最大和,输出这两个最大和所对应的开头的位置a ...

  7. Eclipse中android工程C++文件中出现的莫名其妙的错误

    大多数是std库相关的问题,例如 vector<int> v; v.push_back(23);//这句语法是没有错误的,但是每次执行Run As的时候就会报错 尝试1:在工程名右键-Cl ...

  8. [MAC ] Mac-OSX下安装Git

    转载自 : http://www.cnblogs.com/shanyou/archive/2011/01/30/1948088.html Mac-OSX下安装Git是一件很简单的事,我们可以下载一个安 ...

  9. min-height在安卓下不起作用

    正常情况下,min-height可以当height使用 如: <div class="father"> <div class="child"& ...

  10. 一、HTML和CSS基础--网页布局--网页简单布局之结构与表现原则

    结构.表现和行为分离,不仅是一项技术,更主要的是一种思想,当我们拿到一个网页时,先考虑设计图中的文字内容和内容模块之间的关系,重点放在编写html结构和语义化,然后考虑布局和表现形式.,减少HTML与 ...