What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!

In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n.

Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).

Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF.

The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number

Input

The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105).

The next n lines contain the phone numbers, i-th line contains a string siconsisting of lowercase English letters ().

The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).

Output

Print the answer for each question in a separate line.

Examples

Input
5 5
a
ab
abab
ababab
b
1 5 1
3 5 1
1 5 2
1 5 3
1 4 5
Output
7
5
6
3
6

题意:给定N个串,M次询问,每次询问[L,R]里含多少个X。

思路:(第一次做,结合主席树那里还是不太好想)。对N个串建立AC自动机,建立Fail树,然后得到DFS序。那么,对于每个串S,假设其长度为L,S在AC自动机上面跑,其每个前缀Si在AC自动机上面得到最大深度,对应的Fail树位置,贡献加1,保证这个贡献在Si的后缀的子树里(比如abcdef,那么跑到abcd时,在fail树上面对应的位置贡献加1 ,对于后缀abcd,bcd,cd,d的子树都含这个贡献)。

关键是如何出现子树来自于[L,R]的贡献。开始我以为以dfs序为X轴,以来自的串为Y轴建立主席树,查询的时候查询区间[in[u],out[u]],即子树里关键字在[L,R]里的个数。但是发现没有转移关系。所以想不走了。

看了其他人的代码,是以每个串,以来自的串为X轴(从长的前缀到短的前缀),以dfs序为X轴,保证了前缀和的正确性,查询的时候查询区间[L-1,R]的区间在[in[[u],out[u]]的数量。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int ch[maxn][],fa[maxn],cnt=; //trie树
int Laxt[maxn],Next[maxn],To[maxn],tot; //fail树
int q[maxn],fail[maxn],head,tail; //fail树
int in[maxn],out[maxn],pos[maxn],times;//dfs序
int p[maxn],rt[maxn],cur,num; struct in{int l,r,sum;}s[maxn*];
char c[maxn];
void addedge(int u,int v){ Next[++tot]=Laxt[u]; Laxt[u]=tot; To[tot]=v; }
int insert()
{
int Now=; for(int i=;c[i];i++){
if(!ch[Now][c[i]-'a']){
ch[Now][c[i]-'a']=++cnt;
fa[cnt]=Now;
}
Now=ch[Now][c[i]-'a'];
} return Now;
}
void buildfail()
{
for(int i=;i<;i++){
if(ch[][i]) q[++head]=ch[][i],fail[ch[][i]]=;
else ch[][i]=;
}
while(tail<head){
int Now=q[++tail];
for(int i=;i<;i++){
if(ch[Now][i]) {
q[++head]=ch[Now][i]; fail[ch[Now][i]]=ch[fail[Now]][i];
}
else ch[Now][i]=ch[fail[Now]][i];
}
}
for(int i=;i<=cnt;i++) addedge(fail[i],i);
}
void dfs(int u)
{
in[u]=++times;
for(int i=Laxt[u];i;i=Next[i]) dfs(To[i]);
out[u]=times;
}
void add(int &Now,int pre,int L,int R,int pos)
{
Now=++num; s[Now]=s[pre]; s[Now].sum++;
if(L==R) return ; int Mid=(L+R)>>;
if(pos<=Mid) add(s[Now].l,s[pre].l,L,Mid,pos);
else add(s[Now].r,s[pre].r,Mid+,R,pos);
}
int query(int pre,int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return s[Now].sum-s[pre].sum;
int res=,Mid=(L+R)>>;
if(l<=Mid) res+=query(s[pre].l,s[Now].l,L,Mid,l,r);
if(r>Mid) res+=query(s[pre].r,s[Now].r,Mid+,R,l,r);
return res;
}
int main()
{
int N,M,Q,L,R,x,i,j;
scanf("%d%d",&N,&Q);
for(i=;i<=N;i++){
scanf("%s",c+);
pos[i]=insert();
}
buildfail();
dfs(); cur=;
for(i=;i<=N;i++){
for(j=pos[i];j;j=fa[j]){
cur++;
add(rt[cur],rt[cur-],,times,in[j]);
}
p[i]=rt[cur];
}
while(Q--){
scanf("%d%d%d",&L,&R,&x);
printf("%d\n",query(p[L-],p[R],,times,in[pos[x]],out[pos[x]]));
}
return ;
}

CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)的更多相关文章

  1. [NOI2011][bzoj2434] 阿狸的打字机 [AC自动机+dfs序+fail树+树状数组]

    题面 传送门 正文 最暴力的 最暴力的方法:把所有询问代表的字符串跑一遍kmp然后输出 稍微优化一下:把所有询问保存起来,把模板串相同的合并,求出next然后匹配 但是这两种方法本质没有区别,都是暴力 ...

  2. CodeForces 547E Mike and Friends AC自动机 主席树

    题意: 给出\(n\)个字符串\(s_i\)和\(q\)个询问: \(l,r,k\):\(\sum\limits_{i=l}^{r}count(i, k)\),其中\(count(i,j)\)表示\( ...

  3. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  4. 2018.09.30 bzoj3551:Peaks加强版(dfs序+主席树+倍增+kruskal重构树)

    传送门 一道考察比较全面的题. 这道题又用到了熟悉的kruskal+倍增来查找询问区间的方法. 查到询问的子树之后就可以用dfs序+主席树统计答案了. 代码: #include<bits/std ...

  5. dfs序+主席树 或者 树链剖分+主席树(没写) 或者 线段树套线段树 或者 线段树套splay 或者 线段树套树状数组 bzoj 4448

    4448: [Scoi2015]情报传递 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 588  Solved: 308[Submit][Status ...

  6. 【bzoj3545/bzoj3551】[ONTAK2010]Peaks/加强版 Kruskal+树上倍增+Dfs序+主席树

    bzoj3545 题目描述 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询 ...

  7. 【bzoj1803】Spoj1487 Query on a tree III DFS序+主席树

    题目描述 You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node w ...

  8. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  9. BZOJ2434[Noi2011]阿狸的打字机——AC自动机+dfs序+树状数组

    题目描述 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小 ...

随机推荐

  1. EMC机理------串扰

    转:电子工程师不得不知道的EMC机理------串扰(韬略科技EMC) 串扰是信号完整性中最基本的现象之一,在板上走线密度很高时串扰的影响尤其严重.我们知道,线性无缘系统满足叠加定理,如果受害线上有信 ...

  2. *Android 多线程下载 仿下载助手

    今天带来一个多线程下载的 样例.先看一下效果.点击 下载 開始下载,同一时候显示下载进度.完成下载,变成程 安装,点击安装 提示 安装应用. 界面效果 线程池 ThreadPoolExecutor , ...

  3. NGINX配置文件优化示例

    Nginx主配置文件 upstream.conf配置文件 # server nginx配置文件最好分开写,不要把所有的逻辑都放在一个文件里面,会看着很麻烦,,之前我的配置文件都放一起了,,导致现在维护 ...

  4. ReactNative Navigator

    https://facebook.github.io/react-native/docs/navigator.html Navigator实现了页面之间的跳转. Demo描述:打开即进入“课程”页面, ...

  5. 关于msbuild 编译.net 4.5新语法错误的解决方法

    .net4.5以前msbuild 是在%windir%/Microsoft.NET/FrameworkXX/vXX目录下,如:C:\Windows\Microsoft.NET\Framework64\ ...

  6. 三分钟教你学Git(十二) 之 fast-forward

    什么是fast forward, 顾名思义,就是高速向前进,Git怎么做到高速的呢? 原来假设Git判定能够fast forward的时候,直接改动当前HEAD指针的指向然后再改动当前HEAD指针.说 ...

  7. 搭建sftp服务+nginx代理

    在公司,经常会用到sftp服务,比如两个公司对接生产项目,其中一方,要在sftp上上传pdf文件,另一方公司要在sftp服务器上用nginx代理直接下载pdf文件.下面就说说我在实际中应用到的sftp ...

  8. 【BZOJ1835】[ZJOI2010]base 基站选址 线段树+DP

    [BZOJ1835][ZJOI2010]base 基站选址 Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯 ...

  9. 九度OJ 1162:I Wanna Go Home(我想回家) (最短路径)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:870 解决:415 题目描述: The country is facing a terrible civil war----cities i ...

  10. 九度OJ 1082:代理服务器 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1871 解决:574 题目描述: 使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私.我们知道n个代理服务器的IP地 ...