#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#warning That's the baby, that's not my baby
#define debug(x) #x << " = " << x << '\n'
typedef long long ll;
const int NMAX = 1e5;
const int LGMAX = 16;
int aib[NMAX + 1];
int answer[NMAX + 1];
int a[NMAX + 1];
std::vector<int> g[NMAX + 1];
std::vector<std::pair<int, int>> queries[NMAX + 1];
std::vector<std::pair<int, int>> curQ[NMAX + 1];
int depth[NMAX + 1];
int tin[NMAX + 1], tout[NMAX + 1], timer;
int jump[LGMAX + 1][NMAX + 1];
int sum[NMAX + 1];
int added[NMAX + 1], added2[NMAX + 1];
int parent[NMAX + 1];
bool ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca(int u, int v) {
if (ancestor(u, v)) {
return u;
}
for (int k = LGMAX; k >= 0; k--) {
if (jump[k][u] != 0 && !ancestor(jump[k][u], v)) {
u = jump[k][u];
}
}
return jump[0][u];
}
void dfs(int u, int p = 0) {
depth[u] = depth[p] + 1;
tin[u] = ++timer;
jump[0][u] = p;
for (const auto &v : g[u]) {
if (v != p) {
dfs(v, u);
}
}
tout[u] = ++timer;
}
void dfs1(int u) {
for (const auto &v : g[u]) {
if (v != parent[u]) {
parent[v] = u;
dfs1(v);
}
}
}
std::vector<std::pair<int, int>> updates;
void update(int pos, const int &val) {
updates.push_back({pos, val});
for (; pos <= NMAX; pos += pos & -pos) {
aib[pos] += val;
}
}
int query(int pos) {
int ret = 0;
for (; pos > 0; pos -= pos & -pos) {
ret += aib[pos];
}
return ret;
}
void divide(int l, int r, const std::vector<std::tuple<int, int, int>> &queries) {
if (l >= r || queries.empty()) {
return;
}
int mid = (l + r) / 2;
std::vector<std::tuple<int, int, int>> quel, quer;
for (const auto &[l, r, index] : queries) {
if (r <= mid) {
quel.push_back({l, r, index});
} else if (mid < l) {
quer.push_back({l, r, index});
} else {
curQ[r].push_back({l, index});
}
}
std::vector<int> vert;
for (int i = l; i <= r; i++) {
vert.push_back(a[i]);
}
std::sort(vert.begin(), vert.end(), [&](int x, int y) {
return tin[x] < tin[y];
});
vert.erase(std::unique(vert.begin(), vert.end()), vert.end());
int sz = (int) vert.size();
for (int i = 1; i < sz; i++) {
vert.push_back(lca(vert[i - 1], vert[i]));
}
std::sort(vert.begin(), vert.end(), [&](int x, int y) {
return tin[x] < tin[y];
});
vert.erase(std::unique(vert.begin(), vert.end()), vert.end());
for (int i = 1; i < (int) vert.size(); i++) {
int w = lca(vert[i - 1], vert[i]);
g[w].push_back(vert[i]);
g[vert[i]].push_back(w);
}
for (const auto &u : vert) {
parent[u] = 0;
}
dfs1(a[mid]);
for (const auto &u : vert) {
added[u] = added2[u] = 0;
}
sum[mid] = 0;
added[a[mid]] = added2[a[mid]] = mid;
for (int i = mid - 1; i >= l; i--) {
int u = a[i];
int add = 0;
while (!added[u]) {
added[u] = i;
add += std::abs(depth[u] - depth[parent[u]]);
u = parent[u];
}
sum[i] = sum[i + 1] + add;
}
for (int i = mid + 1; i <= r; i++) {
int u = a[i];
while (!added2[u]) {
if (added[u] != mid) {
update(added[u] + 1, std::abs(depth[u] - depth[parent[u]]));
}
added2[u] = i;
u = parent[u];
}
for (const auto &[L, index] : curQ[i]) {
answer[index] = query(L) + sum[L];
}
curQ[i].clear();
}
for (const auto &[x, y] : updates) {
update(x, -y);
}
updates.clear();
for (const auto &u : vert) {
g[u].clear();
}
divide(l, mid, quel);
divide(mid + 1, r, quer);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n, m, q;
std::cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
for (int k = 1; k <= LGMAX; k++) {
for (int i = 1; i <= n; i++) {
jump[k][i] = jump[k - 1][jump[k - 1][i]];
}
}
for (int i = 1; i <= m; i++) {
std::cin >> a[i];
}
std::vector<std::tuple<int, int, int>> queries;
for (int i = 0; i < q; i++) {
int l, r;
std::cin >> l >> r;
if (l == r) {
answer[i] = 0;
} else {
queries.push_back({l, r, i});
}
}
for (int i = 1; i <= n; i++) {
g[i].clear();
}
divide(1, m, queries);
// std::cout << "\n\nMy debug:\n\n";
// for (int i = 1; i <= n; i++) {
// std::cout << depth[i] << ' ';
// }
// std::cout << '\n';
//
// std::cout << "\n\nYour queries:\n\n";
for (int i = 0; i < q; i++) {
std::cout << answer[i] + 1 << '\n';
}
return 0;
}
Compilation message
tourism.cpp:7:2: warning: #warning That's the baby, that's not my baby [-Wcpp]
7 | #warning That's the baby, that's not my baby
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12728 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12732 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12736 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12712 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12888 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
10888 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12728 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12732 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12736 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12712 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12888 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
10888 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
12636 KB |
Output is correct |
30 |
Correct |
4 ms |
12892 KB |
Output is correct |
31 |
Correct |
5 ms |
12888 KB |
Output is correct |
32 |
Correct |
5 ms |
13144 KB |
Output is correct |
33 |
Correct |
5 ms |
13148 KB |
Output is correct |
34 |
Correct |
5 ms |
13144 KB |
Output is correct |
35 |
Correct |
3 ms |
11100 KB |
Output is correct |
36 |
Correct |
3 ms |
13148 KB |
Output is correct |
37 |
Correct |
4 ms |
13148 KB |
Output is correct |
38 |
Correct |
4 ms |
13148 KB |
Output is correct |
39 |
Correct |
5 ms |
11100 KB |
Output is correct |
40 |
Correct |
4 ms |
13180 KB |
Output is correct |
41 |
Correct |
3 ms |
13148 KB |
Output is correct |
42 |
Correct |
3 ms |
11216 KB |
Output is correct |
43 |
Correct |
3 ms |
13148 KB |
Output is correct |
44 |
Correct |
5 ms |
13148 KB |
Output is correct |
45 |
Correct |
4 ms |
11100 KB |
Output is correct |
46 |
Correct |
5 ms |
13148 KB |
Output is correct |
47 |
Correct |
3 ms |
11096 KB |
Output is correct |
48 |
Correct |
4 ms |
13148 KB |
Output is correct |
49 |
Correct |
3 ms |
13148 KB |
Output is correct |
50 |
Correct |
4 ms |
13176 KB |
Output is correct |
51 |
Correct |
4 ms |
13148 KB |
Output is correct |
52 |
Correct |
4 ms |
13148 KB |
Output is correct |
53 |
Correct |
4 ms |
13144 KB |
Output is correct |
54 |
Correct |
4 ms |
11096 KB |
Output is correct |
55 |
Correct |
5 ms |
13144 KB |
Output is correct |
56 |
Correct |
3 ms |
12888 KB |
Output is correct |
57 |
Correct |
3 ms |
12888 KB |
Output is correct |
58 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12632 KB |
Output is correct |
3 |
Correct |
2 ms |
10840 KB |
Output is correct |
4 |
Correct |
185 ms |
25812 KB |
Output is correct |
5 |
Correct |
141 ms |
27436 KB |
Output is correct |
6 |
Runtime error |
71 ms |
55176 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Runtime error |
46 ms |
35932 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12888 KB |
Output is correct |
4 |
Correct |
292 ms |
23756 KB |
Output is correct |
5 |
Correct |
295 ms |
22980 KB |
Output is correct |
6 |
Correct |
339 ms |
26564 KB |
Output is correct |
7 |
Correct |
355 ms |
27848 KB |
Output is correct |
8 |
Correct |
362 ms |
28104 KB |
Output is correct |
9 |
Correct |
367 ms |
27848 KB |
Output is correct |
10 |
Correct |
396 ms |
27924 KB |
Output is correct |
11 |
Correct |
391 ms |
28108 KB |
Output is correct |
12 |
Correct |
416 ms |
27900 KB |
Output is correct |
13 |
Correct |
413 ms |
27872 KB |
Output is correct |
14 |
Correct |
49 ms |
16240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12728 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12732 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12736 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12712 KB |
Output is correct |
16 |
Correct |
2 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
10844 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12888 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
10888 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
12636 KB |
Output is correct |
30 |
Correct |
4 ms |
12892 KB |
Output is correct |
31 |
Correct |
5 ms |
12888 KB |
Output is correct |
32 |
Correct |
5 ms |
13144 KB |
Output is correct |
33 |
Correct |
5 ms |
13148 KB |
Output is correct |
34 |
Correct |
5 ms |
13144 KB |
Output is correct |
35 |
Correct |
3 ms |
11100 KB |
Output is correct |
36 |
Correct |
3 ms |
13148 KB |
Output is correct |
37 |
Correct |
4 ms |
13148 KB |
Output is correct |
38 |
Correct |
4 ms |
13148 KB |
Output is correct |
39 |
Correct |
5 ms |
11100 KB |
Output is correct |
40 |
Correct |
4 ms |
13180 KB |
Output is correct |
41 |
Correct |
3 ms |
13148 KB |
Output is correct |
42 |
Correct |
3 ms |
11216 KB |
Output is correct |
43 |
Correct |
3 ms |
13148 KB |
Output is correct |
44 |
Correct |
5 ms |
13148 KB |
Output is correct |
45 |
Correct |
4 ms |
11100 KB |
Output is correct |
46 |
Correct |
5 ms |
13148 KB |
Output is correct |
47 |
Correct |
3 ms |
11096 KB |
Output is correct |
48 |
Correct |
4 ms |
13148 KB |
Output is correct |
49 |
Correct |
3 ms |
13148 KB |
Output is correct |
50 |
Correct |
4 ms |
13176 KB |
Output is correct |
51 |
Correct |
4 ms |
13148 KB |
Output is correct |
52 |
Correct |
4 ms |
13148 KB |
Output is correct |
53 |
Correct |
4 ms |
13144 KB |
Output is correct |
54 |
Correct |
4 ms |
11096 KB |
Output is correct |
55 |
Correct |
5 ms |
13144 KB |
Output is correct |
56 |
Correct |
3 ms |
12888 KB |
Output is correct |
57 |
Correct |
3 ms |
12888 KB |
Output is correct |
58 |
Correct |
2 ms |
10844 KB |
Output is correct |
59 |
Correct |
2 ms |
12632 KB |
Output is correct |
60 |
Correct |
2 ms |
12632 KB |
Output is correct |
61 |
Correct |
2 ms |
10840 KB |
Output is correct |
62 |
Correct |
185 ms |
25812 KB |
Output is correct |
63 |
Correct |
141 ms |
27436 KB |
Output is correct |
64 |
Runtime error |
71 ms |
55176 KB |
Execution killed with signal 11 |
65 |
Halted |
0 ms |
0 KB |
- |