CF1073G. Yet Another LCP Problem

选两组后缀$s,t$,求$\sum lcp(s_i,t_j)$

检验板子题

  • 字符串反转插入后缀自动机就是后缀树,记录下每个后缀的终止节点。
  • $lcp(i,j)$=$maxlen[lca(i,j)]$
  • 非常简单的树形$dp$,注意$i=j$的情况即可。
代码
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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 8e5 + 10;
const int mod = 1e9 + 7;

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
const int SN = 4e5 + 10;
const int SM = 26;
struct SAM
{
int trans[SN][SM], link[SN], pre, scnt, len[SN], siz[SN];
SAM() { pre = scnt = 1; }

void insert(int id)
{
int cur = ++scnt;
siz[cur] = 1;
len[cur] = len[pre] + 1;
int u;

for (u = pre; u && !trans[u][id]; u = link[u])
trans[u][id] = cur;
pre = cur;
if (!u)
link[cur] = 1;
else
{
int x = trans[u][id];
if (len[u] + 1 == len[x])
link[cur] = x;
else
{
int nc = ++scnt;
link[nc] = link[x];
len[nc] = len[u] + 1;
memcpy(trans[nc], trans[x], sizeof(trans[x]));
link[cur] = link[x] = nc;
for (; u && trans[u][id] == x; u = link[u])
trans[u][id] = nc;
}
}
}
} sam;
namespace Tree
{

int st[N][22], dfn[N], dep[N], tot, lg2[N];
vector<int> G[N];
void dfs(int x, int fa)
{
st[++tot][0] = x;
dep[x] = dep[fa] + 1;
dfn[x] = tot;
for (int to : G[x])
{
if (to == fa)
continue;
dfs(to, x);
st[++tot][0] = x;
}
}
int lower(int u, int v)
{
return (dep[u] < dep[v]) ? u : v;
}

void Lca_init()
{
dfs(1, 0);
for (int i = 2; i <= tot; i++)
lg2[i] = lg2[i >> 1] + 1;
for (int l = 1; (1 << l) <= tot; l++)
{
for (int i = 1; i + (1 << l) - 1 <= tot; i++)
st[i][l] = lower(st[i][l - 1], st[i + (1 << (l - 1))][l - 1]);
}
}
int lca(int u, int v)
{
u = dfn[u];
v = dfn[v];
if (u > v)
swap(u, v);
int i = lg2[v - u + 1], w = (1 << i);
return lower(st[u][i], st[v - w + 1][i]);
}
int dis(int u, int v)
{
int lc = lca(u, v);
return dep[u] + dep[v] - 2 * dep[lc];
}
}; // namespace Tree
using namespace Tree;

namespace XTree
{
stack<int> s;
vector<int> tmp;
int dp[N][2];
int b[N];
int vis1[N], vis0[N];
ll ans;
vector<pii> g[N];

bool cmp(int x, int y)
{
return Tree::dfn[x] < Tree::dfn[y];
}

void addEdge(int u, int v)
{
int d = Tree::dis(u, v);

g[u].push_back(mk(v, d));
g[v].push_back(mk(u, d));
}

void build(int k)
{

tmp.clear();
while (!s.empty())
s.pop();
sort(b + 1, b + 1 + k, cmp);
k = unique(b + 1, b + 1 + k) - b - 1;
s.push(1);
tmp.push_back(1);
for (int i = 1; i <= k; ++i)
{
int x = b[i];
tmp.push_back(x);
int lca = Tree::lca(x, s.top());
while (s.top() != lca)
{
int tmc = s.top();
s.pop();
if (dfn[s.top()] < dfn[lca])
s.push(lca), tmp.push_back(lca);
addEdge(s.top(), tmc);
}
s.push(x);
}
while (s.top() != 1)
{
int tmp = s.top();
s.pop();
addEdge(s.top(), tmp);
}
}
void clear()
{
for (int x : tmp)
{
g[x].clear();
dp[x][1] = dp[x][0] = 0;
}
}
void dfs1(int x, int fa, int d)
{
ll res = 0;

for (pii now : g[x])
{
int to = now.first;
int w = now.second;
if (to == fa)
continue;

dfs1(to, x, d + w);
if (vis0[x] != 0)
{
res += dp[to][1];
}
if (vis1[x] != 0)
{
res += dp[to][0];
}

res += 1ll * dp[to][1] * dp[x][0];
res += 1ll * dp[x][1] * dp[to][0];

dp[x][0] += dp[to][0];
dp[x][1] += dp[to][1];
}
if (vis0[x] != 0)
dp[x][0] += 1;
if (vis1[x] != 0)
dp[x][1] += 1;

ans += sam.len[x] * res;
}
void solve()
{

dfs1(1, 0, 0);
cout << ans << endl;
}

} // namespace XTree

char s[N];
int ed[N];
int main()
{
int n = read(), q = read();
scanf("%s", s + 1);
reverse(1 + s, 1 + s + n);
for (int i = 1; i <= n; i++)
{
sam.insert(s[i] - 'a');
ed[i] = sam.pre;
}
reverse(1 + ed, 1 + ed + n);

for (int i = 2; i <= sam.scnt; i++)
{
G[sam.link[i]].push_back(i);
}
Tree::Lca_init();

for (int i = 1; i <= q; i++)
{
int A = read(), B = read();
int cnt = 0;
XTree::ans = 0;
for (int j = 1; j <= A; j++)
{
int x = read();
x = ed[x];
XTree::vis1[x] = 1;
XTree::b[++cnt] = x;
}
for (int j = 1; j <= B; j++)
{
int x = read();
int tmp = x;
x = ed[x];
if (XTree::vis1[x])
XTree::ans += n - tmp + 1;
XTree::vis0[x] = 1;
XTree::b[++cnt] = x;
}
XTree::build(cnt);
XTree::solve();
XTree::clear();
for (int j = 1; j <= cnt; j++)
XTree::vis1[XTree::b[j]] = 0, XTree::vis0[XTree::b[j]] = 0;
}
}