#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define dbg(...)
#define STRUCT_DBG(...)
#else
#include "lib/debug.h"
#endif
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T>
using vec = vector<T>;
using vll = vector<ll>;
using vpll = vector<pair<ll, ll>>;
using vvll = vector<vector<ll>>;
using vvpll = vector<vector<pair<ll, ll>>>;
template <class T>
using indexed_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define FOR(i, s, e) for (ll i = (ll)s; i < (ll)e; i++)
#define CFOR(i, s, e) for (ll i = (ll)s; i <= (ll)e; i++)
#define RFOR(i, e, s) for (ll i = (ll)e - 1; i >= (ll)s; i--)
#define TRAV(a, c) for (const auto &a : c)
#define all(x) x.begin(), x.end()
#define MOD 1000000007
// #define MOD 998244353
#define FASTIO
#define PRECISION
// #define FILE "file"
#define SINGLE
// #define MULTIPLE
// #define GOOGLE
void solve() {
ll n, m, q;
cin >> n >> m >> q;
bool st3 = true;
vvll graph(n + 1);
FOR(i, 0, n - 1) {
ll a, b;
cin >> a >> b;
st3 &= (a + 1 == b);
graph[a].push_back(b);
graph[b].push_back(a);
}
if (st3) {
vll c(m + 1);
CFOR(i, 1, m) cin >> c[i];
ll L = 20;
vvll stmn(L, vll(m + 1)), stmx(L, vll(m + 1));
CFOR(i, 1, m) stmn[0][i] = stmx[0][i] = c[i];
FOR(i, 1, L) {
CFOR(j, 1, m + 1 - (1ll << i)) {
stmn[i][j] =
min(stmn[i - 1][j], stmn[i - 1][j + (1ll << (i - 1))]);
stmx[i][j] =
max(stmx[i - 1][j], stmx[i - 1][j + (1ll << (i - 1))]);
}
}
auto qmn = [&](ll l, ll r) {
ll q = 63 - __builtin_clzll(r - l + 1);
return min(stmn[q][l], stmn[q][r - (1ll << q) + 1]);
};
auto qmx = [&](ll l, ll r) {
ll q = 63 - __builtin_clzll(r - l + 1);
return max(stmx[q][l], stmx[q][r - (1ll << q) + 1]);
};
FOR(_, 0, q) {
ll l, r;
cin >> l >> r;
cout << qmx(l, r) - qmn(l, r) + 1 << "\n";
}
} else {
vll euler, dist, tin(n + 1), level(n + 1);
ll cdist = 0;
function<void(ll, ll)> dfs1 = [&](ll curr, ll prev) {
tin[curr] = euler.size();
level[curr] = cdist;
euler.push_back(curr);
dist.push_back(cdist);
cdist++;
TRAV(e, graph[curr]) {
if (e == prev) continue;
dfs1(e, curr);
euler.push_back(curr);
dist.push_back(cdist - 1);
}
cdist--;
};
dfs1(1, 0);
ll L = 20;
vvpll st(L, vpll(euler.size()));
FOR(i, 0, euler.size()) st[0][i] = {dist[i], euler[i]};
FOR(i, 1, L) {
CFOR(j, 0, euler.size() - (1ll << i)) {
st[i][j] = min(st[i - 1][j], st[i - 1][j + (1ll << (i - 1))]);
}
}
auto lca = [&](ll x, ll y) {
x = tin[x];
y = tin[y];
if (x > y) swap(x, y);
ll q = 63 - __builtin_clzll(y - x + 1);
return min(st[q][x], st[q][y - (1ll << q) + 1]).second;
};
auto ds = [&](ll x, ll y) {
return level[x] + level[y] - 2 * level[lca(x, y)];
};
vll c(m + 1);
CFOR(i, 1, m) cin >> c[i];
FOR(_, 0, q) {
ll l, r;
cin >> l >> r;
vll nodes;
nodes.reserve(r - l + 2);
ll root = c[l];
CFOR(i, l, r) {
nodes.push_back(c[i]);
root = lca(root, c[i]);
}
nodes.push_back(root);
sort(all(nodes));
nodes.erase(unique(all(nodes)), nodes.end());
sort(all(nodes), [&](ll o1, ll o2) { return tin[o1] < tin[o2]; });
vll stk = {root};
ll ans = 1;
for (auto it = nodes.begin() + 1; it != nodes.end(); it++) {
vll tr;
while (lca(stk.back(), *it) != stk.back()) {
tr.push_back(stk.back());
stk.pop_back();
}
if (tr.size() && stk.back() != lca(*it, tr.back())) {
stk.push_back(lca(*it, tr.back()));
}
FOR(i, 0, tr.size() - 1) {
ans += ds(tr[i], tr[i + 1]);
}
if (tr.size()) ans += ds(tr.back(), stk.back());
stk.push_back(*it);
}
FOR(i, 0, stk.size() - 1) {
ans += ds(stk[i], stk[i + 1]);
}
cout << ans << "\n";
}
}
}
int main() {
#ifdef FASTIO
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif
#ifdef PRECISION
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
#endif
#ifdef FILE
freopen((FILE + string(".in")).c_str(), "r", stdin);
freopen((FILE + string(".out")).c_str(), "w", stdout);
#endif
#ifdef SINGLE
solve();
#endif
#ifdef MULTIPLE
ll t;
cin >> t;
for (ll i = 1; i <= t; i++) {
solve();
}
#endif
#ifdef GOOGLE
ll t;
cin >> t;
for (ll i = 1; i <= t; i++) {
cout << "Case #" << i << ": ";
solve();
}
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
608 KB |
Output is correct |
5 |
Correct |
3 ms |
856 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
628 KB |
Output is correct |
8 |
Correct |
3 ms |
856 KB |
Output is correct |
9 |
Correct |
6 ms |
604 KB |
Output is correct |
10 |
Correct |
6 ms |
720 KB |
Output is correct |
11 |
Correct |
6 ms |
604 KB |
Output is correct |
12 |
Correct |
14 ms |
604 KB |
Output is correct |
13 |
Correct |
14 ms |
600 KB |
Output is correct |
14 |
Correct |
14 ms |
600 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
748 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
5 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
604 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
5 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
720 KB |
Output is correct |
23 |
Correct |
5 ms |
856 KB |
Output is correct |
24 |
Correct |
5 ms |
712 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
608 KB |
Output is correct |
5 |
Correct |
3 ms |
856 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
628 KB |
Output is correct |
8 |
Correct |
3 ms |
856 KB |
Output is correct |
9 |
Correct |
6 ms |
604 KB |
Output is correct |
10 |
Correct |
6 ms |
720 KB |
Output is correct |
11 |
Correct |
6 ms |
604 KB |
Output is correct |
12 |
Correct |
14 ms |
604 KB |
Output is correct |
13 |
Correct |
14 ms |
600 KB |
Output is correct |
14 |
Correct |
14 ms |
600 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
748 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
5 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
604 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
5 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
720 KB |
Output is correct |
23 |
Correct |
5 ms |
856 KB |
Output is correct |
24 |
Correct |
5 ms |
712 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
158 ms |
1604 KB |
Output is correct |
31 |
Correct |
197 ms |
1576 KB |
Output is correct |
32 |
Correct |
294 ms |
1884 KB |
Output is correct |
33 |
Correct |
294 ms |
1884 KB |
Output is correct |
34 |
Correct |
301 ms |
2128 KB |
Output is correct |
35 |
Correct |
748 ms |
2136 KB |
Output is correct |
36 |
Correct |
760 ms |
1992 KB |
Output is correct |
37 |
Correct |
749 ms |
1884 KB |
Output is correct |
38 |
Correct |
232 ms |
2136 KB |
Output is correct |
39 |
Correct |
229 ms |
2180 KB |
Output is correct |
40 |
Correct |
228 ms |
2140 KB |
Output is correct |
41 |
Correct |
634 ms |
2136 KB |
Output is correct |
42 |
Correct |
627 ms |
2236 KB |
Output is correct |
43 |
Correct |
659 ms |
2148 KB |
Output is correct |
44 |
Correct |
232 ms |
1880 KB |
Output is correct |
45 |
Correct |
250 ms |
2128 KB |
Output is correct |
46 |
Correct |
240 ms |
2088 KB |
Output is correct |
47 |
Correct |
640 ms |
2088 KB |
Output is correct |
48 |
Correct |
636 ms |
1880 KB |
Output is correct |
49 |
Correct |
631 ms |
2324 KB |
Output is correct |
50 |
Correct |
258 ms |
1884 KB |
Output is correct |
51 |
Correct |
258 ms |
1884 KB |
Output is correct |
52 |
Correct |
259 ms |
2132 KB |
Output is correct |
53 |
Correct |
248 ms |
1992 KB |
Output is correct |
54 |
Correct |
252 ms |
1880 KB |
Output is correct |
55 |
Correct |
254 ms |
1996 KB |
Output is correct |
56 |
Correct |
2 ms |
1116 KB |
Output is correct |
57 |
Correct |
2 ms |
1884 KB |
Output is correct |
58 |
Correct |
2 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
51 ms |
33700 KB |
Output is correct |
5 |
Correct |
43 ms |
25728 KB |
Output is correct |
6 |
Correct |
51 ms |
34988 KB |
Output is correct |
7 |
Correct |
65 ms |
42300 KB |
Output is correct |
8 |
Correct |
77 ms |
42276 KB |
Output is correct |
9 |
Correct |
69 ms |
42196 KB |
Output is correct |
10 |
Correct |
70 ms |
42196 KB |
Output is correct |
11 |
Correct |
70 ms |
42196 KB |
Output is correct |
12 |
Correct |
79 ms |
41896 KB |
Output is correct |
13 |
Correct |
61 ms |
41912 KB |
Output is correct |
14 |
Correct |
62 ms |
42056 KB |
Output is correct |
15 |
Correct |
37 ms |
7564 KB |
Output is correct |
16 |
Correct |
66 ms |
41728 KB |
Output is correct |
17 |
Correct |
42 ms |
34884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
65 ms |
42352 KB |
Output is correct |
3 |
Correct |
103 ms |
45936 KB |
Output is correct |
4 |
Correct |
72 ms |
52416 KB |
Output is correct |
5 |
Correct |
96 ms |
77276 KB |
Output is correct |
6 |
Correct |
130 ms |
77128 KB |
Output is correct |
7 |
Correct |
144 ms |
77100 KB |
Output is correct |
8 |
Correct |
121 ms |
77240 KB |
Output is correct |
9 |
Correct |
127 ms |
77096 KB |
Output is correct |
10 |
Correct |
123 ms |
77280 KB |
Output is correct |
11 |
Correct |
117 ms |
77172 KB |
Output is correct |
12 |
Correct |
112 ms |
77232 KB |
Output is correct |
13 |
Correct |
107 ms |
77236 KB |
Output is correct |
14 |
Correct |
104 ms |
77244 KB |
Output is correct |
15 |
Correct |
95 ms |
77152 KB |
Output is correct |
16 |
Correct |
106 ms |
77208 KB |
Output is correct |
17 |
Correct |
135 ms |
77096 KB |
Output is correct |
18 |
Correct |
114 ms |
77080 KB |
Output is correct |
19 |
Correct |
88 ms |
76824 KB |
Output is correct |
20 |
Correct |
97 ms |
76820 KB |
Output is correct |
21 |
Correct |
97 ms |
76876 KB |
Output is correct |
22 |
Correct |
121 ms |
76760 KB |
Output is correct |
23 |
Correct |
110 ms |
76724 KB |
Output is correct |
24 |
Correct |
109 ms |
76724 KB |
Output is correct |
25 |
Correct |
108 ms |
76728 KB |
Output is correct |
26 |
Correct |
106 ms |
76884 KB |
Output is correct |
27 |
Correct |
108 ms |
76724 KB |
Output is correct |
28 |
Correct |
117 ms |
76684 KB |
Output is correct |
29 |
Correct |
111 ms |
76860 KB |
Output is correct |
30 |
Correct |
113 ms |
76892 KB |
Output is correct |
31 |
Correct |
108 ms |
76724 KB |
Output is correct |
32 |
Correct |
100 ms |
76824 KB |
Output is correct |
33 |
Correct |
98 ms |
76732 KB |
Output is correct |
34 |
Correct |
91 ms |
76980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Execution timed out |
5008 ms |
43376 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
608 KB |
Output is correct |
5 |
Correct |
3 ms |
856 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
628 KB |
Output is correct |
8 |
Correct |
3 ms |
856 KB |
Output is correct |
9 |
Correct |
6 ms |
604 KB |
Output is correct |
10 |
Correct |
6 ms |
720 KB |
Output is correct |
11 |
Correct |
6 ms |
604 KB |
Output is correct |
12 |
Correct |
14 ms |
604 KB |
Output is correct |
13 |
Correct |
14 ms |
600 KB |
Output is correct |
14 |
Correct |
14 ms |
600 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
748 KB |
Output is correct |
17 |
Correct |
5 ms |
600 KB |
Output is correct |
18 |
Correct |
5 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
604 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
5 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
720 KB |
Output is correct |
23 |
Correct |
5 ms |
856 KB |
Output is correct |
24 |
Correct |
5 ms |
712 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
158 ms |
1604 KB |
Output is correct |
31 |
Correct |
197 ms |
1576 KB |
Output is correct |
32 |
Correct |
294 ms |
1884 KB |
Output is correct |
33 |
Correct |
294 ms |
1884 KB |
Output is correct |
34 |
Correct |
301 ms |
2128 KB |
Output is correct |
35 |
Correct |
748 ms |
2136 KB |
Output is correct |
36 |
Correct |
760 ms |
1992 KB |
Output is correct |
37 |
Correct |
749 ms |
1884 KB |
Output is correct |
38 |
Correct |
232 ms |
2136 KB |
Output is correct |
39 |
Correct |
229 ms |
2180 KB |
Output is correct |
40 |
Correct |
228 ms |
2140 KB |
Output is correct |
41 |
Correct |
634 ms |
2136 KB |
Output is correct |
42 |
Correct |
627 ms |
2236 KB |
Output is correct |
43 |
Correct |
659 ms |
2148 KB |
Output is correct |
44 |
Correct |
232 ms |
1880 KB |
Output is correct |
45 |
Correct |
250 ms |
2128 KB |
Output is correct |
46 |
Correct |
240 ms |
2088 KB |
Output is correct |
47 |
Correct |
640 ms |
2088 KB |
Output is correct |
48 |
Correct |
636 ms |
1880 KB |
Output is correct |
49 |
Correct |
631 ms |
2324 KB |
Output is correct |
50 |
Correct |
258 ms |
1884 KB |
Output is correct |
51 |
Correct |
258 ms |
1884 KB |
Output is correct |
52 |
Correct |
259 ms |
2132 KB |
Output is correct |
53 |
Correct |
248 ms |
1992 KB |
Output is correct |
54 |
Correct |
252 ms |
1880 KB |
Output is correct |
55 |
Correct |
254 ms |
1996 KB |
Output is correct |
56 |
Correct |
2 ms |
1116 KB |
Output is correct |
57 |
Correct |
2 ms |
1884 KB |
Output is correct |
58 |
Correct |
2 ms |
2048 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
2 ms |
1116 KB |
Output is correct |
62 |
Correct |
51 ms |
33700 KB |
Output is correct |
63 |
Correct |
43 ms |
25728 KB |
Output is correct |
64 |
Correct |
51 ms |
34988 KB |
Output is correct |
65 |
Correct |
65 ms |
42300 KB |
Output is correct |
66 |
Correct |
77 ms |
42276 KB |
Output is correct |
67 |
Correct |
69 ms |
42196 KB |
Output is correct |
68 |
Correct |
70 ms |
42196 KB |
Output is correct |
69 |
Correct |
70 ms |
42196 KB |
Output is correct |
70 |
Correct |
79 ms |
41896 KB |
Output is correct |
71 |
Correct |
61 ms |
41912 KB |
Output is correct |
72 |
Correct |
62 ms |
42056 KB |
Output is correct |
73 |
Correct |
37 ms |
7564 KB |
Output is correct |
74 |
Correct |
66 ms |
41728 KB |
Output is correct |
75 |
Correct |
42 ms |
34884 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
65 ms |
42352 KB |
Output is correct |
78 |
Correct |
103 ms |
45936 KB |
Output is correct |
79 |
Correct |
72 ms |
52416 KB |
Output is correct |
80 |
Correct |
96 ms |
77276 KB |
Output is correct |
81 |
Correct |
130 ms |
77128 KB |
Output is correct |
82 |
Correct |
144 ms |
77100 KB |
Output is correct |
83 |
Correct |
121 ms |
77240 KB |
Output is correct |
84 |
Correct |
127 ms |
77096 KB |
Output is correct |
85 |
Correct |
123 ms |
77280 KB |
Output is correct |
86 |
Correct |
117 ms |
77172 KB |
Output is correct |
87 |
Correct |
112 ms |
77232 KB |
Output is correct |
88 |
Correct |
107 ms |
77236 KB |
Output is correct |
89 |
Correct |
104 ms |
77244 KB |
Output is correct |
90 |
Correct |
95 ms |
77152 KB |
Output is correct |
91 |
Correct |
106 ms |
77208 KB |
Output is correct |
92 |
Correct |
135 ms |
77096 KB |
Output is correct |
93 |
Correct |
114 ms |
77080 KB |
Output is correct |
94 |
Correct |
88 ms |
76824 KB |
Output is correct |
95 |
Correct |
97 ms |
76820 KB |
Output is correct |
96 |
Correct |
97 ms |
76876 KB |
Output is correct |
97 |
Correct |
121 ms |
76760 KB |
Output is correct |
98 |
Correct |
110 ms |
76724 KB |
Output is correct |
99 |
Correct |
109 ms |
76724 KB |
Output is correct |
100 |
Correct |
108 ms |
76728 KB |
Output is correct |
101 |
Correct |
106 ms |
76884 KB |
Output is correct |
102 |
Correct |
108 ms |
76724 KB |
Output is correct |
103 |
Correct |
117 ms |
76684 KB |
Output is correct |
104 |
Correct |
111 ms |
76860 KB |
Output is correct |
105 |
Correct |
113 ms |
76892 KB |
Output is correct |
106 |
Correct |
108 ms |
76724 KB |
Output is correct |
107 |
Correct |
100 ms |
76824 KB |
Output is correct |
108 |
Correct |
98 ms |
76732 KB |
Output is correct |
109 |
Correct |
91 ms |
76980 KB |
Output is correct |
110 |
Correct |
0 ms |
344 KB |
Output is correct |
111 |
Correct |
0 ms |
348 KB |
Output is correct |
112 |
Correct |
1 ms |
860 KB |
Output is correct |
113 |
Execution timed out |
5008 ms |
43376 KB |
Time limit exceeded |
114 |
Halted |
0 ms |
0 KB |
- |