原问题可以转化为:给定第k个字符串,求它在L-R的字符串里作为子串出现了多少次

定义子串为字符串的某个前缀的某个后缀(废话)

等价于我们把一个字符串插入到trie里,其过程中每个经过的节点和其向上的fail链上的点都是该字符串的子串

又因为对于一条fail链,u向上能访问到v当前仅当u在v的子树内

那么原问题又变成了:

将L-R个字符串按照上述方法插入到trie中并将经过的节点的val值增加

求第k个字符串对应的单词节点在fail树上的子树的权值和

又因为查询的信息满足区间可减性,所以我们可以建出fail树

对fail树用可持久化线段树维护DFS序 完成单点修改和子树查询

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; const int maxn=500010;
int n,m,sum=0;
int L,R,k;
int pos[maxn];
int rt[maxn];
char s[maxn];
queue<int>Q; int h[maxn],cnt=0;
int A[maxn],B[maxn],tot=0;
struct edge{
int to,next;
}G[maxn<<1]; void add(int x,int y){++cnt;G[cnt].to=y;G[cnt].next=h[x];h[x]=cnt;}
void Get_DFS(int u){
A[u]=++tot;
for(int i=h[u];i;i=G[i].next)Get_DFS(G[i].to);
B[u]=tot;
} struct Seg_Tree{
int L,R,v;
}t[11000010]; void build(int &o,int L,int R){
o=++sum;
if(L==R)return;
int mid=(L+R)>>1;
build(t[o].L,L,mid);
build(t[o].R,mid+1,R);
}
void modify(int &o,int L,int R,int p){
t[++sum]=t[o];o=sum;
if(L==R){t[o].v++;return;}
int mid=(L+R)>>1;
if(p<=mid)modify(t[o].L,L,mid,p);
else modify(t[o].R,mid+1,R,p);
t[o].v=t[t[o].L].v+t[t[o].R].v;
}
int ask(int o,int L,int R,int x,int y){
if(L>=x&&R<=y)return t[o].v;
int mid=(L+R)>>1;
if(y<=mid)return ask(t[o].L,L,mid,x,y);
else if(x>mid)return ask(t[o].R,mid+1,R,x,y);
else return ask(t[o].L,L,mid,x,y)+ask(t[o].R,mid+1,R,x,y);
} struct Trie{
int cnt;
int t[maxn][26];
int fail[maxn],fa[maxn];
void init(){
cnt=1;fail[0]=1;
for(int i=0;i<26;++i)t[0][i]=1;
}
int insert(){
int len=strlen(s+1);
int now=1;
for(int i=1;i<=len;++i){
int id=s[i]-'a';
if(!t[now][id])t[now][id]=++cnt,fa[t[now][id]]=now;
now=t[now][id];
}return now;
}
void build_fail(){
Q.push(1);fail[1]=0;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=0;i<26;++i){
if(t[u][i]){
int k=fail[u];
while(!t[k][i])k=fail[k];
fail[t[u][i]]=t[k][i];
add(t[k][i],t[u][i]);
Q.push(t[u][i]);
}
}
}return;
}
void UPD(){
build(rt[0],1,cnt);
for(int i=1;i<=n;++i){
rt[i]=rt[i-1];
for(int j=pos[i];j!=1;j=fa[j]){
modify(rt[i],1,cnt,A[j]);
}
}return;
}
}AC; int main(){
scanf("%d%d",&n,&m);
AC.init();
for(int i=1;i<=n;++i){
scanf("%s",s+1);
pos[i]=AC.insert();
}AC.build_fail();Get_DFS(1);AC.UPD();
for(int i=1;i<=m;++i){
scanf("%d%d%d",&L,&R,&k);
printf("%d\n",ask(rt[R],1,AC.cnt,A[pos[k]],B[pos[k]])-ask(rt[L-1],1,AC.cnt,A[pos[k]],B[pos[k]]));
}return 0;
}

  

即可

codeforces #305 E Mike and friends的更多相关文章

  1. codeforces #305 A Mike and Frog

    挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨 ...

  2. codeforces #305 B Mike and Feet

    跟之前做过的51Nod的移数博弈是一样的QAQ 我们考虑每个数的贡献 定义其左边第一个比他小的数的位置为L 定义其右边第一个比他小的数的位置为R 这个可以用排序+链表 或者 单调队列 搞定 那么对于区 ...

  3. codeforces #305 D Mike and Fish

    正解貌似是大暴搜? 首先我们考虑这是一个二分图,建立网络流模型后很容易得出一个算法 S->行 容量为Num[X]/2; 行->列 容量为1 且要求(x,y)这个点存在 列->T 容量 ...

  4. codeforces #305 C Mike and Foam

    首先我们注意到ai<=50w 因为2*3*5*7*11*13*17=510510 所以其最多含有6个质因子 我们将每个数的贡献分离, 添加就等于加上了跟这个数相关的互素对 删除就等于减去了跟这个 ...

  5. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

  6. (CodeForces 548B 暴力) Mike and Fun

    http://codeforces.com/problemset/problem/548/B Mike and some bears are playing a game just for fun. ...

  7. codeforces 361 E - Mike and Geometry Problem

    原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...

  8. codeforces 361 A - Mike and Cellphone

    原题: Description While swimming at the beach, Mike has accidentally dropped his cellphone into the wa ...

  9. codeforces 361 B - Mike and Shortcuts

    原题: Description Recently, Mike was very busy with studying for exams and contests. Now he is going t ...

随机推荐

  1. 新唐MCU常用的工具软件

    ICP   在电路编程  需要NULINK ISP   在系统编程,可通过串口或USB PINVIEW 可以显示管脚目前的状态.提供keil下或者单独运行两种模式.Keil下进入debug模式后,点击 ...

  2. Windows上使用telnet测试端口号

    之前测试服务器某一端口开启开启情况一般在服务器上使用  netstat –ano|findstr "端口号"命令查看. 但是有时候端口在服务器上开通了,但是客户端并不一定可以访问到 ...

  3. HTML使用CSS样式的方法

      在html网页中引入css样式表主要有一下四种方法 1.行内引入 <p ></p> 2.嵌入式 <style type="text/css"> ...

  4. 以太坊: ETH 发送交易 sendRawTransaction 方法数据的签名 和 验证过程

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  5. AbstractQueuedSynchronizer-AQS

    AbstractQueuedSynchronizer,这个类是整个java.util.concurrent的核心之一. CountDownLatch 能够使一个或多个线程等待其他线程完成各自的工作后再 ...

  6. 如何判断java对象是否为String数组

    if (entry.getValue() instanceof String[]) {// ko .................... }

  7. 如何查看MySQL单个数据库或者表的大小

    总体来说,这些信息存在于information_schema数据库的TABLES表中 mysql> desc information_schema.TABLES; +-------------- ...

  8. tensorflow 在windows下的安装

    anaconda3 python3.5 tensorflow 在 windows下的安装 1.安装Anaconda 清华的镜像:https://mirrors.tuna.tsinghua.edu.cn ...

  9. python处理u开头的字符串

    是用python处理excel过程中,从表格中解析除字符串,打印出来的中文却显示成了u'开头的乱码字符串,在控制台中输出的编码格式是utf-8,而excel表格的数据也是utf-8编码成的,但是解析成 ...

  10. adb 常用命名

    adb是Android Debug Bridge的简称, 就是起到调试桥的作用,用来操作android设备 adb help (显示帮助信息) adb devices (获取设备列表及设备状态) ad ...