【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的权,因为乘法分配率,所以是可以区间维护的。

。。

posted @ 2014-12-10 19:07  iwtwiioi  阅读(426)  评论(3编辑  收藏  举报