给出一个长度为 nn 的字符串 ssmm 个区间 [li,ri][l_i, r_i], 给定kk

qq次 询问. 每次给定一个长度为 kk 的字符串 ww 以及一对 L,RL,R, 求所有满足 i[L,R]i\in[L, R]w[li:ri]w[l_i:r_i]ss 中的出现次数之和.

n,m,k,q1×105n,m,k,q\le 1\times 10^5, w1×105\sum |w|\le 1\times 10^5

LOJ6031

Solution

因为kk一定,且w\sum|w|不大,考虑根号平衡

先对ss串建SAM,设kq=Tkq = T

  • kmk \le \sqrt m 时:

    因为kk很小,所有可能的区间[li,ri][l_i, r_i]只有k2k^2种,可以对每个询问暴力O(k2)O(k^2)枚举子串,丢到SAM上匹配求出对应right集合大小,再乘上这个子串对应的区间在[L,R][L, R]中的出现次数(用vector二分实现)即可

    复杂度是O(k2qlogn=O(Tmlogn)O(k^2q\log n = O(T\sqrt m\log n)

  • k>mk > \sqrt m时:

    此时询问很少,考虑对每个询问都暴力枚举[L,R][L, R]中的区间,然后倍增在SAM上匹配,复杂度是单词询问O(mlogn)O(m \log n)

    要想每次倍增求的话首先要对ww预处理出以每个点为右端点时,在SAM上匹配的节点,和最大匹配长度,这部分复杂度是单次询问O(n)O(n)

    总复杂度O(q(n+mlogn))=O(nm+Tmlogn)O\big(q(n + m\log n)\big) = O(n\sqrt m + T\sqrt m\log n)

Code

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <climits>
#include <queue>
#include <set>
#include <cmath>
#include <map>
#include <bitset>
#include <fstream>
#include <tr1/unordered_map>
#include <assert.h>

#define x first
#define y second
#define y0 Y0
#define y1 Y1
#define mp make_pair
#define pb push_back
#define DEBUG(x) cout << #x << " = " << x << endl;

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int MAXN = 1e5;
const int LIM = sqrt (MAXN);

int N, M, Q, K;
char S[MAXN + 5];

namespace SAM
{
const int MAXN = ::MAXN * 2;

struct info
{
int fa, ch[26], maxlen, size;
} node[MAXN + 5];

int node_cnt = 1, lst = 1;

inline int new_node (int pre)
{
int o = ++node_cnt;
node[o].maxlen = node[pre].maxlen + 1;
node[o].size = 1;
return o;
}

inline void extend (int c)
{
int o = new_node (lst), pre = lst; lst = o;

for (; pre && !node[pre].ch[c]; pre = node[pre].fa) node[pre].ch[c] = o;

if (!pre) node[o].fa = 1;
else
{
int x = node[pre].ch[c];
if (node[x].maxlen == node[pre].maxlen + 1) node[o].fa = x;
else
{
int y = ++node_cnt; node[y] = node[x], node[y].maxlen = node[pre].maxlen + 1;
node[y].size = 0, node[x].fa = node[o].fa = y;
for (; node[pre].ch[c] == x; pre = node[pre].fa) node[pre].ch[c] = y;
}
}
}

vector <int> G[MAXN + 5];

const int MAX_LOG = log2(MAXN);
int anc[MAX_LOG + 1][MAXN + 5];

inline void dfs (int x)
{
for (int i = 1; 1 << i <= N; ++i) anc[i][x] = anc[i - 1][anc[i - 1][x]];

for (int i = 0; i < G[x].size(); ++i)
{
int y = G[x][i];
dfs (y);
node[x].size += node[y].size;
}
}

inline void build ()
{
for (int i = 1; i <= N; ++i) extend (S[i] - 'a');
for (int i = 1; i <= node_cnt; ++i) G[node[i].fa].pb (i), anc[0][i] = node[i].fa;
dfs (1);
}

inline void match (char *S, int *O, int *Len)
{
int o = 1, len = 0;
for (int i = 1; i <= K; ++i)
{
int c = S[i] - 'a';
while (o > 1 && !node[o].ch[c]) o = node[o].fa, len = node[o].maxlen;

if (node[o].ch[c]) o = node[o].ch[c], O[i] = o, ++len;
else O[i] = 0;

Len[i] = len;
}
}

inline int get_anc (int o, int len)
{
for (int i = MAX_LOG; i >= 0; --i) if (node[anc[i][o]].maxlen >= len)
o = anc[i][o];
return o;
}
}

namespace S1
{
vector <int> vec[LIM + 5][LIM + 5];

inline void main ()
{
for (int i = 1; i <= M; ++i)
{
int l = read<int>() + 1, r = read<int>() + 1;
vec[l][r].pb (i);
}

while (Q--)
{
static char S[LIM + 5];
scanf("%s", S + 1);
int l = read<int>() + 1, r = read<int>() + 1;
LL ans = 0;
for (int i = 1; i <= K; ++i)
for (int j = i, o = 1; j <= K; ++j)
{
o = SAM :: node[o].ch[S[j] - 'a'];
if (!o) break;
int x = lower_bound (vec[i][j].begin(), vec[i][j].end(), l) - vec[i][j].begin();
int y = upper_bound (vec[i][j].begin(), vec[i][j].end(), r) - vec[i][j].begin() - 1;
ans += (LL) SAM :: node[o].size * max (0, y - x + 1);
}
printf("%lld\n", ans);
}
}
}

namespace S2
{
int L[MAXN + 5], R[MAXN + 5];

inline void main ()
{
for (int i = 1; i <= M; ++i) L[i] = read<int>() + 1, R[i] = read<int>() + 1;
while (Q--)
{
static char S[MAXN + 5];
scanf("%s", S + 1);
int x = read<int>() + 1, y = read<int>() + 1;

static int O[MAXN + 5], Len[MAXN + 5];
SAM :: match (S, O, Len);

// for (int i = 1; i <= K; ++i) cout << O[i] << ' ' ; puts("");

LL ans = 0;
for (int i = x; i <= y; ++i)
{
int l = L[i], r = R[i];
if (r - l + 1 > Len[r]) continue;
int o = SAM :: get_anc (O[r], r - l + 1);
// cout << l << ' ' << r << ' ' << o << ' ' << SAM :: node[o].maxlen << endl;
ans += SAM :: node[o].size;
}

printf("%lld\n", ans);
}
}
}

inline void Solve ()
{
SAM :: build ();
if (K <= LIM) S1 :: main ();
else S2 :: main ();
}

inline void Input ()
{
N = read<int>(), M = read<int>(), Q = read<int>(), K = read<int>();
scanf("%s", S + 1);
}

int main ()
{

#ifdef hk_cnyali
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif

Input ();
Solve ();

return 0;
}