[Codeforces504E] Misha and LCP on Tree

做法

作重链剖分,然后求出重链剖分后的 dfs 序。

一条链可以拆成 dfs 序上 \(\mathcal O(\log n)\) 个区间。

按 dfs 序把每个点的字符排成一个字符串 \(s\),预先建一个能 \(\mathcal O(1)\) 询问 \(s\) 的任意两个后缀 lcp,询问 \(s^R\) 任意两个后缀 lcp 和询问一个 \(s\) 的后缀和 \(s^R\) 的一个后缀的 lcp 的数据结构。可以对 \(ss^R\) 建树状数组,因为我不擅长写树状数组,所以我对它们用 SAM 求出了后缀树,然后写了一个 \(\mathcal O(n\log n) - \mathcal O(1)\) lca。

对于一个询问,一条链,可以看成 \(\mathcal O(\log n)\) 个区间拼起来,所以就变成了两组若干个区间拼起来的串要求 lcp,利用预处理的信息从前往后依次求就行了。

时间复杂度 \(\mathcal O(n \log n)\)

代码

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
#include <bits/stdc++.h>

using namespace std;

const int maxn = 300010;

int n, q, l[maxn], e;
int son[maxn], sz[maxn], top[maxn], dep[maxn];
int dfn[maxn], idfn[maxn], fa[maxn], tim;
char str[maxn];

struct Edge {
int v, x;
} E[maxn<<1];

inline void addEdge(int u, int v) {
E[e].v = v, E[e].x = l[u], l[u] = e++;
}

void dfs1(int u, int f) {
sz[u] = 1; fa[u] = f;
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (v != f) {
dep[v] = dep[u] + 1;
dfs1(v, u);
sz[u] += sz[v];
if (!son[u] || sz[v] > sz[son[u]]) son[u] = v;
}
}
}

void dfs2(int u, int t) {
top[u] = t; dfn[u] = ++ tim; idfn[tim] = u;
if (son[u]) dfs2(son[u], t);
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (v != fa[u] && v != son[u]) {
dfs2(v, v);
}
}
}

struct LCP {
// 写一个给一个字符串支持询问 LCP 的东西
int tot, last, tim, n;
int ch[maxn*4][26], par[maxn*4], len[maxn*4], dfn[maxn*4], idfn[maxn*4], a[maxn*4], ind[maxn], rind[maxn];
int mnl[maxn*4][22], mnr[maxn*4][22], lg[maxn*4], mn;
vector<int> son[maxn*4];
void addchar(int c, int l) {
int np = ++ tot; len[np] = l;
while (last && !ch[last][c]) ch[last][c] = np, last = par[last];
if (!last) par[np] = 1;
else {
int q = ch[last][c];
if (len[q] == len[last] + 1) par[np] = q;
else {
int nq = ++ tot;
len[nq] = len[last] + 1;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
par[nq] = par[q];
par[q] = par[np] = nq;
while (last && ch[last][c] == q) ch[last][c] = nq, last = par[last];
}
}
last = np;
}
void dfs(int u) {
dfn[u] = ++ tim; idfn[tim] = u;
a[tim] = min(mn, len[u]); mn = len[u];
for (int i = 0; i < son[u].size(); i++) {
int v = son[u][i];
dfs(v);
mn = min(mn, len[u]);
}
}
int _lcp(int p1, int p2) {
if (p1 == p2) return 2*n - p1 + 1;
p1 = dfn[p1], p2 = dfn[p2];
if (p1 > p2) swap(p1, p2);
int l = p1 + 1, r = p2;
int len = r - l + 1;
int t = lg[len];
return min(mnr[l][t], mnl[r][t]);
}
int lcp(int l1, int r1, int l2, int r2) {
int len1 = abs(r1 - l1) + 1, len2 = abs(r2 - l2) + 1;
int res = min(len1, len2);
if (l1 <= r1 && l2 <= r2) res = min(res, _lcp(ind[l1], ind[l2]));
if (l1 <= r1 && l2 > r2) res = min(res, _lcp(ind[l1], rind[n - l2 + 1]));
if (l1 > r1 && l2 <= r2) res = min(res, _lcp(rind[n - l1 + 1], ind[l2]));
if (l1 > r1 && l2 > r2) res = min(res, _lcp(rind[n - l1 + 1], rind[n - l2 + 1]));
return res;
}
void init(string s) {
n = int (s.size());
tot = last = 1;
string rs = s; reverse(rs.begin(), rs.end());
// ind 是正串后缀对应点
// rind 是反串后缀对应点
for (int i = 0; i < rs.size(); i++) {
addchar(rs[i] - 'a', i+1);
ind[n - i] = last;
}
for (int i = 0; i < s.size(); i++) {
addchar(s[i] - 'a', i + 1 + n);
rind[n - i] = last;
}
for (int i = 2; i <= tot; i++) son[par[i]].push_back(i);
for (int i = 0; (1<<i) <= tot; i++) lg[1<<i] = i;
for (int i = 1; i <= tot; i++) if (!lg[i]) lg[i] = lg[i-1];
mn = 0x3f3f3f3f;
dfs(1);
// cal mnl, mnr
for (int i = 1; i <= tot; i++) {
mnl[i][0] = a[i];
for (int j = 1; j <= 21 && i - (1<<j) + 1 >= 1; j++) {
mnl[i][j] = min(mnl[i][j-1], mnl[i-(1<<(j-1))][j-1]);
}
}
for (int i = tot; i >= 1; i--) {
mnr[i][0] = a[i];
for (int j = 1; j <= 21 && i + (1<<j) - 1 <= tot; j++) {
mnr[i][j] = min(mnr[i][j-1], mnr[i+(1<<(j-1))][j-1]);
}
}
}
} L;

struct Seg {
int l, r;
Seg(int l_=0, int r_=0) : l(l_), r(r_) {}
};

vector<Seg> cal(int u, int v) {
vector<Seg> ret1, ret2;
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]]) {
ret1.push_back(Seg(dfn[u], dfn[top[u]]));
u = fa[top[u]];
} else {
ret2.push_back(Seg(dfn[top[v]], dfn[v]));
v = fa[top[v]];
}
}
if (dep[u] > dep[v]) {
ret1.push_back(Seg(dfn[u], dfn[v]));
} else {
ret2.push_back(Seg(dfn[u], dfn[v]));
}
reverse(ret2.begin(), ret2.end());
vector<Seg> ret = ret1;
for (int i = 0; i < ret2.size(); i++) ret.push_back(ret2[i]);
return ret;
}

int lcp(vector<Seg> v1, vector<Seg> v2) {
int ret = 0;
int i1 = 0, i2 = 0;
while (i1 < v1.size() && i2 < v2.size()) {
int t = L.lcp(v1[i1].l, v1[i1].r, v2[i2].l, v2[i2].r);
if (!t) break;
// cout << t << endl;
// exit(0);
ret += t;
if (v1[i1].l <= v1[i1].r) {
v1[i1].l += t;
if (v1[i1].l > v1[i1].r) ++ i1;
} else {
v1[i1].l -= t;
if (v1[i1].l < v1[i1].r) ++ i1;
}
if (v2[i2].l <= v2[i2].r) {
v2[i2].l += t;
if (v2[i2].l > v2[i2].r) ++ i2;
} else {
v2[i2].l -= t;
if (v2[i2].l < v2[i2].r) ++ i2;
}
}
return ret;
}

int main() {
memset(l, -1, sizeof(l));
scanf("%d", &n);
scanf("%s", str+1);
for (int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
addEdge(u, v), addEdge(v, u);
}
dfs1(1, 0);
dfs2(1, 1);
string s;
for (int i = 1; i <= n; i++) s.push_back(str[idfn[i]]);
L.init(s);
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d);
printf("%d\n", lcp(cal(a, b), cal(c, d)));
}
return 0;
}