一个长度为 nn 的小写字母串

mm 次询问,每次给出 l,rl, r 表示将 [l,r][l, r] 内的字母转化成其对应的大写字母,然后求整个串的本质不同子串数

n,m2×105n, m \le 2 \times 10^5

Solution

一题复习三个算法一个套路,血赚。

若一个串中既有大写字母,又有小写字母,那这个串只会出现一次,可以直接计算。于是只需要统计只由大写/小写字母构成的串,即每次对区间 [l,r][l, r] 统计:

  1. [1,l1][r+1,n][1, l - 1] \cup [r + 1, n] 内本质不同子串个数

  2. [l,r][l, r] 内本质不同子串个数

以下对这两种情况分别讨论

Part1

首先进行一步转化: 用 全串的本质不同子串数 减去 完全被覆盖在 [l, r] 中的本质不同子串数

考虑某个子串串T对询问的贡献:

20-7-23-1

上图表示 T 串在 S 中的所有出现位置

则当 lLl \le LRrR \le r 时,该串会被完全覆盖在 [l,r][l, r]

下面考虑SAM上的某个节点对询问的贡献:

20-7-23-2

则当 lLl \le LR1r<R2R_1 \le r < R_2 时,该点会对答案贡献1

而当 lLl \le LR2r<R3R_2 \le r < R_3 时,该点会对答案贡献2,以此类推

考虑扫描线,把询问挂在左端点,并从右往左扫。每遇到一个 LL 就把对应的 [R1,Rk][R_1, R_k] 区间加一。查询只需统计 [1,r][1, r] 的和即可

通过在SAM上维护right集合中最大最小的位置即可求出L,RL, R。用树状数组即可实现区间加,区间求和

Part2

from https://www.cnblogs.com/chenxiaoran666/p/Luogu6292.html

20-7-23-3

即考虑每次的增量。与维护区间颜色个数类似,对于每种颜色,在它当前最后一次出现位置+1,查询即统计后缀和

此处类似,只不过一个节点对答案的贡献是一段区间 (树状数组区间加区间查即可),且需要把很多后缀 (即parent链上所有串) 的值都修改成当前位置 (类似树点涂色,把当前点到根的路径都染成同一种颜色,复杂度就对了)

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 6.6K after deleting the debug information 
#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 = 2e5;

int N, M, type;
char S[MAXN + 5];
pii Q[MAXN + 5];
vector <pii> query[MAXN + 5];
vector <int> nds[MAXN + 5];
LL Ans[MAXN + 5];

namespace BIT
{
struct bit
{
LL sum[MAXN + 5];
inline void add (int x, int val) { for (; x <= N; x += x & (-x)) sum[x] += val; }
inline LL query (int x) { LL ans = 0; for (; x; x -= x & (-x)) ans += sum[x]; return ans; }
} A, B;

inline void init () { memset (A.sum, 0, sizeof A.sum); memset (B.sum, 0, sizeof B.sum); }

inline void update (int x, int y, LL val)
{
if (x > y) return ;
A.add (x, val * x), A.add (y + 1, -val * (y + 1));
B.add (x, val), B.add (y + 1, -val);
}

inline LL query (int x) { return B.query (x) * (x + 1) - A.query (x); }

inline LL query (int x, int y) { if (x > y) return 0; return query (y) - query (x - 1); }
};

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

int node_cnt, lst;

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

int O[MAXN + 5];

LL ans;

inline void init ()
{
for (int i = 1; i <= node_cnt; ++i)
{
node[i].maxlen = node[i].fa = 0;
memset (node[i].ch, 0, sizeof node[i].ch);
}
node_cnt = lst = 1;
}

inline int new_node (int p, int id)
{
int o = ++node_cnt;
node[o].maxlen = node[p].maxlen + 1;
node[o].mn = node[o].mx = id;
return o;
}

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

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

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

ans += node[o].maxlen - node[node[o].fa].maxlen;

O[id] = o;
}

vector <int> G[MAXN + 5];

inline void dfs (int x)
{
for (int i = 0; i < G[x].size(); ++i)
{
int y = G[x][i];
dfs (y);
Chkmin (node[x].mn, node[y].mn);
Chkmax (node[x].mx, node[y].mx);
}
}

inline void build ()
{
init ();

for (int i = 1; i <= N; ++i) extend (S[i] - 'a', i);

for (int i = 2; i <= node_cnt; ++i) G[node[i].fa].pb (i);

dfs (1);

for (int i = 2; i <= node_cnt; ++i) nds[node[i].mn].pb (i);
}

inline void modify (int x)
{
int p = node[x].mx;
BIT :: update (p - node[x].maxlen + 1, p - node[node[x].fa].maxlen, 1);
}
}

using SAM :: O;

namespace LCT
{
#define fa(o) node[o].f
#define ls(o) node[o].ch[0]
#define rs(o) node[o].ch[1]
const int MAXN = SAM :: MAXN;

struct info
{
int ch[2], f;
int minlen, tag, lst;
} node[MAXN + 5];

inline int is_root (int o) { return (ls(fa(o)) != o) && (rs(fa(o)) != o); }

inline int chk (int o) { return rs(fa(o)) == o; }

inline void connect (int o, int f, int d) { node[f].ch[d] = o; fa(o) = f; }

inline void init ()
{
for (int i = 1; i <= SAM :: node_cnt; ++i)
{
fa(i) = SAM :: node[i].fa;
node[i].minlen = SAM :: node[fa(i)].maxlen + 1;
}
}

inline void apply (int o, int val) { node[o].lst = node[o].tag = val; }

inline void push_up (int o)
{
node[o].minlen = SAM :: node[SAM :: node[o].fa].maxlen + 1;
if (ls(o)) Chkmin (node[o].minlen, node[ls(o)].minlen);
if (rs(o)) Chkmin (node[o].minlen, node[rs(o)].minlen);
}

inline void push_down (int o)
{
if (!node[o].tag) return ;
if (ls(o)) apply (ls(o), node[o].tag);
if (rs(o)) apply (rs(o), node[o].tag);
node[o].tag = 0;
}

inline void rotate (int x)
{
int f = fa(x), anc = fa(f), dx = chk(x), df = chk(f);

if (!is_root(f)) node[anc].ch[df] = x; fa(x) = anc;
connect (node[x].ch[!dx], f, dx);
connect (f, x, !dx);

push_up (f), push_up (x);
}

inline void splay (int o)
{
static int stk[MAXN + 5]; int top = 0;

stk[++top] = o; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for (int x = o; !is_root (x); x = fa(x)) stk[++top] = fa(x);
for (; top; --top) push_down (stk[top]);

for (int x = o; !is_root (x); rotate (x)) if (!is_root(fa(x)))
rotate (chk(x) == chk(fa(x)) ? fa(x) : x);
}

inline void access (int o, int id)
{
for (int x = o, y = 0; x; y = x, x = fa(x))
{
splay (x);
if (node[x].lst) BIT :: update (node[x].lst - SAM :: node[x].maxlen + 1, node[x].lst - node[x].minlen + 1, -1);
rs(x) = y, push_up (x);
}

splay (o), apply (o, id);

BIT :: update (node[o].lst - SAM :: node[o].maxlen + 1, node[o].lst - node[o].minlen + 1, 1);

}
}

inline void Solve ()
{
// sub 1

SAM :: build ();

for (int i = N; i >= 1; --i)
{
for (int x : nds[i]) SAM :: modify (x);
for (pii t : query[i]) Ans[t.y] += SAM :: ans - BIT :: query (1, t.x);
query[i].clear();
}

// sub 2

BIT :: init (), LCT :: init ();
for (int i = 1; i <= M; ++i) query[Q[i].y].pb (mp (Q[i].x, i));

for (int i = 1; i <= N; ++i)
{
LCT :: access (O[i], i);
for (pii t : query[i])
{
if (type == 1) Ans[t.y] += i - t.x + 1;
else Ans[t.y] += BIT :: query (t.x, i);
}
}

for (int i = 1; i <= M; ++i) printf("%lld\n", Ans[i]);
}

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

M = read<int>();
for (int i = 1; i <= M; ++i)
{
int x = read<int>(), y = read<int>();
Q[i] = mp (x, y);
Ans[i] = (LL) (y - x + 1) * (x - 1 + N - y);
Ans[i] += (LL) (x - 1) * (N - y);
query[x].pb (mp (y, i));
}
}

int main ()
{

freopen("string.in", "r", stdin);
freopen("string.out", "w", stdout);

Input ();
Solve ();

return 0;
}