【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维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...
随机推荐
- HttpServletRequest和HttpServletResponse实例
先看一下web.xml文件配置: <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- 《学习OpenCV》课后习题解答3
题目:(P104) 创建一个大小为100*100的三通道RGB图像.将它的元素全部置0.使用指针算法以(20,5)与(40,20)为项点绘制一个绿色平面. 解答: #include "cv. ...
- 【week2】 词频统计效能分析
效能统计工具:Jprofiler License Key:L-Larry_Lau@163.com#23874-hrwpdp1sh1wrn#0620 该性能分析工具对服务器进行监听,图一是线程变化图,当 ...
- Python的压缩文件处理 zipfile & tarfile
本文从以下两个方面, 阐述Python的压缩文件处理方式: 一. zipfile 二. tarfile 一. zipfile 虽然叫zipfile,但是除了zip之外,rar,war,jar这些压缩( ...
- 微信支付java
直接上代码: 1.支付配置PayCommonUtil import com.legendshop.payment.tenpay.util.MD5Util; import com.legendshop. ...
- delphi鼠标状态
Screen.Cursor := crNo;
- hdu 1142 A Walk Through the Forest (最短路径)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- [NOI2001]炮兵阵地 状压DP
题面: 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最多 ...
- 在Windows*上编译Tensorflow教程
背景介绍 最简单的 Tensorflow 的安装方法是在 pip 一键式安装官方预编译好的包 pip install tensorflow 通常这种预编译的包的编译参数选择是为了最大兼容性而不是为了最 ...
- CF17E:Palisection——题解
https://vjudge.net/problem/CodeForces-17E http://codeforces.com/problemset/problem/17/E 题目大意:给一个长度为n ...