【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 使用 ionice 限制 Xen 虚拟机磁盘 IO
作为 VPS 服务商我们需要保证每个 VPS 公平的使用 host(服务器)的资源,避免某个 VPS 因为程序死循环.挂起.滥用等因素 “拖累” 其他 VPS,如果出现这个情况如何临时限制这个 VPS ...
- HTML前端--各种小案例
掬一捧清水,放逐在江河,融入流逝的岁月,将心洗净; 捻一缕心香,遥寄在云端,在最深的红尘里重逢,将心揉碎; 望一程山水,徘徊在月下,在相思渡口苦守寒冬,将心落寞. 案例一: 隐藏扩展域,并去掉afte ...
- ZJOI Day 2 游记
---恢复内容开始--- 去ZJOI Day 2打了一会酱油...各种神犇大爷都来屠,南外的小朋友也来屠我们了真是感动...没有看到毛爷爷真是可惜.. Day[-1] 早上还在上课,吃完中饭立马跑去找 ...
- centos rm -rf 恢复删除的文件
Linux有时候执行了 rm -rf 等操作误删了文件绝对是一件可怕的事情,好在有一些解决的办法可以临时救急.这时我们就要用到一款叫做extundelete的工具了. 目录[-] 依赖 安装 查找要恢 ...
- Linux命令之exit - 退出当前shell【返回值状态】
原文链接:http://codingstandards.iteye.com/blog/836625 (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...
- spring3 + mybatis + maven:junit测试错误
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...
- 41.把数组排成最小的数[Sort array to smallest value]
[题目] 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个.例如输入数组{3,32, 321},则输出这两个能排成的最小数字321323.请给出解决问题的算法,并证明该 ...
- ORACLE查询某一字段重复的数据
第一种方法: select a.* from ASSET_MAINTAIN a inner join ASSET_MAINTAIN b on a.asset_id=b.asset_id and a. ...
- ubuntu maven环境安装配置
转载地址:http://my.oschina.net/hongdengyan/blog/150472#OSC_h1_4 一.环境说明: 操作系统:Ubuntu 12.04.2 LTS maven:ap ...
- vector.end() 指向的节点
存储器vector, vector.end() 指向的是最后的结束符,而不是最后一个元素.