-
Notifications
You must be signed in to change notification settings - Fork 0
/
2720.cpp
156 lines (152 loc) · 4.57 KB
/
2720.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <utility>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod = 998244353;
const int inf = 1 << 30;
const int maxn = 500000 + 5;
char s[maxn];
int n, q, dep[maxn];
namespace SegTrees {
const int maxm = maxn * 60;
int tot, root[maxn << 1], ls[maxm], rs[maxm], val[maxm];
void update(int i, int l, int r, int& rt) {
if (!rt) rt = ++tot, ls[rt] = rs[rt] = val[rt] = 0;
if (l == r) {
val[rt]++; return ;
}
int m = (l + r) / 2;
if (i <= m) update(i, l, m, ls[rt]);
else update(i, m + 1, r, rs[rt]);
val[rt] = val[ls[rt]] + val[rs[rt]];
}
int join(int x, int y, int l = 1, int r = n) {
if (!x || !y) return x + y;
int u = ++tot; ls[u] = rs[u] = val[u] = 0;
if (l == r) {
val[u] = val[x] + val[y];
} else {
int m = (l + r) / 2;
ls[u] = join(ls[x], ls[y], l, m);
rs[u] = join(rs[x], rs[y], m + 1, r);
val[u] = val[ls[u]] + val[rs[u]];
}
return u;
}
int query(int L, int R, int l, int r, int rt) {
if (!rt) return 0;
if (L <= l && r <= R) return val[rt];
int m = (l + r) / 2, ans = 0;
if (L <= m) ans = query(L, R, l, m, ls[rt]);
if (R > m) ans += query(L, R, m + 1, r, rs[rt]);
return ans;
}
}
using SegTrees::root;
struct sam {
int tot, last, cnt[maxn << 1], pos[maxn << 1];
int len[maxn << 1], link[maxn << 1], ch[maxn << 1][26];
void clear() {
tot = last = 1; ms(ch[1], 0);
}
int insert(int c, int i) {
int cur = ++tot, p = last;
ms(ch[cur], 0);
pos[cur] = i;
len[cur] = len[last] + 1;
cnt[cur] = 1;
for (; p && !ch[p][c]; p = link[p]) ch[p][c] = cur;
if (!p) link[cur] = 1;
else {
int q = ch[p][c];
if (len[p] + 1 == len[q]) link[cur] = q;
else {
int nq = ++tot;
len[nq] = len[p] + 1;
cnt[nq] = 0;
pos[nq] = pos[q];
link[nq] = link[q];
link[q] = link[cur] = nq;
memcpy(ch[nq], ch[q], sizeof ch[q]);
for (; ch[p][c] == q; p = link[p]) ch[p][c] = nq;
}
}
return last = cur;
}
int c[maxn << 1], a[maxn << 1];
void rsort() {
for (int i = 1; i <= tot; i++) c[i] = 0;
for (int i = 1; i <= tot; i++) c[len[i]]++;
for (int i = 1; i <= tot; i++) c[i] += c[i - 1];
for (int i = 1; i <= tot; i++) a[c[len[i]]--] = i;
}
void build() {
rsort();
for (int i = tot; i >= 2; i--) {
int u = a[i];
root[link[u]] = SegTrees::join(root[link[u]], root[u]);
}
}
ll cal() {
ll ans = 0;
for (int i = 1; i <= tot; i++) {
ans += max(0, len[i] - max(dep[pos[i]], len[link[i]]));
}
return ans;
}
} f, g;
int main() {
freopen("name.in", "r", stdin);
freopen("name.out", "w", stdout);
f.clear();
scanf("%s%d", s + 1, &q);
n = strlen(s + 1);
for (int i = 1; i <= n; i++) {
int u = f.insert(s[i] - 'a', i);
SegTrees::update(i, 1, n, root[u]);
}
f.build();
int ql, qr, c = 0;
while (q--) {
c++;
g.clear();
scanf("%s%d%d", s + 1, &ql, &qr);
int m = strlen(s + 1);
int u = 1, l = 0;
auto check = [&](int c) -> bool {
if (!f.ch[u][c]) return false;
int v = f.ch[u][c];
if (ql + l <= qr && SegTrees::query(ql + l, qr, 1, n, root[v])) {
return true;
} else {
return false;
}
};
for (int i = 1; i <= m; i++) {
int c = s[i] - 'a';
g.insert(c, i);
if (check(c)) u = f.ch[u][c], l++;
else {
while (u && !check(c)) {
if (l == 0) {
u = 0; break;
}
l--;
if (l == f.len[f.link[u]]) u = f.link[u];
}
if (!u) u = 1, l = 0;
else l++, u = f.ch[u][c];
}
dep[i] = l;
}
printf("%lld\n", g.cal());
}
return 0;
}