#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 100000, LGN = 18;
size_t c[N], e[N], anc[LGN][N], h[N], preorder[N];
vector<size_t> g[N];
size_t traverse(size_t u, size_t p, size_t i = 0)
{
preorder[u] = i++;
for (auto const &v : g[u])
if (v != p)
{
h[v] = h[u] + 1;
anc[0][v] = u;
for (size_t i = 1; i < LGN; ++i)
anc[i][v] = anc[i - 1][anc[i - 1][v]];
i = traverse(v, u, i);
}
return i;
}
size_t lift(size_t u, size_t d)
{
size_t z = 0;
while (d)
{
if (d & 1)
u = anc[z][u];
++z;
d >>= 1;
}
return u;
}
size_t lca(size_t u, size_t v)
{
if (h[u] < h[v])
swap(u, v);
u = lift(u, h[u] - h[v]);
if (u == v)
return u;
for (size_t i = LGN - 1; i < LGN; --i)
if (anc[i][u] != anc[i][v])
u = anc[i][u], v = anc[i][v];
return anc[0][u];
}
size_t distance(size_t u, size_t v)
{
return h[u] + h[v] - (h[lca(u, v)] << 1);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m, q;
cin >> n >> m >> q;
for (size_t i = 0; i < n - 1; ++i)
{
size_t u, v;
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
traverse(0, -1);
for (size_t i = 0; i < m; ++i)
cin >> c[i], --c[i];
while (q--)
{
size_t l, r;
cin >> l >> r, --l, --r;
memcpy(e + l, c + l, (r - l + 1) * sizeof *c);
sort(e + l, e + r + 1, [](size_t u, size_t v)
{ return preorder[u] < preorder[v]; });
size_t distance_sum = distance(e[l], e[r]);
for (size_t i = l; i < r; ++i)
distance_sum += distance(e[i], e[i + 1]);
cout << (distance_sum >> 1) + 1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2864 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
6 ms |
2772 KB |
Output is correct |
13 |
Correct |
6 ms |
2772 KB |
Output is correct |
14 |
Correct |
6 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
4 ms |
2780 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
4 ms |
2772 KB |
Output is correct |
22 |
Correct |
4 ms |
2864 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2828 KB |
Output is correct |
26 |
Correct |
4 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2864 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
6 ms |
2772 KB |
Output is correct |
13 |
Correct |
6 ms |
2772 KB |
Output is correct |
14 |
Correct |
6 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
4 ms |
2780 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
4 ms |
2772 KB |
Output is correct |
22 |
Correct |
4 ms |
2864 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2828 KB |
Output is correct |
26 |
Correct |
4 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
67 ms |
3156 KB |
Output is correct |
31 |
Correct |
80 ms |
3104 KB |
Output is correct |
32 |
Correct |
121 ms |
3204 KB |
Output is correct |
33 |
Correct |
118 ms |
3220 KB |
Output is correct |
34 |
Correct |
122 ms |
3200 KB |
Output is correct |
35 |
Correct |
318 ms |
3200 KB |
Output is correct |
36 |
Correct |
310 ms |
3204 KB |
Output is correct |
37 |
Correct |
331 ms |
3196 KB |
Output is correct |
38 |
Correct |
75 ms |
3272 KB |
Output is correct |
39 |
Correct |
79 ms |
3284 KB |
Output is correct |
40 |
Correct |
74 ms |
3296 KB |
Output is correct |
41 |
Correct |
219 ms |
3300 KB |
Output is correct |
42 |
Correct |
217 ms |
3284 KB |
Output is correct |
43 |
Correct |
218 ms |
3272 KB |
Output is correct |
44 |
Correct |
76 ms |
3228 KB |
Output is correct |
45 |
Correct |
80 ms |
3224 KB |
Output is correct |
46 |
Correct |
79 ms |
3252 KB |
Output is correct |
47 |
Correct |
223 ms |
3216 KB |
Output is correct |
48 |
Correct |
223 ms |
3212 KB |
Output is correct |
49 |
Correct |
225 ms |
3232 KB |
Output is correct |
50 |
Correct |
129 ms |
3204 KB |
Output is correct |
51 |
Correct |
125 ms |
3224 KB |
Output is correct |
52 |
Correct |
132 ms |
3196 KB |
Output is correct |
53 |
Correct |
132 ms |
3216 KB |
Output is correct |
54 |
Correct |
128 ms |
3156 KB |
Output is correct |
55 |
Correct |
123 ms |
3156 KB |
Output is correct |
56 |
Correct |
13 ms |
2644 KB |
Output is correct |
57 |
Correct |
2 ms |
3156 KB |
Output is correct |
58 |
Correct |
2 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
13 ms |
2728 KB |
Output is correct |
4 |
Execution timed out |
5022 ms |
18048 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
48 ms |
14164 KB |
Output is correct |
3 |
Correct |
61 ms |
15540 KB |
Output is correct |
4 |
Correct |
63 ms |
16888 KB |
Output is correct |
5 |
Correct |
99 ms |
23560 KB |
Output is correct |
6 |
Correct |
114 ms |
23616 KB |
Output is correct |
7 |
Correct |
165 ms |
23616 KB |
Output is correct |
8 |
Correct |
124 ms |
23568 KB |
Output is correct |
9 |
Correct |
143 ms |
23500 KB |
Output is correct |
10 |
Correct |
128 ms |
23496 KB |
Output is correct |
11 |
Correct |
155 ms |
23624 KB |
Output is correct |
12 |
Correct |
133 ms |
23620 KB |
Output is correct |
13 |
Correct |
128 ms |
23536 KB |
Output is correct |
14 |
Correct |
130 ms |
23624 KB |
Output is correct |
15 |
Correct |
86 ms |
23740 KB |
Output is correct |
16 |
Correct |
132 ms |
23588 KB |
Output is correct |
17 |
Correct |
122 ms |
23592 KB |
Output is correct |
18 |
Correct |
124 ms |
23592 KB |
Output is correct |
19 |
Correct |
75 ms |
23108 KB |
Output is correct |
20 |
Correct |
87 ms |
23064 KB |
Output is correct |
21 |
Correct |
86 ms |
23116 KB |
Output is correct |
22 |
Correct |
83 ms |
23092 KB |
Output is correct |
23 |
Correct |
88 ms |
23080 KB |
Output is correct |
24 |
Correct |
99 ms |
23116 KB |
Output is correct |
25 |
Correct |
107 ms |
23068 KB |
Output is correct |
26 |
Correct |
125 ms |
23244 KB |
Output is correct |
27 |
Correct |
130 ms |
23080 KB |
Output is correct |
28 |
Correct |
120 ms |
23056 KB |
Output is correct |
29 |
Correct |
144 ms |
23036 KB |
Output is correct |
30 |
Correct |
133 ms |
23172 KB |
Output is correct |
31 |
Correct |
194 ms |
23164 KB |
Output is correct |
32 |
Correct |
137 ms |
23140 KB |
Output is correct |
33 |
Correct |
122 ms |
23188 KB |
Output is correct |
34 |
Correct |
92 ms |
23388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
14 ms |
2724 KB |
Output is correct |
4 |
Execution timed out |
5042 ms |
15236 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2864 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
6 ms |
2772 KB |
Output is correct |
13 |
Correct |
6 ms |
2772 KB |
Output is correct |
14 |
Correct |
6 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
4 ms |
2780 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
4 ms |
2772 KB |
Output is correct |
22 |
Correct |
4 ms |
2864 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2828 KB |
Output is correct |
26 |
Correct |
4 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
67 ms |
3156 KB |
Output is correct |
31 |
Correct |
80 ms |
3104 KB |
Output is correct |
32 |
Correct |
121 ms |
3204 KB |
Output is correct |
33 |
Correct |
118 ms |
3220 KB |
Output is correct |
34 |
Correct |
122 ms |
3200 KB |
Output is correct |
35 |
Correct |
318 ms |
3200 KB |
Output is correct |
36 |
Correct |
310 ms |
3204 KB |
Output is correct |
37 |
Correct |
331 ms |
3196 KB |
Output is correct |
38 |
Correct |
75 ms |
3272 KB |
Output is correct |
39 |
Correct |
79 ms |
3284 KB |
Output is correct |
40 |
Correct |
74 ms |
3296 KB |
Output is correct |
41 |
Correct |
219 ms |
3300 KB |
Output is correct |
42 |
Correct |
217 ms |
3284 KB |
Output is correct |
43 |
Correct |
218 ms |
3272 KB |
Output is correct |
44 |
Correct |
76 ms |
3228 KB |
Output is correct |
45 |
Correct |
80 ms |
3224 KB |
Output is correct |
46 |
Correct |
79 ms |
3252 KB |
Output is correct |
47 |
Correct |
223 ms |
3216 KB |
Output is correct |
48 |
Correct |
223 ms |
3212 KB |
Output is correct |
49 |
Correct |
225 ms |
3232 KB |
Output is correct |
50 |
Correct |
129 ms |
3204 KB |
Output is correct |
51 |
Correct |
125 ms |
3224 KB |
Output is correct |
52 |
Correct |
132 ms |
3196 KB |
Output is correct |
53 |
Correct |
132 ms |
3216 KB |
Output is correct |
54 |
Correct |
128 ms |
3156 KB |
Output is correct |
55 |
Correct |
123 ms |
3156 KB |
Output is correct |
56 |
Correct |
13 ms |
2644 KB |
Output is correct |
57 |
Correct |
2 ms |
3156 KB |
Output is correct |
58 |
Correct |
2 ms |
3156 KB |
Output is correct |
59 |
Correct |
1 ms |
2772 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
13 ms |
2728 KB |
Output is correct |
62 |
Execution timed out |
5022 ms |
18048 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |