CF587F Duff is Mad
有趣的思想
首先暴力的话,自然是对每一个询问在\(AC\)自动机上跑一遍\(k\),看看跑出来的节点在\(fail\)树到根的路径上有多少个\(l\)到\(r\)之间的结束标记就好了
我们发现无论怎么优化好像都不是很可行,考虑一下对根号优化
对于长度大于\(\sqrt{n}\)的串,显然这样的串也不会超过\(\sqrt{n}\)个,我们把这些串在\(AC\)机上跑一遍,之后统计一下子树和,统计一个前缀和就可以回答询问了
这样复杂度是\(O(n\sqrt{n})\)
对于长度小于\(\sqrt{n}\)的串,我们允许把每一个串都放到\(AC\)自动机上跑一遍,于是可以直接用主席树来查一下跑出来的节点到根有多少个标记在\(l\)到\(r\)之间
这边的复杂度是\(O(n\sqrt{n}logn)\)
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#define maxn 100005
#define M 4000005
#define re register
#define LL long long
#define int long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read() {
int x=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
struct E{int v,nxt;}e[maxn];
struct Ask{int l,r,k,rk,o;}q[maxn];
int n,m,cnt,tot,num,sz;
int fail[maxn],son[maxn][26],len[maxn],head[maxn],a[maxn];
LL sum[maxn],pre[maxn],Ans[maxn];
int rt[maxn],ls[M],rs[M],d[M];
std::string S[maxn];
char T[maxn];
std::vector<int> v[maxn];
inline int cmp1(Ask A,Ask B) {if(A.o==B.o)return A.k<B.k;return A.o>B.o;}
inline void add(int x,int y) {e[++num].v=y;e[num].nxt=head[x];head[x]=num;}
inline void work() {for(re int i=1;i<=n;i++) pre[i]=pre[i-1]+sum[a[i]];}
inline void ins(int i) {
scanf("%s",T);
S[i]=T;len[i]=S[i].size();tot+=len[i];
int now=0;
for(re int j=0;j<S[i].size();j++) {
if(!son[now][S[i][j]-'a']) son[now][S[i][j]-'a']=++cnt;
now=son[now][S[i][j]-'a'];
}
v[now].push_back(i);a[i]=now;
}
inline void Build() {
std::queue<int> q;
for(re int i=0;i<26;i++) if(son[0][i]) q.push(son[0][i]);
while(!q.empty()) {
int k=q.front();q.pop();
for(re int i=0;i<26;i++)
if(son[k][i]) fail[son[k][i]]=son[fail[k]][i],q.push(son[k][i]);
else son[k][i]=son[fail[k]][i];
}
}
inline int change(int pre,int x,int y,int pos) {
int root=++sz;
d[root]=d[pre]+1;
if(x==y) return root;
ls[root]=ls[pre],rs[root]=rs[pre];
int mid=x+y>>1;
if(pos<=mid) ls[root]=change(ls[pre],x,mid,pos);
else rs[root]=change(rs[pre],mid+1,y,pos);
return root;
}
inline int query(int p,int x,int y,int pos) {
if(!pos) return 0;
if(x==y) return d[p];
int mid=x+y>>1;
if(pos<=mid) return query(ls[p],x,mid,pos);
return d[ls[p]]+query(rs[p],mid+1,y,pos);
}
inline void Query(int i) {
int now=0;
for(re int j=0;j<len[i];j++)
now=son[now][S[i][j]-'a'],sum[now]++;
}
inline void dfs(int x) {
for(re int i=head[x];i;i=e[i].nxt) {
dfs(e[i].v),sum[x]+=sum[e[i].v];
}
}
inline void Dfs(int x) {
for(re int i=head[x];i;i=e[i].nxt) {
rt[e[i].v]=rt[x];
for(re int j=0;j<v[e[i].v].size();j++) rt[e[i].v]=change(rt[e[i].v],1,n,v[e[i].v][j]);
Dfs(e[i].v);
}
}
signed main() {
n=read(),m=read();
for(re int i=1;i<=n;i++) ins(i);
tot=std::sqrt(tot);Build();
for(re int i=1;i<=m;i++)
q[i].l=read(),q[i].r=read(),q[i].k=read(),q[i].rk=i,q[i].o=(len[q[i].k]>tot);
std::sort(q+1,q+m+1,cmp1);
for(re int i=1;i<=cnt;i++) add(fail[i],i);
Dfs(0);
int L=1,R=1;Query(q[1].k);dfs(0),work();
for(re int i=2;i<=m+1;i++) {
if(len[q[i].k]<=tot) {for(re int j=L;j<i;j++) Ans[q[j].rk]=pre[q[j].r]-pre[q[j].l-1];R=i;break;}
if(q[i].k!=q[i-1].k) {
for(re int j=L;j<i;j++) Ans[q[j].rk]=pre[q[j].r]-pre[q[j].l-1];
memset(sum,0,sizeof(sum));
Query(q[i].k);dfs(0);work();L=i;
}
}
for(re int i=R;i<=m;i++) {
int now=0;
for(re int j=0;j<len[q[i].k];j++)
now=son[now][S[q[i].k][j]-'a'],Ans[q[i].rk]+=query(rt[now],1,n,q[i].r)-query(rt[now],1,n,q[i].l-1);
}
for(re int i=1;i<=m;i++) printf("%I64d\n",Ans[i]);
return 0;
}
CF587F Duff is Mad的更多相关文章
- [CF587F]Duff is Mad[AC自动机+根号分治+分块]
题意 给你 \(n\) 个串 \(s_{1\cdots n}\) ,每次询问给出 \(l,r,k\) ,问在 \(s_{l\cdots r}\) 中出现了多少次 \(s_k\) . \(n,q,\su ...
- CF587F Duff is Mad(AC自动机+树状数组+分块)
考虑两一个暴力 1 因为询问\([a,b]\)可以拆成\([1,b]\)-\([1,a-1]\)所以把询问离线,然后就是求\([1,x]\)中被\(S_i\)包含的串的数量.考虑当\([1,x-1]- ...
- 【CF587F】Duff is Mad AC自动机+分块
[CF587F]Duff is Mad 题意:给出n个串$s_1,s_2..s_n$,有q组询问,每次给出l,r,k,问你编号在[l,r]中的所有串在$s_k$中出现了多少次. $\sum|s_i|, ...
- Codeforces 587F - Duff is Mad(根号分治+AC 自动机+树状数组)
题面传送门 第一眼看成了 CF547E-- 话说 CF587F 和 CF547E 出题人一样欸--还有另一道 AC 自动机的题 CF696D 也是这位名叫 PrinceOfPersia 的出题人出的- ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- cf Round 587
A.Duff and Weight Lifting(思维) 显然题目中只有一种情况可以合并 2^a+2^a=2^(a+1).我们把给出的mi排序一下,模拟合并操作即可. # include <c ...
- Lesson 21 Mad or not?
Text Aeroplanes are slowly driving me mad. I live near an airport and passing planes can be heard ni ...
- Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting
B. Pasha and PhonePasha has recently bought a new phone jPager and started adding his friends' phone ...
- 【转】Duff's Device
在看strcpy.memcpy等的实现发现用了内存对齐,每一个word拷贝一次的办法大大提高了实现效率,参加该blog(http://totoxian.iteye.com/blog/1220273). ...
随机推荐
- EveryThing 使用方法
1.按照时间范围:datetimed: <*.doc|*.docx> dm:1/2015-6/2015// 如果本地的时间设置是年份在前,那么把年份放在月份前面// d: <*.d ...
- NodeJs接口token认证express框架passport实现方式Bearer认证
1.生成一个简单的express项目(命令:express passport-test),项目结构如下: 2.添加项目依赖: npm install passport --save npm insta ...
- Java - 网络IO的阻塞
最近学习时碰到事件驱动和非阻塞的相关知识,随之想到了Java中的Reactor.io与nio的一些东西:在前辈的博客上翻了翻.复习复习,在此记录一番. 实在找不到比较大点的东西,于是随便弄了个压缩包, ...
- spring AOP Capability and Goals(面向方面编程功能和目标归纳)
原官方文档链接: https://docs.spring.io/spring/docs/5.1.6.RELEASE/spring-framework-reference/core.html#aop-i ...
- SSM面试
Spring两大核心:IOC AOP DI AOP:所谓面向切面变成,是一种通过预编译和运行期动态化代理的方式实现了再不修改源代码的情况下给程序动态添加功能的技术. Mybatis(半自动化实现obj ...
- 小任务之使用SVG画柱状图~
function drawBar(data) { var barGraph = document.querySelector("#bar-graph"); var graphWid ...
- css移动端:acitve效果的实现
做移动前端也有一些日子了,一直有个问题没有解决,就是与pc端那样的一个:hover的效果,:hover是鼠标指针浮动在其上的元素的一个选择器,但因为在移动端是没有鼠标的,代替的是触摸屏,用户也不是有“ ...
- HDD 机械硬盘 安装 linux(centos7)
1. 下载 UltraISO 文件-->打开, 选中centos.iso镜像; 启动-->写入硬盘映像-->硬盘驱动器(选中u盘)写入方式(USB-HDD+v2)-->写入 ...
- ElementUI组件库常见方法及问题汇总(持续更新)
本文主要介绍在使用ElementUI组件库的时候,常遇见的问题及使用到的方法,汇总记录便于查找. 1.表单 阻止表单的默认提交 <!-- @submit.native.prevent --> ...
- Python入门-生成器和生成器表达式
昨天我们说了迭代器,那么和今天说的生成器是什么关系呢? 一.生成器 什么是生成器?说白了生成器的本质就是迭代器. 在Python中中有三种方式来获取生成器. 1.通过生成器函数 2.通过各种推导式来实 ...