【BZOJ】1014 [JSOI2008]火星人prefix
【算法】splay
【题解】对于每个结点维护其子树串的hash值,前面为高位,后面为低位。
sum[x]=sum[L]*base[s[R]+1]+A[x]*base[s[R]]+sum[R],其中sum为哈希,base为乘权,A为数值(即字符)。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=,inf=0x3f3f3f3f,bases=;
int f[maxn],t[maxn][],s[maxn],A[maxn],a[maxn],sz=,root,n;
unsigned long long sum[maxn],a_,b_,b[maxn];
int Node(int fa,int num)
{
sz++;
f[sz]=fa;t[sz][]=t[sz][]=;
s[sz]=;A[sz]=sum[sz]=num;
return sz;
}
void count(int x)
{
s[x]=s[t[x][]]++s[t[x][]];
sum[x]=sum[t[x][]]*b[s[t[x][]]+]+1ull*A[x]*b[s[t[x][]]]+sum[t[x][]];
}
void build(int fa,int &x,int l,int r)
{
if(l>r)return;
int mid=(l+r)>>;
x=Node(fa,a[mid]);
build(x,t[x][],l,mid-);
build(x,t[x][],mid+,r);
count(x);
}
void rotate(int x)
{
int k=x==t[f[x]][];
int y=f[x];
t[y][k]=t[x][!k];f[t[x][!k]]=y;
if(f[y])t[f[y]][y==t[f[y]][]]=x;f[x]=f[y];f[y]=x;
t[x][!k]=y;
sum[x]=sum[y];s[x]=s[y];
count(y);
}
void splay(int x,int r)
{
for(int fa=f[r];f[x]!=fa;)
{
if(f[f[x]]==fa){rotate(x);return;}
int X=x==t[x][],Y=f[x]==t[f[f[x]]][];
if(X^Y)rotate(x),rotate(x);
else rotate(f[x]),rotate(x);
}
}
void find(int &x,int k)
{
for(int i=x;i;)
{
if(k<=s[t[i][]]){i=t[i][];continue;}
if(k==s[t[i][]]+){splay(i,x);x=i;return;}
k-=s[t[i][]]+;i=t[i][];
}
}
bool work(int x,int y,int longs)
{
if(x+longs->n||y+longs->n)return ;
find(root,x);find(t[root][],longs+);
a_=sum[t[t[root][]][]];
find(root,y);find(t[root][],longs+);
b_=sum[t[t[root][]][]];
if(a_==b_)return ;
return ;
}
void ask()
{
int x,y;
scanf("%d%d",&x,&y);
int l=,r=maxn;
while(l<r)
{
int mid=(l+r)>>;
if(work(x,y,mid))l=mid+;
else r=mid;
}
printf("%d\n",l-);
}
void repair()
{
int x;char c;
scanf("%d %c",&x,&c);
find(root,x+);
A[root]=c-'a'+;
count(root);
}
void insert()
{
n++;
int x;char c;
scanf("%d %c",&x,&c);
find(root,x+);find(t[root][],);
int y=Node(t[root][],c-'a'+);
t[t[root][]][]=y;
f[y]=t[root][];
count(t[root][]);
count(root);
}
char str[maxn];
int main()
{
scanf("%s",str+);
n=strlen(str+);
for(int i=;i<=n;i++)a[i]=str[i]-'a'+;
b[]=;
for(int i=;i<=maxn;i++)b[i]=b[i-]*bases;
root=a[]=a[n+]=;
build(,root,,n+);
int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%s",str);//不要用%c会读空格,用%s读到空格会停。
if(str[]=='Q')ask();
if(str[]=='R')repair();
if(str[]=='I')insert();
}
return ;
}
update:现在已改用fhq-treap代替splay。
【BZOJ】1014 [JSOI2008]火星人prefix的更多相关文章
- 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 火星人 ...
- 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 )
用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: 8112 Solved: 2569[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)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 给定一个字符串,要求提供修改一个字符,插入一个字符,查询两个后缀LCP ...
- bzoj 1014 [JSOI2008]火星人prefix——splay+哈希
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...
随机推荐
- lintcode-161-旋转图像
161-旋转图像 给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像. 样例 给出一个矩形[[1,2],[3,4]],90度顺时针旋转后,返回[[3,1],[4,2]] 挑战 能否在原地完成? 标 ...
- iOS音频播放概述
在iOS系统中apple对音频播放需要的操作进行了封装并提供了不同层次的接口 下面对其中的中高层接口进行功能说明: Audio File Services:读写音频数据,可以完成播放流程中的第2步: ...
- bootstrap控件点击之后没有反应的原因
引用的jquery.js文件要放到bootstrap.js的前面 jquery.js文件版本太低. 这些问题可以通过firebug或者谷歌调试器发现. 问题很简单,简单记录下,以免以后遗忘.
- flash builder 4.6在debug调试时需要系统安装flashplayer debug版本
http://blog.csdn.net/cupid0051/article/details/46684295
- 【week5】psp
本周psp psp饼图: 随笔字数折线图: 代码行折线图:
- 使用gdb查看栈帧的情况,有ebp
0x7fffffffdb30: 0x00000000 0x00000000 0xf7ffe700 0x0000001a0x7fffffffdb40: 0xffffdc98 ...
- matlab函数列表(A~Z)【转】
A a abs 绝对值.模.字符的ASCII码值acos 反余弦acosh 反双曲余弦acot 反余切acoth 反双曲余切acsc 反余割acsch 反双曲余割align 启动图形对象几何位置排列工 ...
- 【bzoj2259】[Oibh]新型计算机 堆优化Dijkstra
题目描述 Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计算机的输入问题.新型计算机的输入也很独特,假设输入序列中有 ...
- linux文件服务器:samba服务器
windows上,需要和linux虚拟机进行方便的文件交互,总结一下遇到的问题. 1.samba简介 windows和windows之间共享文件可以用“网上邻居”,linux和linux间共享文件用 ...
- [洛谷P4777]【模板】扩展中国剩余定理(EXCRT)
题目大意:给你一些关于$x$的方程组:$$\begin{cases}x\equiv a_1\pmod{mod_1}\\x\equiv a_2\pmod{mod_2}\\\vdots\\x\equiv ...