【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)
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)的更多相关文章
- BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8112 Solved: 2569[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )
用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分
题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...
- BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)
题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj 1014 [JSOI2008]火星人prefix——splay+哈希
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...
- BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值
题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3154 Solved: 948[Submit][ ...
随机推荐
- 让Linux下的打印机hp1020、hp p1008自动加载固件
前言: 前段时间,处理公司打印机服务器Linux化工作.遇到问题如下:hp1020.hp1008断电后不能继续打印.而其他打印机在连接Linux打印机的情况下,断电后也能正常打印. 鉴于此情况,我搜寻 ...
- PHP网页数据正则采集
$url ="https://********"; $contents = file_get_contents($url); //抓取页面数据 //如果出现中文乱码使用下面代码 / ...
- django动态表格总结
应用场景: A与B之间存在一对多关系. CBV实现方案: CreateView/UpdateView + inlineformset + jquery 具体: view方面:重写post/get方法, ...
- nginx lua处理图片
user apache apache; worker_processes 4; worker_rlimit_nofile 100000; #error_log logs/error.log; #err ...
- 2.设计包含 min 函数的栈[StackWithMinValue]
[题目]: 定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素.要求函数min.push以及pop的时间复杂度都是O(1). [解法一]: 使用一个辅助栈来保存最小元素,其栈顶元素为当前栈 ...
- 30.赋值运算符重载函数[Assign copy constructor]
[问题] 给出如下CMyString的声明,要求为该类型添加赋值运算符函数. C++ Code 1234567891011 class CMyString { public: CMyS ...
- Cocos2d-JS坐标系统
标准屏幕坐标系 如果接触过iOS,Android,Windows Phone等系统的应用开发,或使用DOM,CSS开发过Web网页,开发者会非常熟悉所谓的标准屏幕坐标系:左上角为原点,向右为X轴正方向 ...
- JavaScript设计模式 - 迭代器模式
迭代器模式是指提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示. 迭代器模式可以把迭代的过程从业务逻辑中分离出来,在使用迭代器模式之后,即使不关心对象的内部构造,也可以按顺 ...
- 补丁vs错误(codevs 2218 错误答案)
题目描述 Description 错误就是人们所说的Bug.用户在使用软件时总是希望其错误越少越好,最好是没有错误的.但是推出一个没有错误的软件几乎不可能,所以很多软件公司都在疯狂地发放补丁(有时这种 ...
- ASP.Net核心对象HttpRequest
描述context. Request["username"]; 通过这种方式,能够得到一个HttpRequest对象.HttpRequest对象描述了,关于请求的相关信息,我们可以 ...