bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)
题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符。
前几天刚学了hash+二分求lcp,就看到这题。
原来splay还能这么用?!原来splay模板这么好写?我以前写的splay是假的吧woc
splay每个节点代表一个字符,并维护这个子树代表一个子串的哈希值。因为splay旋转不破坏树结构,所以不论怎么旋转这棵splay树都能代表这个字符串。
预处理处理少了调了半天呜呜呜
赶紧跑去更新自己的splay模板
- #include<iostream>
- #include<cstring>
- #include<cstdlib>
- #include<cstdio>
- #define which(x) (son[fa[x]][1]==x)
- #define ull unsigned long long
- using namespace std;
- const int maxn=;
- int n,m,x,y,tot,root;
- int fa[maxn],son[maxn][],size[maxn],s[maxn];
- ull hs[maxn],mul[maxn];
- char st[maxn],ch[maxn],ch2[maxn];
- void read(int &k)
- {
- int f=;k=;char c=getchar();
- while(c<''||c>'')c=='-'&&(f=-),c=getchar();
- while(c<=''&&c>='')k=k*+c-'',c=getchar();
- k*=f;
- }
- void update(int x)
- {
- size[x]=size[son[x][]]+size[son[x][]]+;
- hs[x]=hs[son[x][]]+mul[size[son[x][]]]*s[x]+mul[size[son[x][]]+]*hs[son[x][]];
- }
- void rotate(int x)
- {
- int f=fa[x];
- bool k=which(x);
- son[f][k]=son[x][!k];
- son[x][!k]=f;
- son[fa[f]][which(f)]=x;
- fa[son[f][k]]=f;
- fa[x]=fa[f];
- fa[f]=x;
- size[x]=size[f];
- hs[x]=hs[f];
- update(f);
- }
- void splay(int x,int g)
- {
- while(fa[x]!=g)
- {
- int f=fa[x];
- if(fa[f]==g)
- {
- rotate(x);
- break;
- }
- if(which(x)^which(f))rotate(x);
- else rotate(f);
- rotate(x);
- }
- if(!g)root=x;
- }
- int rank(int x,int k)
- {
- if(k<=size[son[x][]])return rank(son[x][],k);
- else if(k==(size[son[x][]]+))return x;
- else return rank(son[x][],k-size[son[x][]]-);
- }
- int build(int l,int r,int f)
- {
- if(l>r)return ;
- int x=++tot,mid=(l+r)>>;
- fa[x]=f;s[x]=(int)(st[mid]-'a')+;
- son[x][]=build(l,mid-,x);
- son[x][]=build(mid+,r,x);
- update(x);
- return x;
- }
- void insert(int k,int ch)
- {
- int x=rank(root,k),y=rank(root,k+);
- splay(x,);splay(y,x);
- s[++tot]=ch;
- fa[tot]=y;son[y][]=tot;
- update(tot);update(y);update(x);
- }
- void change(int k,int ch)
- {
- int x=rank(root,k);
- splay(x,);
- s[x]=ch;
- update(x);
- }
- int lcp(int kx,int ky)
- {
- int l=,r=n;
- while(l<r)
- {
- int mid=(l+r+)>>;
- if(ky+mid>n+)
- {
- r=mid-;
- continue;
- }
- int x=rank(root,kx-),y=rank(root,kx+mid);
- splay(x,);splay(y,x);
- ull haxi=hs[son[y][]];
- x=rank(root,ky-),y=rank(root,ky+mid);
- splay(x,);splay(y,x);
- if(haxi==hs[son[y][]])l=mid;
- else r=mid-;
- }
- return l;
- }
- int main()
- {
- scanf("%s",st+);
- n=strlen(st+);
- mul[]=;
- for(int i=;i<=;i++)mul[i]=mul[i-]*;
- root=build(,n+,);
- read(m);
- for(int i=;i<=m;i++)
- {
- scanf("%s",ch);
- if(ch[]=='Q')
- {
- read(x);read(y);
- if(x>y)swap(x,y);
- if(x!=y)printf("%d\n",lcp(x+,y+));
- else printf("%d\n",n-x+);
- }
- if(ch[]=='R')
- {
- read(x);
- scanf("%s",ch2);
- change(x+,(int)(ch2[]-'a')+);
- }
- if(ch[]=='I')
- {
- read(x);
- scanf("%s",ch2);
- insert(x+,(int)(ch2[]-'a')+);
- n++;
- }
- }
- return ;
- }
bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)的更多相关文章
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分
题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...
- BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8112 Solved: 2569[Submit] ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash
我写的代码好像自古以来就是bzoj不友好型的 本地跑的比std快,但是交上去巧妙被卡 答案...应该是对的,拍了好久了 #include <bits/stdc++.h> #define M ...
- 【BZOJ1014】[JSOI2008]火星人prefix Splay+hash
[BZOJ1014][JSOI2008]火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个 ...
- BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )
用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...
- 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$ ...
- BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)
题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...
- [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- BZOJ1014[JSOI2008]火星人prefix(splay维护hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
随机推荐
- hdu1010Tempter of the Bone(迷宫dfs+剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Vs2015 遇到 CL:fatal error c1510 cannot load language clui.dll
网上说什么点击修复VS,修改VS的,经验证都不好使,直接下载这个库,放在system32/64下面皆可以了
- Windows运行机理——消息与消息队列
Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 Windows程序设计时一种基于消息的时机驱动方式的设计模式,完全不同 ...
- GIt学习第一天之安装和版本库创建
搬运自 ‘廖雪峰的官方网站’ 1.git安装 官网下载地址:https://git-scm.com/download/win 百度网盘下载地址:https://pan.baidu.com/s/1k ...
- 初学Direct X(4)
初学Direct X(4) 本文学着做出一个如下的小游戏 游戏方式是使用键盘控制红色的Bucket收集蓝色的炸弹 1.酝酿一下 现在我已经掌握: 将位图文件加载到内存 绘制位图到buckbuffer ...
- 统计hive库表在具体下所有分区大小
1 查询具体表分区大小,以字节展示 hadoop fs -du /user/hive/warehouse/treasury.db/dm_user_excercise > dm_user_exce ...
- lintcode 二叉树前序遍历
二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. / ...
- JAVA基础学习之路(二)方法定义,重载,递归
一,方法的定义: package test; public class test1 { public static void main(String args[]) { int result = ad ...
- truffle框架快速开发合约步骤
矩阵元区块链智能合约开发指南 1 适用范围 本规范描述了矩阵元区块链系统智能合约的开发约束与规范,用以指导DAPP开发者按照本规范开发基于矩阵元区块链运行的应用. 2 术语解释 术语 术语解释 DAP ...
- maven 教程二 深入
一:编写POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...