BZOJ2555 SubString【SAM + Link Cut Tree】
BZOJ2555. SubString
要求在线询问一个串在原串中出现的次数,并且可以在原串末尾添加字符串
如果没有修改的话,考虑建出\(parent\)树之后统计每个\(endpos\)节点的\(right\)集合大小,现在要求动态添加字符,那么由于\(parent\)树的形态会变,所以用\(LCT\)来维护\(parent\)树,具体就是增删\(link\)边
\(LCT\)一直以初始状态为根,每次提出来初始状态到当前节点的链然后更新一条链上的值即可,根一直没变,不需要\(makeroot\)操作也不需要\(split\)操作,直接\(access\)当前点,然后\(splay\)上去之后更新
//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int MAXN = (6e5+7)*2;
void decode(string& str, int mask){
for(int i = 0; i < (int)str.size(); i++){
mask = (mask*131+i)%str.size();
swap(str[i],str[mask]);
}
}
struct LinkCutTree{
int ch[MAXN][2],fa[MAXN],val[MAXN],lazy[MAXN],rev[MAXN];
bool isroot(int rt){ return ch[fa[rt]][0]!=rt and ch[fa[rt]][1]!=rt; }
int check(int rt){ return rt == ch[fa[rt]][1]; }
void pushdown(int rt){
if(rev[rt]){
swap(ch[rt][0],ch[rt][1]);
rev[ch[rt][0]] ^= 1;
rev[ch[rt][1]] ^= 1;
rev[rt] ^= 1;
}
if(lazy[rt]){
if(ch[rt][0]){
val[ch[rt][0]] += lazy[rt];
lazy[ch[rt][0]] += lazy[rt];
}
if(ch[rt][1]){
val[ch[rt][1]] += lazy[rt];
lazy[ch[rt][1]] += lazy[rt];
}
lazy[rt] = 0;
}
}
void pushdownall(int rt){
if(!isroot(rt)) pushdownall(fa[rt]);
pushdown(rt);
}
void rotate(int rt){
int f = fa[rt], gf = fa[f], d = check(rt);
if(!isroot(f)) ch[gf][check(f)] = rt;
fa[rt] = gf;
ch[f][d] = ch[rt][d^1]; fa[ch[rt][d^1]] = f;
ch[rt][d^1] = f; fa[f] = rt;
}
void splay(int rt){
pushdownall(rt);
while(!isroot(rt)){
int f = fa[rt];
if(!isroot(f)){
if(check(rt)==check(f)) rotate(f);
else rotate(rt);
}
rotate(rt);
}
}
void access(int rt){
int c = 0;
while(rt){
splay(rt);
ch[rt][1] = c;
rt = fa[c = rt];
}
}
void link(int x, int f){
fa[x] = f; access(f); splay(f);
val[f] += val[x]; lazy[f] += val[x];
}
void cut(int u){
access(u);
splay(u);
lazy[ch[u][0]] -= val[u];
val[ch[u][0]] -= val[u];
fa[ch[u][0]] = 0; ch[u][0] = 0;
}
int query(int u){
splay(u);
return val[u];
}
}lct;
struct SAM{
int len[MAXN],link[MAXN],ch[MAXN][26],tot,last;
SAM(){ link[last = tot = 1] = 0; }
void extend(int c){
int np = ++tot, p = last;
lct.val[np] = 1;
len[np] = len[last] + 1;
while(p and !ch[p][c]){
ch[p][c] = np;
p = link[p];
}
if(!p){
link[np] = 1;
lct.link(np,1);
}
else{
int q = ch[p][c];
if(len[p]+1==len[q]){
link[np] = q;
lct.link(np,q);
}
else{
int clone = ++tot;
len[clone] = len[p] + 1;
link[clone] = link[q];
lct.link(clone,link[q]);
for(int i = 0; i < 26; i++) ch[clone][i] = ch[q][i];
lct.cut(q); lct.link(q,clone);
lct.link(np,clone);
link[np] = link[q] = clone;
while(p and ch[p][c]==q){
ch[p][c] = clone;
p = link[p];
}
}
}
last = np;
}
int calsub(string s){
int u = 1;
for(int i = 0; i < (int)s.size(); i++){
int c = s[i] - 'A';
if(!ch[u][c]) return 0;
u = ch[u][c];
}
return lct.query(u);
}
}sam;
int n,mask;
string s,op;
int main(){
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s;
for(int i = 0; i < (int)s.size(); i++) sam.extend(s[i]-'A');
while(n--){
cin >> op >> s;
decode(s,mask);
if(op[0]=='Q'){
int ret = sam.calsub(s);
mask ^= ret;
cout << ret << endl;
}
else for(int i = 0; i < (int)s.size(); i++) sam.extend(s[i]-'A');
}
return 0;
}
BZOJ2555 SubString【SAM + Link Cut Tree】的更多相关文章
- [BZOJ 1036] [ZJOI2008] 树的统计Count 【Link Cut Tree】
题目链接:BZOJ - 1036 题目分析 这道题可以用树链剖分,块状树等多种方法解决,也可以使用 LCT. 修改某个点的值时,先将它 Splay 到它所在的 Splay 的根,然后修改它的值,再将它 ...
- CF614A 【Link/Cut Tree】
题意:求出所有w^i使得l<=w^i<=r 输入为一行,有三个数,分别是l,r,w.意义如题目所描述 输出为一行,输出所有满足条件的数字,每两个数字中间有一个空格 如果没有满足条件的数字则 ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
随机推荐
- readhat6.5下安装weblogic10.3.6
转载自:http://www.mianhuage.com/752.html 1.安装前准备 1.1.准备安装包generic.jar1.2.创建weblogic用户及用户组创建组命令:groupadd ...
- 剑指Offer-连续子数组中的最大和
题目 输入一个整型数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为 O(n). 输入 [1,-2,3,10,-4,7,2,-5] 返回值 ...
- Pandas应用案例-股票分析:使用tushare包获取股票的历史行情数据进行数据分析
目标: 使用tushare包获取股票的历史行情数据 输出该股票所有收盘比开盘上涨3%以上的日期 输出该股票所有开盘比前日收盘跌幅超过2%以上的日期 假如为我们从2010年1月1日开始,每月第一个交易日 ...
- C#使用struct直接转换下位机数据
编写上位机与下位机通信的时候,涉及到协议的转换,比较多会使用到二进制.传统的方法,是将数据整体获取到byte数组中,然后逐字节对数据进行解析.这样操作工作量比较大,对于较长数据段更容易计算位置出错. ...
- Description Resource Path Location Type Failure to transfer org.apache.maven.plugins:maven-surefire-
url:https://www.pianshen.com/article/8003307916/ Description Resource Path Location Type Failure to ...
- three.js cannon.js物理引擎之Heightfield
今天郭先生说一说cannon.js物理引擎之Heightfield高度场,学过场论的朋友都知道物理学中把某个物理量在空间的一个区域内的分布称为场,高度场就是与高度相关的场,而cannon.js物理引擎 ...
- Flutter--Flutter中Widget、App的生命周期
前言 在App的开发过程中,我们通常都需要了解App以及各个页面的生命周期,方便我们在App进入前台时启动一些任务,在进入后台后暂停一些任务.同时,各个页面的生命周期也很重要,每个页面消失时要做一些内 ...
- wordpress迁移报错
背景: 因为一些原因迁移wordpress的博客.备份好数据库和网站源码到另一台生产环境上线的时候报错: Warning: require(/www/wwwroot/pazzn/wp-includes ...
- https://dev.mysql.com/doc/refman/8.0/en/savepoint.html
https://dev.mysql.com/doc/refman/8.0/en/savepoint.html
- .Net 5 C# 泛型(Generics)
这里有个目录 什么是泛型? 后记 什么是泛型? 我们试试实现这个需求,给一个对象,然后返回 另一个同样的对象,先不管这个实用性,我们实现看看 首先是int型 private int Get(int a ...