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$

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef unsigned long long ull;
int read(){
char c=getchar(); int x=;
while(c<''||c>'') c=getchar();
while(''<=c&&c<='') x=x*+c-,c=getchar();
return x;
}
#define N 300005
const int bas=;
int n,Q; char q[N],q1[]; ull ha[N];
struct node{int ch[],sz,fa; ull vl,w;}a[N];
struct Splay{
#define lc a[x].ch[0]
#define rc a[x].ch[1]
#define mid (l+r)/2
int rt,u;
inline void up(int x){
a[x].sz=a[lc].sz+a[rc].sz+,
a[x].vl=a[lc].vl*ha[a[rc].sz+]+a[x].w*ha[a[rc].sz]+a[rc].vl;
}
inline int whi(int y,int x){return a[y].ch[]==x;}
void turn(int x,int &k){
int y=a[x].fa,z=a[y].fa,l=whi(y,x),r=l^;
if(y==k) k=x;
else a[z].ch[whi(z,y)]=x;
a[a[x].ch[r]].fa=y; a[y].fa=x; a[x].fa=z;
a[y].ch[l]=a[x].ch[r]; a[x].ch[r]=y;
up(y); up(x);
}
void splay(int x,int &k){
for(;x!=k;turn(x,k)){
int y=a[x].fa,z=a[y].fa;
if(y!=k) turn(whi(z,y)==whi(y,x)?y:x,k);
}
}
int kth(int k){
int x=rt;
while(a[lc].sz+!=k){
if(a[lc].sz>=k) x=lc;
else k=k-a[lc].sz-,x=rc;
}return x;
}
inline void rep(int x,ull k){a[x=kth(x)].w=k,splay(x,rt);}
void ins(int x,ull k){
x=kth(x); ++u;
if(!rc) rc=u;
else{for(x=rc;lc;x=lc); lc=u;}
a[u].fa=x; a[u].w=k; up(u); splay(u,rt);
}
ull Fi(int x,int y){
int L=kth(x-),R=kth(y+);
splay(L,rt); splay(R,a[L].ch[]);
return a[a[R].ch[]].vl;
}
int ask(int x,int y){
if(x>y) swap(x,y);
int l=,r=u-y,re=;
while(l<=r){
if(Fi(x,x+mid-)!=Fi(y,y+mid-)) r=mid-;
else re=mid,l=mid+;
}return re;
}
int build(int Fa,int l,int r){
a[mid].w=(ull)q[mid-]; a[mid].fa=Fa; ++u;
if(l<mid) a[mid].ch[]=build(mid,l,mid-);
if(r>mid) a[mid].ch[]=build(mid,mid+,r);
up(mid); return mid;
}
}S;
int main(){
scanf("%s",q+); n=strlen(q+); ha[]=;
for(int i=;i<N;++i) ha[i]=ha[i-]*bas;
S.rt=S.build(,,n+); Q=read();//建树时记得加入边界
for(int i=,w1,w2;i<=Q;++i){
scanf("%s",q1);
if(q1[]=='Q') w1=read(),w2=read(),printf("%d\n",S.ask(w1+,w2+));
if(q1[]=='R') w1=read(),scanf("%s",q1),S.rep(w1+,(ull)q1[]);
if(q1[]=='I') w1=read(),scanf("%s",q1),S.ins(w1+,(ull)q1[]);
}return ;
}

P4036 [JSOI2008]火星人(splay+hash+二分)的更多相关文章

  1. BZOJ1014:[JSOI2008]火星人(Splay,hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam, 我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...

  2. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

    题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...

  3. BZOJ_1014_[JSOI2008]火星人prefix_splay+hash

    BZOJ_1014_[JSOI2008]火星人prefix_splay+hash 题意:火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam, 我们 ...

  4. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  5. 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 题意:支持插入一个字符.修改一个字符,查询lcp.(总长度<=100000, 操作< ...

  6. bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

    题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...

  7. bzoj 1014: [JSOI2008]火星人prefix hash && splay

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3154  Solved: 948[Submit][ ...

  8. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  9. 洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)

    题面 洛谷 题解 首先,我们知道求最长公共前缀可以用二分答案+hash来求 因为有修改操作, 考虑将整个字符串的hash值放入splay中 接着就是splay的基本操作了 Code #include& ...

随机推荐

  1. manjaro软件源报错 不停看到错误 "PackageName: signature from "User <email@archlinux.org>" is invalid" 的几种解决方法

    对于报错情况, 格式大致如下: error: PackageName: signature from "User <email@archlinux.org>" is i ...

  2. JDK自带的线程池详解

    1.线程池的使用场景 等待返回任务的结果的多步骤的处理场景, 批量并发执行任务,总耗时是单个步骤耗时最长的那个,提供整体的执行效率, 最终一致性,异步执行任务,无需等待,快速返回 2.线程池的关键参数 ...

  3. 学习旧岛小程序 (5) observer 函数中修改属性的值

    不要在一个属性的 observer  函数中修改属性的值 不然会造成内存泄露 错误代码: properties: { /* 期刊号 */ index: { type: String, observer ...

  4. 【串线篇】spring boot页面模板引擎

    如JSP.Velocity.Freemarker.Thymeleaf SpringBoot推荐的Thymeleaf:语法更简单,功能更强大: 一.引入thymeleaf <dependency& ...

  5. bzoj3991 [SDOI2015]寻宝游戏 树链的并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3991 题解 貌似这个东西叫做树链的并,以前貌似写过一个类似的用来动态维护虚树. 大概就是最终的 ...

  6. 为什么选择Linux

    从最近的统计数据可以看到,全球大量数据中心的服务器已经开始向基于 Linux Server 平台转移.相较 Windows Server 而言,Linux Server 提供了更多优势.包括 Goog ...

  7. HDU6668 Polynomial(模拟)

    HDU6668 Polynomial 顺序遍历找出最高次幂项的系数 分三种情况 \(1/0\).\(0/1\).\(f(x)/g(x)\) . 复杂度为 \(O(n)\) . #include< ...

  8. P2158仪仗队

    今天早上你谷崩了 由于脑子抽筋,所以选了一道数学题来做.做着做着就疯了 传送 窝盟先画张图冷静冷静 这是样例的图,其中蓝点是有学生的地方. 窝盟来看一下那些学生可以被C君看到. 假设这张图是一个坐标系 ...

  9. Oralce-资源配置PROFILE

    profile:作为用户配置文件,它是密码限制,资源限制的命名集合 在安装数据库时,Oracle自动会建立名为default的默认配置文件 使用profile文件时,要注意以下几点: 建立用户时,如果 ...

  10. Jenkins使用六:搭建流水线任务

    流水线可以把多个任务串起来,比如发布版本的一系列流程 配置流水线任务 构建语法为Groovy,执行3次test(job名) node { stage("test") { echo ...