#include <cstdio>
#include <queue>
#include <vector>
#define pb emplace_back
using namespace std; int read() {
int x = 0;
char c = getchar();
while (c < 48) c = getchar();
while (c > 47) x = x * 10 + (c - 48), c = getchar();
return x;
} const int maxn = 2e5 + 52;
const int maxq = 5e5 + 55; int n, m, len = 0;
char s[maxn];
int fa[maxn], ch[maxn][26], fail[maxn], cnt = 1;
int ed[maxn]; void rs() {
len = 0;
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
while (c != ' ' && c != '\n') s[len++] = c, c = getchar();
} int ins() {
int p = 1;
for (int i = 0; i < len; i++) {
int c = s[i] - 'a';
if (!ch[p][c]) ch[p][c] = ++cnt, fa[ch[p][c]] = p;
p = ch[p][c];
}
return p;
} std ::vector<int> g[maxn];
int sz[maxn], dfn[maxn], idx = 0;
void dfs(int u) {
sz[u] = 1, dfn[u] = ++idx;
for (int v : g[u]) dfs(v), sz[u] += sz[v];
} using pii = pair<int, int>;
std ::vector<pii> que[maxn]; int c[maxn];
int low(int x) { return x & -x; }
void add(int x, int y) {
for (; x <= cnt; x += low(x)) c[x] += y;
}
int qry(int x) {
int ans = 0;
for (; x; x ^= low(x)) ans += c[x];
return ans;
} int main() {
// freopen("testdata.in", "r", stdin);
n = read(), m = read();
for (int i = 1; i <= n; i++) ed[i] = (rs(), ins());
std ::queue<int> q;
for (int i = 0; i < 26; i++)
if (ch[1][i])
fail[ch[1][i]] = 1, q.push(ch[1][i]);
else
ch[1][i] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < 26; i++)
if (ch[u][i])
fail[ch[u][i]] = ch[fail[u]][i], q.push(ch[u][i]);
else
ch[u][i] = ch[fail[u]][i];
}
for (int i = 2; i <= cnt; i++) g[fail[i]].pb(i);
dfs(1); std ::vector<int> ans(m + 1, 0);
for (int i = 1; i <= m; i++) {
int l = read(), r = read(), k = read();
--l, que[l].pb(-i, k), que[r].pb(i, k);
} for (int i = 1; i <= n; i++) {
int x = ed[i];
while (x) {
add(dfn[x], 1);
x = fa[x];
}
for (auto x : que[i]) {
int sign = x.first > 0 ? 1 : -1, u = ed[x.second], id = x.first * sign;
ans[id] += sign * (qry(dfn[u] + sz[u] - 1) - qry(dfn[u] - 1));
}
} for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}

CF547E Mike and Friends [AC自动机,离线树状数组]的更多相关文章

  1. 【BZOJ2434】阿狸的打字机(AC自动机,树状数组)

    [BZOJ2434]阿狸的打字机(AC自动机,树状数组) 先写个暴力: 每次打印出字符串后,就插入到\(Trie\)树中 搞完后直接搭\(AC\)自动机 看一看匹配是怎么样的: 每次沿着\(AC\)自 ...

  2. 【BZOJ2434】【NOI2011】阿狸的打字机(AC自动机,树状数组)

    [BZOJ2434]阿狸的打字机(AC自动机,树状数组) 先写个暴力: 每次打印出字符串后,就插入到\(Trie\)树中 搞完后直接搭\(AC\)自动机 看一看匹配是怎么样的: 每次沿着\(AC\)自 ...

  3. Codeforces 163E(ac自动机、树状数组)

    要点 显然ac自动机的板子就可以暴力一下答案了 为了优化时间复杂度,考虑套路fail树的dfs序.发现本题需要当前这个尾点加上所有祖先点的个数,考虑使用树状数组差分一下,在父点+1,在子树后-1,每次 ...

  4. BZOJ 2434 Luogu P2414 [NOI2011]阿狸的打字机 (AC自动机、树状数组)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2434 题解: 我写的是离线做法,不知道有没有在线做法. 转化一波题意,\(x\)在AC ...

  5. BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机、树状数组)

    吐槽: 为啥很多人用AC自动机暴力跳都过了?复杂度真的对么? 做法一: AC自动机+树状数组 姓名的问题,中间加个特殊字符连起来即可. 肯定是对点名串建AC自动机(map存儿子),然后第一问就相当于问 ...

  6. P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]

    题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...

  7. Hdu-3333 Turning Tree (离线树状数组/线段树)

    Hdu-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有多重解法.我另一篇博客写了分块的解法  HDU-3333 Turing ...

  8. 洛谷 P1972"[SDOI2009]HH的项链"(离线+树状数组 or 在线+主席树)

    传送门 •题意 给你一个包含 n 个数的数组 $a$: 有 m 此操作,每次操作求区间 [l,r] 中不同数的个数: •题解(离线+树状数组) 以样例 $[1,2,3,4,3,5]$ 为例,求解区间 ...

  9. POJ 3416 Crossing --离线+树状数组

    题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少 ...

  10. HDU 2852 KiKi's K-Number(离线+树状数组)

    题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. 《Kubernetes权威指南》01_Kubernetes入门——Kubernetes 是什么

    01_Kubernetes入门 li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style ...

  2. Spring注解开发系列Ⅲ --- 生命周期

    Bean的生命周期 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解. 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: S ...

  3. centos7 手动设置时间

    date -s "2020-02-03 23:13:00" hwclock -w clock -w

  4. Linux学习笔记-Centos7搭建owncloud私有云盘

    使用环境:虚拟机centos7 1.下载安装LAMP相关软件 [root@localhost yum.repos.d]# yum install httpd –y [root@localhost yu ...

  5. Arduino系列之pwm控制LED灯(呼吸灯)

    下面我将写出最简单控制呼吸灯的方法 void setup()                                 // { pinMode(12,OUTPUT);             ...

  6. 读取sysTreeview32和SysListView32

    #include <stdio.h> #include <windows.h> #include <commctrl.h> int main(void) { /* ...

  7. Webpack 一,打包JS

    创建入口文件 app.js // es6 module 规范 import sum_d from './sum.js' import {sum_e} from './sum.js' // commco ...

  8. Codeforces_723_B

    http://codeforces.com/problemset/problem/723/B 求括号内单词数和括号外最大单词长度,注意细心,尤其是ok和sum的置0. #include<iost ...

  9. sqlserver check running process 1

    check process script 1, check which is current running: use master SELECTspid,ER.percent_complete,CA ...

  10. Go语言实现:【剑指offer】正则表达式匹配

    该题目来源于牛客网<剑指offer>专题. 请实现一个函数用来匹配包括 . 和 * 的正则表达式.模式中的字符.表示任意一个字符,而 * 表示它前面的字符可以出现任意次(包含0次). 在本 ...