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. spring-boot集成mybatis真的很简单吗?

    在日常的后端开发中,使用mybatis作为DAO层的持久框架已经是惯例.但很多时候都是在别人搭好的框架中进行开发,对怎么搭建环境是一知半解,今天就来实践下. 一.集成分哪些步骤 来看下集成mybati ...

  2. Python初学者友好丨详解参数传递类型

    摘要: 本文清晰地解释了Python中的不同参数传递类型,并提供了示例代码来说明每种类型的用法.对于初学者或不清楚Python传参的读者们来说是非常有益的,文中提供了足够的信息来理解和使用Python ...

  3. [solved] login to server failed: EOF

    问题 frp 报错:login to server failed: EOF 解决方案 客户端配置 common 下 添加 tls_enable = true [common] tls_enable = ...

  4. 如何构建高效、可观的系统「GitHub 热点速览」

    经典老项目 system-design 教你如何设计一个健壮的系统,新项目 noodle 教你如何提升教育效率,而后者甚至单日获得了 1,600 star,刚开源就获得了 6k+ 的 star. 除了 ...

  5. 最为常用的Laravel操作(3)-模板

    Blade 模板引擎 模板继承 定义布局: <!-- 存放在 resources/views/layouts/app.blade.php --> <html> <head ...

  6. 原生poi实现模版导出

    背景 我们公司是内网开发,外网jar包我的权限不够,所以easyexcel jar包无法使用,参考了easyexcel的填充思想,写了一个较简单的填充方法,如果直接用easyexcel的话,可以参考这 ...

  7. React: Warning: `value` prop on `input` should not be null.

    警告 解决方案 对value进行判断 修改后的效果 至此问题解决

  8. APubPlat 一款Devops自动化部署、持续集成、堡垒机开源项目、友好的Web Terminal

    嗨.很高心你能进入这里,我是zane,  在这里给你介绍一款完整的Devops自动化部署工具 APubPlat - 一款完整的Devops自动化部署.持续集成.堡垒机.并且友好的Web Termina ...

  9. 学习OI两年我到底收获了什么

    做一个小小的总结 学习了两年的代码,刚刚要进入高中,留下一点文字给以前的学习做一个总结. 命中注定の邂逅-- 这两年之间,和编程产生了比学习更为低调的羁绊关系(我觉得用这个词语比较合适).编程给我带来 ...

  10. python命令行解析模块argparse

    argparse是Python标准库中推荐的命令行解析模块 code01: tmp.py import argparse parser = argparse.ArgumentParser(descri ...