BZOJ.1014.[JSOI2008]火星人(Splay 二分 Hash)
后缀数组显然不行啊。求LCP还可以哈希+二分,于是考虑用平衡树维护哈希值。
\]
好像跑的很慢。。
//4800kb 5492ms
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define lson son[x][0]
#define rson son[x][1]
#define gc() getchar()
typedef unsigned long long ull;
const int N=110000;
const ull base=31;
int root,size,len,sz[N],fa[N],son[N][2],s[N];
char tmp[N];
ull hs[N],pw[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline char Get_opt()
{
char c=gc();
while(c!='Q'&&c!='R'&&c!='I') c=gc();
return c;
}
inline char Get_char()
{
char c=gc();
while(!isalpha(c)) c=gc();
return c;
}
inline void Update(int x)
{
int rs=rson;
sz[x]=sz[lson]+sz[rs]+1,
hs[x]=hs[lson]*pw[sz[rs]+1]+s[x]*pw[sz[rs]]+hs[rs];
}
void Build(int l,int r,int f)
{
if(l>r) return;
int m=l+r>>1;
son[f][m>f]=m, fa[m]=f, s[m]=tmp[m]-'a'+1;
if(l==r) sz[l]=1, hs[l]=s[m];
else Build(l,m-1,m), Build(m+1,r,m), Update(m);
}
void Rotate(int x,int &k)
{
int a=fa[x],b=fa[a],l=son[a][1]==x,r=l^1;
if(a==k) k=x;
else son[b][son[b][1]==a]=x;
fa[a]=x, fa[x]=b, fa[son[x][r]]=a, son[a][l]=son[x][r], son[x][r]=a;
Update(a);
}
void Splay(int x,int &k)
{
while(x!=k)
{
int a=fa[x];
if(a!=k) son[a][1]==x^son[fa[a]][1]==a?Rotate(x,k):Rotate(a,k);
Rotate(x,k);
}
Update(x);
}
int Kth(int k,int x)
{
while(1)
{
if(sz[lson]+1==k) return x;
if(sz[lson]<k) k-=sz[lson]+1, x=rson;
else x=lson;
}
}
inline int Split(int l,int r)//Get(l,r)
{
int x=Kth(l,root),y=Kth(r,root);
Splay(x,root), Splay(y,son[x][1]);
return son[y][0];
}
inline bool Check(int x,int y,int len)
{
ull v1=hs[Split(x,x+len+1)], v2=hs[Split(y,y+len+1)];
return v1==v2;
}
void Query(int x,int y)
{
int l=1,r=std::min(size-x-1,size-y-1),mid;
while(l<=r){
if(Check(x,y,mid=l+r>>1)) l=mid+1;
else r=mid-1;
}
printf("%d\n",r);
}
void Modify(int p,char c)
{
int x=Kth(p,root);
Splay(x,root), s[x]=c-'a'+1, Update(x);
}
void Insert(int p,char c)
{
// Split(p,p+1);//不能只用Split()!这的p是下标,不一定是编号!
int p1=Kth(p,root),p2=Kth(p+1,root);
Splay(p1,root), Splay(p2,son[root][1]);
int x=++size;
sz[x]=1, hs[x]=s[x]=c-'a'+1, fa[x]=p2, son[p2][0]=x;
Update(p2), Update(p1);
}
int main()
{
pw[0]=1;
for(int i=1; i<N; ++i) pw[i]=pw[i-1]*31;
scanf("%s",tmp+2), len=strlen(tmp+2);
size=len+2, root=len+3>>1, Build(1,size,0);
int m=read(),x,y; char opt;
while(m--)
switch(opt=Get_opt(),opt)
{
case 'Q':x=read(),y=read(),Query(x,y); break;
case 'R':x=read(),Modify(x+1,Get_char()); break;
case 'I':x=read(),Insert(x+1,Get_char()); break;
}
return 0;
}
BZOJ.1014.[JSOI2008]火星人(Splay 二分 Hash)的更多相关文章
- 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 + 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+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3154 Solved: 948[Submit][ ...
- 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4164 Solved: 1277[Submit] ...
- [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】
题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...
- BZOJ 1014: [JSOI2008]火星人prefix
Sol Splay+Hash+二分答案. 用Splay维护Hash,二分答案判断. 复杂度 \(O(nlog^2n)\) PS:这题调了两个晚上因为没开long long.许久不写数据结构题感觉写完整 ...
- BZOJ 1014 [JSOI2008]火星人prefix (splay+二分答案+字符串hash)
题目大意:维护一个字符串,支持插入字符和替换字符的操作,以及查询该字符串两个后缀的最长公共前缀长度 乍一看以为是后缀数组,然而并没有可持久化后缀数组(雾) 看题解才知道这是一道splay题,首先要对s ...
随机推荐
- python---django使用数据库(orm)
官方教程点击此处 项目默认使用sqlite DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os. ...
- 情人节网站logo赏析
一年一度的情人节,不少网站都进行了不错的装点,我们不妨来简单浏览一下,借以触发灵感. 百度 百度的logo放上了改变,变成了一个gif,图片如下. 腾讯 淘宝 淘宝的logo同样换成了一个gif 谷歌 ...
- 对 JavaScript 下 namespace 功能的简单分析
前些天在剥离 百度随心听 的播放器引擎时,看到了一个namespace方法,觉得新奇,当然只是对于我自己而言,我入门js不久,经验尚浅.之前看到网易还是新浪还是什么什么网站来着,也是用类似这种东西的, ...
- iOS 在viewDidLayoutSubviews自动布局crash问题
1 viewDidLayoutSubviews改成viewWillLayoutSubviews在iOS7上就不会crash了2 viewDidLoad中还需要设置self.edgesForExtend ...
- codeforces 235 div2 B. Sereja and Contests
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good ...
- Java 学习札记(一)JDK安装配置
Windows上配置JDK 1.下载windows版JDK 网址:http://www.oracle.com/technetwork/java/javase/archive-139210.html 2 ...
- 第八集:魔法阵 NTT求循环卷积
题目来源:http://www.fjutacm.com/Problem.jsp?pid=3283 题意:给两串长度为n的数组a和b,视为环,a和b可以在任意位置开始互相匹配得到这个函数的值,求这个函数 ...
- 【linux kernel】 中断处理-中断上半部【转】
转自:http://www.cnblogs.com/embedded-tzp/p/4451354.html 欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地 ...
- Linux 串口、usb转串口驱动分析(2-2) 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4186852 Linux 串口.usb转 ...
- 初始ASP.NET数据控件【续 ListView】
ListView控件 ListView控件可以用来显示数据,它还提供编辑,删除,插入,分页与排序等功能.ListView是GridView与DataList的融合体,它具有GridView控件编辑 ...