CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)

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 5aababababababb1 5 13 5 11 5 21 5 31 4 5

Output

75636

题意:给定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]]的数量。

原文地址:https://www.cnblogs.com/hua-dong/p/9249988.html

时间: 2024-07-29 08:59:17

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

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

题面 传送门 正文 最暴力的 最暴力的方法:把所有询问代表的字符串跑一遍kmp然后输出 稍微优化一下:把所有询问保存起来,把模板串相同的合并,求出next然后匹配 但是这两种方法本质没有区别,都是暴力 不那么暴力的 我们对于所有的串建立一个AC自动机,把询问按照$y$排序,然后在AC自动机上面跑,每次跳fail更新答案 这样可以拿到70分,但是时间上限还是会$O\left(n^2\right)$左右 巧妙的优化 这道题里面,所有的模板串和文本串都在AC自动机里 那么,题目中实际是在要求什么呢?

【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 whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. 输入 The first line contains one integer n (1 <= n <

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

一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状数组维护, DFS到的查询点就回答询问.时间复杂度O(|ACAM|+QlogQ) ------------------------------------------------------------------------------------------- #include<cstdio>

[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

[NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: l 输入小写字母,打字机的一个凹槽中会加入这个字母(这个字母加在凹槽的最后).l 按一下印有'B'的按键,打字机凹槽中最后一个字母会消失.l 按一下印有'P'的按键,打字机会在纸上打印出凹槽中现有的所有字母并换行,但凹槽中的字母不会消失.例如,阿狸输入aPaPBbP

[COCI2015] Divljak - AC自动机,DFS序,树状数组,LCA

有 \(n\) 个询问串 \(S_i\),有一个初始为空的字符串集合 \(T\),接下来有 \(q\) 个操作,每次向集合中添加一个字符串,或给定 \(x\) 询问集合中有多少个字符串包含 \(S_x\) Solution 考虑对 \(S_i\) 建立 ACAM,建出 \(fail\) 树,一个点发生匹配,则需要修改它到根的链,询问就是查询一个点的值 这样需要树链剖分,我们可以差分一下,那么每次修改的就是一个点,询问的是一个子树的和 于是我们求出 \(fail\) 树的 DFS 序,用树状数组维

BZOJ 3439: Kpm的MC密码( trie + DFS序 + 主席树 )

把串倒过来插进trie上, 那么一个串的kpm串就是在以这个串最后一个为根的子树, 子树k大值的经典问题用dfs序+可持久化线段树就可以O(NlogN)解决 ------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<cctype> using namespa

BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )

子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 ------------------------------------------------------------------- #include<bits/stdc++.h> using namespace std; #define M(l, r) (((l) + (r)) >> 1) const int maxn = 200009; typedef long long ll; inline ll

PT07J - Query on a tree III DFS序 + 主席树

dfs序编号后跑权值主席树 但写起来是真的麻烦,总是wa,只能拿出模板过了 #include<bits/stdc++.h> const int N = 100001; using namespace std; struct node { int to, ne; } e[N<<1]; int n, m, len, l1, l2; int id[N], rk[N], eid[N], re[N]; int he[N], a[N], b[N]; int val[N << 5],

bzoj4771 七彩树 dfs序+主席树+树链的并

题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4771 题解 一道不错的树链并的基础练习题. 如果不是树,而是一个数组的话,对于给定区间内的不同颜色数,我们可以维护一个 \(pre_{i, j}\) 表示前 \(i\) 个最后一个 \(j\) 出现的位置.那么答案就是 \(pre_{r, j} \geq l\) 的 \(j\) 的个数,用主席树维护,第一维 \(i\),第二维 \(pre_i, j\),维护的是数量就可以了.(当然这个问题还