Description

Link.

给定字符串,正整数集合 \(A,B\),满足 \(\forall u\in A,v\in B,1\le u,v\le n\)。

求 \(\sum_{i\in A}\sum_{j\in B}\text{LCP}(A,B)\)。

Solution

双倍经验是 SvT,只不过 SvT 这屑玩意儿卡常。

先反转串,然后插入 SAM。众所周知

把字符串反转后插入 SAM 后,两个原串的后缀在 parent tree 上的 \(\text{LCA}\) 是这两个后缀的 \(\text{LCP}\)。

然后你就可以搞两个 DP,分别跑 \(A\) 子树大小,\(B\) 子树大小。

注意根节点需要特殊处理,因为我们是跨子树跑的 DP。不过 SvT 不需要,不知道是不是我的问题(应该就是)。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m,dfn[500010],fa[500010][21],dep[500010],sjc,pos[200010],onepower[500010],anopower[500010],onef[500010],anof[500010];
char s[200010];
LL ans;
struct SuffixAutomaton
{
#define ID(c) ((c)-'a')
vector<int> e[500010];
int n,cntot,las,len[500010],pre[500010],ch[500010][26];
char s[200010];
void init(int _n,char c[])
{
n=_n;
for(int i=1;i<=n;++i) s[i]=c[i];
cntot=las=1;
}
void extend(char c)
{
int cur=++cntot,one=las,ano=0;
len[cur]=len[las]+1,las=cur;
while(one&&!ch[one][ID(c)]) ch[one][ID(c)]=cur,one=pre[one];
if(one==0) pre[cur]=1;
else
{
ano=ch[one][ID(c)];
if(len[one]+1==len[ano]) pre[cur]=ano;
else
{
int clone=++cntot;
len[clone]=len[one]+1;
pre[clone]=pre[ano];
memcpy(ch[clone],ch[ano],sizeof(ch[ano]));
while(one&&ch[one][ID(c)]==ano) ch[one][ID(c)]=clone,one=pre[one];
pre[ano]=pre[cur]=clone;
}
}
}
void build()
{
for(int i=1;i<=n;++i) extend(s[i]),pos[i]=las;
for(int i=2;i<=cntot;++i) e[pre[i]].emplace_back(i);
}
}SAM;
void dfs(int x,int las)
{
dfn[x]=++sjc,fa[x][0]=las,dep[x]=dep[las]+1;
for(int i=1;i^21;++i) fa[x][i]=fa[fa[x][i-1]][i-1];
for(int y : SAM.e[x]) dfs(y,x);
}
int LCA(int one,int ano)
{
if(dep[one]<dep[ano]) swap(one,ano);
for(int i=20;~i;--i) if(dep[fa[one][i]]>=dep[ano]) one=fa[one][i];
if(one^ano)
{
for(int i=20;~i;--i) if(fa[one][i]^fa[ano][i]) one=fa[one][i],ano=fa[ano][i];
return fa[one][0];
}
else return one;
}
bool cmp(int one,int ano){return dfn[one]<dfn[ano];}
struct VirtualTree
{
vector<int> e[500010];
vector<int> build(vector<int> poi)
{
sort(poi.begin(),poi.end(),cmp);
poi.erase(unique(poi.begin(),poi.end()),poi.end());
int len=poi.size();
for(int i=1;i<len;++i) poi.push_back(LCA(poi[i-1],poi[i]));
sort(poi.begin(),poi.end(),cmp);
poi.erase(unique(poi.begin(),poi.end()),poi.end());
len=poi.size();
for(int i=1;i<len;++i) e[LCA(poi[i-1],poi[i])].push_back(poi[i]);
return poi;
}
}VRT;
template<class T>
void read(T &hhh)
{
T x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^'0'),c=getchar();
if(~f) hhh=x;
else hhh=-x;
}
template<class T>
void write(T x,char las='\n')
{
static int st[100],top=0;
if(x<0) putchar('-'),x=-x;
do st[++top]=x%10,x/=10; while(x);
while(top) putchar(st[top--]^'0');
putchar(las);
}
void exdfs(int x)
{
for(int y : VRT.e[x]) exdfs(y),onef[x]+=onef[y],anof[x]+=anof[y];
for(int y : VRT.e[x]) ans+=(LL)SAM.len[x]*(onef[x]-onef[y])*anof[y];
ans+=(LL)((onepower[x]&anopower[x])+onepower[x]*anof[x]+anopower[x]*onef[x])*SAM.len[x];
onef[x]+=onepower[x],anof[x]+=anopower[x];
}
int main()
{
read(n),read(m);
scanf("%s",s+1);
reverse(s+1,s+n+1);
SAM.init(n,s),SAM.build();
dfs(1,0);
while(m--)
{
int ones,anos,x;
read(ones),read(anos);
vector<int> key,tmp;
while(ones--) read(x),key.push_back(pos[n-x+1]),onepower[pos[n-x+1]]=1;
while(anos--) read(x),key.push_back(pos[n-x+1]),anopower[pos[n-x+1]]=1;
tmp=VRT.build(key);
ans=0,exdfs(tmp[0]);
write(ans);
for(int now : tmp) onef[now]=anof[now]=0,VRT.e[now].clear(),onepower[now]=anopower[now]=0;
}
return 0;
}

Solution -「CF 1073G」Yet Another LCP Problem的更多相关文章

  1. Solution -「CF 1342E」Placing Rooks

    \(\mathcal{Description}\)   Link.   在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...

  2. Solution -「CF 1622F」Quadratic Set

    \(\mathscr{Description}\)   Link.   求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...

  3. Solution -「CF 923F」Public Service

    \(\mathscr{Description}\)   Link.   给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...

  4. Solution -「CF 923E」Perpetual Subtraction

    \(\mathcal{Description}\)   Link.   有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...

  5. Solution -「CF 1586F」Defender of Childhood Dreams

    \(\mathcal{Description}\)   Link.   定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...

  6. Solution -「CF 1237E」Balanced Binary Search Trees

    \(\mathcal{Description}\)   Link.   定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...

  7. Solution -「CF 623E」Transforming Sequence

    题目 题意简述   link.   有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...

  8. Solution -「CF 1023F」Mobile Phone Network

    \(\mathcal{Description}\)   Link.   有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...

  9. Solution -「CF 599E」Sandy and Nuts

    \(\mathcal{Description}\)   Link.   指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...

  10. Solution -「CF 487E」Tourists

    \(\mathcal{Description}\)   Link.   维护一个 \(n\) 个点 \(m\) 条边的简单无向连通图,点有点权.\(q\) 次操作: 修改单点点权. 询问两点所有可能路 ...

随机推荐

  1. Vue自定义指令-让你的业务开发更简单

    1.使用场景 在日常开发中,我们会将重复代码抽象为一个函数或者组件,然后在需要时调用或者引入.但是,对于某些功能,这种方法可能不够优雅或者不够灵活.例如,我们可能需要在DOM元素上添加一些自定义属性或 ...

  2. kotlin 函数格式大赏

    fun main() { // 一个有引用的lambda表达式 val f11: (Int, Int) -> Unit = {n1, n2 -> println("f11 is ...

  3. RT_object

    以下图片来自"张世争"的微博  

  4. Java基础之基础语法与面向对象

    前言 小知识 Java由Sun公司于1995年推出,2009年Sun公司被Oracle公司收购,取得Java的版权 Java之父:James Gosling(詹姆斯·高斯林) 专业术语 JDK:jav ...

  5. Mybatis的ResultMap对column和property的理解

    Mybatis的ResultMap对column和property的理解 首先,先看看这张图,看能不能一下看明白: select元素有很多属性(这里说用的比较多的): id:命名空间唯一标识,可以被用 ...

  6. ReactNative原理与核心知识点

    React Native特点 跨平台 使用js写出页面组件代码被React框架统一转成Virtual DOM树,Virtual DOM树是UI结构的一层抽象,可以被转换成任何支持端的UI视图. Rea ...

  7. Linux系统运维之MYSQL数据库集群部署(主从复制)

    一.介绍 Mysql主从复制,前段时间生产环境部署了一套主从复制的架构,当时现找了很多资料,现在记录下 二.拓扑图 三.环境以及软件版本 主机名 IP 操作系统 角色 软件版本 MysqlDB_Mas ...

  8. mac Typora最新版逆向破解

    0x00 Typora是一款强大的markdown编辑器,它可以让你轻松地写出美观的文档.但是其一直是不开源的,而且现在也已经开始收费了.所以本着学习探索的精神去逆向看看- 0x01 众所周知Typo ...

  9. 春节无法线下社交聚会,来线上“一起X”共享体验

    引语: 共享体验或许是全真互联网时代最显著的标志. 被疫情深刻改变的一年里,人们的社交关系和社交活动正在发生巨大的改变. 一方面,从线下转线上,我们能通过互联网连接更多的人,参与各种形式的社交活动,将 ...

  10. Delta Lake_ High-Performance ACID Table Storage over Cloud Object Stores

    论文发表于 2020年, 研究数据湖产品的很好的学习资料. 概要 开篇很明确的表明了为什么要做Delta lake这样一个产品. Databricks尝试将数据仓库直接架在云上对象存储之上, 这种尝试 ...