Solution -「CF 1073G」Yet Another LCP Problem
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的更多相关文章
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
- Solution -「CF 599E」Sandy and Nuts
\(\mathcal{Description}\) Link. 指定一棵大小为 \(n\),以 \(1\) 为根的有根树的 \(m\) 对邻接关系与 \(q\) 组 \(\text{LCA}\ ...
- Solution -「CF 487E」Tourists
\(\mathcal{Description}\) Link. 维护一个 \(n\) 个点 \(m\) 条边的简单无向连通图,点有点权.\(q\) 次操作: 修改单点点权. 询问两点所有可能路 ...
随机推荐
- @Override注解的使用
先看看@Override注解在Java SE中的声明: package .lang; import java.lang.annotation.*; @Target(ElementType.METHOD ...
- LLE算法的应用场景和案例:详解LLE算法在实际问题中的效果和表现
目录 引言 随着深度学习技术的不断发展,神经网络模型在人工智能领域的应用越来越广泛.其中,LLE(Largely Element-wise Linear) 神经网络是一种常用的神经网络模型,其基本思想 ...
- 聊聊Flink CDC必知必会
CDC是(Change Data Capture变更数据获取)的简称. 核心思想是,监测并捕获数据库的变动(包括数据 或 数据表的插入INSERT.更新UPDATE.删除DELETE等),将这些变更按 ...
- CPU性能指标介绍及分析
CPU是计算机系统中最核心的组件之一,对系统性能起着至关重要的作用.以下是一些常见的CPU性能指标及其分析: 1. %user(用户态)和 %system(内核态) %user:表示CPU花费在用户进 ...
- Win10激活步骤、密钥key
统安装完毕后,首先以Win+R打开CMD命令行窗口,按下Win+X,选择命令提示符(管理员). Win10企业版 用户举例请依次输入: slmgr /ipk NPPR9-FWDCX-D2C8J-H87 ...
- allure报告生成
allure生成测试报告 1.生成xml文件 pytest 测试文件所在路径 --alluredir 生成的测试结果数据保存的目录 pytest --alluredir=resport/xml/ D ...
- 图像处理_Retinex图像增强
单尺度SSR (Single Scale Retinex) 图像 S ( x , y ) S(x,y) S(x,y)分解为两个不同的图像:反射图像 R ( x , y ) R(x,y) R(x,y), ...
- 华为P9黑屏的解决方案-更换屏幕
解决办法(系统软件) 1.回退系统版本,b198或者b139固件. 2.升级版本,到最新版本.新版本使用时并没有发现这个问题. 解决方法(系统设置) 点开设置-电池-选择进入超级省电模式,然后退出超级 ...
- GaussDB技术解读丨高级压缩
本文作者|华为云数据库GaussDB首席架构师 冯柯 [背景介绍] 数据压缩与关系数据库的结合,早已不是一个新鲜的话题,当前我们已经看到了各种各样数据库压缩的产品和解决方案.对于GaussDB来说,在 ...
- PWM点灯
目录 PWM脉冲宽调点灯 前言 1.什么是PWM 2.PWM的实现 3.PWM实现步骤(通用定时器) 3.1 打开定时器的时钟 3.2 配置端口 3.3 设置定时器 3.4 设置PWM 3.5 完整代 ...