luogu P4036 [JSOI2008]火星人
很久以前xzz大佬就喊我做这题,结果现在才做qwq
因为要在序列中插入,所以直接用\(Splay\)维护这个串的哈希值,插入就直接把那个点插♂进去,修改就把点旋到根,然后修改和pushup,询问的话可以考虑二分,check就看两个对应区间哈希值是否相等
#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register
#define ull unsigned long long
using namespace std;
const int N=150000+10;
il int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
int n,q;
char cc[N];
int rt,tt,fa[N],ch[N][2],sz[N];
ull a[N],ha[N],bs=377,bb[N];
il void psup(int x)
{
sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
ha[x]=ha[ch[x][0]]*bb[sz[ch[x][1]]+1]+a[x]*bb[sz[ch[x][1]]]+ha[ch[x][1]];
}
il void rot(int x)
{
int y=fa[x],z=fa[y],yy=ch[y][1]==x,w=ch[x][!yy];
ch[z][ch[z][1]==y]=x;fa[x]=z;
ch[y][yy]=w;fa[w]=y;
ch[x][!yy]=y,fa[y]=x;
psup(y),psup(x);
}
il void spl(int x,int en)
{
if(!x) return;
while(fa[x]!=en)
{
int y=fa[x],z=fa[y];
if(z!=en) ((ch[y][1]==x)^(ch[z][1]==y))?rot(x):rot(y);
rot(x);
}
if(!en) rt=x;
}
il int gkth(int x)
{
if(x<=0||x>n+2) return 0;
int nw=rt;
while(233)
{
if(x<=sz[ch[nw][0]]) nw=ch[nw][0];
else if(x>sz[ch[nw][0]]+1) x-=sz[ch[nw][0]]+1,nw=ch[nw][1];
else return nw;
}
}
il int bui(int l,int r)
{
if(l>r) return 0;
int mid=(l+r)>>1,nw=++tt;
sz[nw]=1,a[nw]=(cc[mid-1]>='a')?(ull)(cc[mid-1]-'a'):0;
fa[ch[nw][0]=bui(l,mid-1)]=nw;
fa[ch[nw][1]=bui(mid+1,r)]=nw;
psup(nw);
return nw;
}
int main()
{
bb[0]=1;
for(int i=1;i<=150000;++i) bb[i]=bb[i-1]*bs;
scanf("%s",cc+1);
n=strlen(cc+1);
rt=bui(1,n+2);
q=rd();
char ss[2];
while(q--)
{
scanf("%s",ss);
if(ss[0]=='Q')
{
int x=rd(),y=rd();
if(x>y) swap(x,y);
int ll=gkth(x),rr=gkth(y),l=1,r=n-y+1,an=0;
while(l<=r)
{
int mid=(l+r)>>1;
ull h1,h2;
int zz=gkth(x+mid+1);//x+mid-1+2
spl(ll,0),spl(zz,ll),h1=ha[ch[zz][0]];
zz=gkth(y+mid+1);
spl(rr,0),spl(zz,rr),h2=ha[ch[zz][0]];
if(h1==h2) an=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",an);
}
else if(ss[0]=='R')
{
int x=gkth(rd()+1);
scanf("%s",ss);
spl(x,0),a[x]=ss[0]-'a',psup(x);
}
else
{
int x=gkth(rd()+1),nw=++tt;
++n;
scanf("%s",ss);
spl(x,0);
a[nw]=ss[0]-'a',fa[nw]=x;
fa[ch[x][1]]=nw,ch[nw][1]=ch[x][1],ch[x][1]=nw;
psup(nw),psup(x);
}
}
return 0;
}
luogu P4036 [JSOI2008]火星人的更多相关文章
- P4036 [JSOI2008]火星人(splay+hash+二分)
P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...
- bzoj 1014: 洛谷 P4036: [JSOI2008]火星人
题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...
- 洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)
题面 洛谷 题解 首先,我们知道求最长公共前缀可以用二分答案+hash来求 因为有修改操作, 考虑将整个字符串的hash值放入splay中 接着就是splay的基本操作了 Code #include& ...
- 1014: [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...
- [BZOJ1014][JSOI2008]火星人prefix
[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- 【bzoj1014】[JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6031 Solved: 1917[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- JSOI2008 火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2918 Solved: 866[Submit][ ...
随机推荐
- JavaScript 数据类型检测总结
JavaScript 数据类型检测总结 原文:https://blog.csdn.net/q3254421/article/details/85483462 在js中,有四种用于检测数据类型的方式,分 ...
- LOJ #2434. 「ZJOI2018」历史(LCT)
题意 click here 题解 我们首先考虑答案是个什么样的东西, 不难 发现每个点可以单独计算它的贡献. 令每个点 \(i\) 崛起次数为 \(a_i\) . 假设一个点子树的 \(\sum a_ ...
- jasperReport和Ireport
<!-- groovy --> <dependency> <groupId>org.codehaus.groovy</groupId> <arti ...
- 自学Linux Shell10.1-使用编辑器vim
点击返回 自学Linux命令行与Shell脚本之路 10.1-使用编辑器vim 所有的 Unix系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在.但是目前我们使用比较多的是 vim 编辑 ...
- 【转】Linux常用命令
日常操作命令 查看当前所在的工作目录的全路径 pwd 12 [root@localhost ~]# pwd/root 查看当前系统的时间 date 12345678 [root@localhost ~ ...
- 修复Mysql主从不同步shell
使用第三方工具MySQL Enterprise Monitor,MySQL企业版监控工具.MONyog – MySQL Monior and Advisor,MONyog大家都不陌生,windows下 ...
- JDK源码分析(1)ArrayList
JDK版本 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomA ...
- (转)IBM mq基本使用
一.下载7.5 Trial版本 http://www.ibm.com/developerworks/downloads/ws/wmq/ 这是下载网址,下载前先必须注册IBM ID,下载完成后一路Nex ...
- UIManager的字体颜色参数对照表
FileChooser.fileTypeHeaderTextTextArea.margincom.sun.java.swing.plaf.windows.WindowsSplitPaneUIcom.s ...
- react与umi
我们知道umi 是一个编译工具,但它同时也是一个前端框架.它对社区的 webpack,react-router 等进行的封装, 使得我们可以基于它快速搭建一个 React 项目. 第一步:安装umi ...