#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 500000, maxc = 1000000, maxv = 30000000;
char s[maxn + 10];
int n, q, ch[maxc + 10][26], fa[maxc + 10], rt[maxn + 10];
int dfn[maxc + 10], ed[maxc + 10], dfscnt;
int maxl[maxc + 10], np = 1, ndcnt = 1, lim[maxc + 10];
bool islv[maxc + 10];
vector<int> g[maxc + 10];
__attribute__((optimize("-O3")))
void add(int c) {
int p = np; np = ++ndcnt; islv[np] = 1;
maxl[np] = maxl[p] + 1;
while (p && !ch[p][c]) {
ch[p][c] = np; p = fa[p];
}
if (p) {
int q = ch[p][c];
if (maxl[p] + 1 == maxl[q]) fa[np] = q;
else {
int nq = ++ndcnt; fa[nq] = fa[q];
fa[q] = fa[np] = nq; maxl[nq] = maxl[p] + 1;
for (int i = 0; i < 26; ++i) ch[nq][i] = ch[q][i];
while (ch[p][c] == q) {
ch[p][c] = nq; p = fa[p];
}
}
} else fa[np] = 1;
}
int lc[maxv + 10], rc[maxv + 10], sz[maxv + 10], vcnt;
__attribute__((optimize("-O3")))
void build(int &p, int ls, int rs) {
p = ++vcnt;
if (ls != rs) {
int mid = ls + rs >> 1;
build(lc[p], ls, mid); build(rc[p], mid + 1, rs);
}
}
__attribute__((optimize("-O3")))
void add(int &p, int cmp, int k, int ls, int rs) {
p = ++vcnt; lc[p] = lc[cmp]; rc[p] = rc[cmp]; sz[p] = sz[cmp] + 1;
if (ls != rs) {
int mid = ls + rs >> 1;
if (k <= mid) add(lc[p], lc[cmp], k, ls, mid);
else add(rc[p], rc[cmp], k, mid + 1, rs);
}
}
__attribute__((optimize("-O3")))
int query(int p, int l, int r, int ls, int rs) {
if (l > r) return 0;
if (l == ls && r == rs) return sz[p];
else {
int mid = ls + rs >> 1;
if (r <= mid) return query(lc[p], l, r, ls, mid);
else if (l > mid) return query(rc[p], l, r, mid + 1, rs);
else return query(lc[p], l, mid, ls, mid) + query(rc[p], mid + 1, r, mid + 1, rs);
}
}
__attribute__((optimize("-O3")))
void dfs(int p) {
dfn[p] = dfscnt;
if (islv[p]) {
++dfscnt; add(rt[dfscnt], rt[dfscnt - 1], maxl[p], 1, n);
}
for (int i = 0; i < g[p].size(); ++i) dfs(g[p][i]);
ed[p] = dfscnt;
}
namespace anothersam {
int n, q, ch[maxc * 2 + 10][26], fa[maxc * 2 + 10], maxl[maxc * 2 + 10], np, ndcnt;
int pos[maxc * 2 + 10];
__attribute__((optimize("-O3")))
void init() {
for (int i = 1; i <= ndcnt; ++i) {
for (int j = 0; j < 26; ++j) ch[i][j] = 0;
fa[i] = maxl[i] = pos[i] = 0;
}
np = ndcnt = 1;
}
__attribute__((optimize("-O3")))
void add(int c) {
int p = np; np = ++ndcnt;
maxl[np] = pos[np] = maxl[p] + 1;
while (p && !ch[p][c]) {
ch[p][c] = np; p = fa[p];
}
if (p) {
int q = ch[p][c];
if (maxl[p] + 1 == maxl[q]) fa[np] = q;
else {
int nq = ++ndcnt; fa[nq] = fa[q]; pos[nq] = pos[q];
fa[q] = fa[np] = nq; maxl[nq] = maxl[p] + 1;
for (int i = 0; i < 26; ++i) ch[nq][i] = ch[q][i];
while (ch[p][c] == q) {
ch[p][c] = nq; p = fa[p];
}
}
} else fa[np] = 1;
}
__attribute__((optimize("-O3")))
ll calc() {
ll ans = 0;
for (int i = 2; i <= ndcnt; ++i)
ans += max(0, maxl[i] - max(maxl[fa[i]], lim[pos[i]]));
return ans;
}
}
__attribute__((optimize("-O3")))
int main() {
scanf("%s", s + 1); n = strlen(s + 1);
for (int i = 1; i <= n; ++i) add(s[i] - 'a');
for (int i = 2; i <= ndcnt; ++i) g[fa[i]].push_back(i);
build(rt[0], 1, n);
dfs(1);
scanf("%d", &q);
while (q--) {
int l, r, p = 1, nowl = 0;
scanf("%s%d%d", s + 1, &l, &r); int len = strlen(s + 1);
anothersam::init();
for (int i = 1; i <= len; ++i) {
int c = s[i] - 'a';
anothersam::add(c);
while (1) {
if (ch[p][c]) {
int e = ch[p][c];
if (query(rt[ed[e]], l + nowl, r, 1, n) - query(rt[dfn[e]], l + nowl, r, 1, n)) {
++nowl; p = e; break;
}
}
if (nowl == 0) break;
--nowl; if (nowl == maxl[fa[p]]) p = fa[p];
}
lim[i] = nowl;
}
printf("%lld\n", anothersam::calc());
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 6.956 ms | 23 MB + 72 KB | Accepted | Score: 4 | 显示更多 |
Testcase #2 | 8.501 ms | 23 MB + 356 KB | Accepted | Score: 4 | 显示更多 |
Testcase #3 | 8.688 ms | 23 MB + 360 KB | Accepted | Score: 4 | 显示更多 |
Testcase #4 | 63.054 ms | 23 MB + 800 KB | Accepted | Score: 4 | 显示更多 |
Testcase #5 | 60.416 ms | 23 MB + 796 KB | Accepted | Score: 4 | 显示更多 |
Testcase #6 | 763.81 ms | 337 MB + 568 KB | Accepted | Score: 4 | 显示更多 |
Testcase #7 | 763.987 ms | 337 MB + 452 KB | Accepted | Score: 4 | 显示更多 |
Testcase #8 | 116.747 ms | 68 MB + 428 KB | Accepted | Score: 4 | 显示更多 |
Testcase #9 | 176.173 ms | 64 MB + 144 KB | Accepted | Score: 4 | 显示更多 |
Testcase #10 | 262.629 ms | 112 MB + 892 KB | Accepted | Score: 4 | 显示更多 |
Testcase #11 | 436.693 ms | 107 MB + 336 KB | Accepted | Score: 4 | 显示更多 |
Testcase #12 | 446.123 ms | 157 MB + 944 KB | Accepted | Score: 4 | 显示更多 |
Testcase #13 | 724.249 ms | 152 MB + 176 KB | Accepted | Score: 4 | 显示更多 |
Testcase #14 | 620.851 ms | 204 MB + 644 KB | Accepted | Score: 4 | 显示更多 |
Testcase #15 | 1.036 s | 198 MB + 500 KB | Accepted | Score: 4 | 显示更多 |
Testcase #16 | 807.209 ms | 251 MB + 492 KB | Accepted | Score: 4 | 显示更多 |
Testcase #17 | 1.355 s | 245 MB + 132 KB | Accepted | Score: 4 | 显示更多 |
Testcase #18 | 901.183 ms | 121 MB + 104 KB | Accepted | Score: 4 | 显示更多 |
Testcase #19 | 1.056 s | 163 MB + 572 KB | Accepted | Score: 4 | 显示更多 |
Testcase #20 | 1.222 s | 207 MB + 208 KB | Accepted | Score: 4 | 显示更多 |
Testcase #21 | 1.285 s | 251 MB + 596 KB | Accepted | Score: 4 | 显示更多 |
Testcase #22 | 1.375 s | 251 MB + 532 KB | Accepted | Score: 4 | 显示更多 |
Testcase #23 | 1.373 s | 251 MB + 460 KB | Accepted | Score: 4 | 显示更多 |
Testcase #24 | 1.335 s | 251 MB + 568 KB | Accepted | Score: 4 | 显示更多 |
Testcase #25 | 1.303 s | 251 MB + 604 KB | Accepted | Score: 4 | 显示更多 |