# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
986924 |
2024-05-21T15:08:19 Z |
876pol |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
78688 KB |
#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;
vvll graph(n + 1);
FOR(i, 0, n - 1) {
ll a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
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";
// vll cc;
// cc.reserve(edges.size() * 2);
// TRAV(e, edges) {
// cc.push_back(e.first);
// cc.push_back(e.second);
// }
// sort(all(cc));
// cc.erase(unique(all(cc)), cc.end());
// vvpll cgraph(cc.size());
// ll ans = 0;
// TRAV(e, edges) {
// ll u = lower_bound(all(cc), e.first) - cc.begin();
// ll v = lower_bound(all(cc), e.second) - cc.begin();
// ll d = ds(e.first, e.second);
// cgraph[u].push_back({v, d});
// cgraph[v].push_back({u, d});
// ans += d - 1;
// }
// function<pll(ll, ll)> dfs2 = [&](ll curr, ll prev) -> pll {
// pll best = {0, curr};
// TRAV(e, cgraph[curr]) {
// if (e.first == prev) continue;
// pll obj = dfs2(e.first, curr);
// obj.first += e.second;
// best = max(best, obj);
// }
// return best;
// };
// pll o1 = dfs2(0, -1);
// pll o2 = dfs2(o1.second, -1);
// cout << ans - o2.first << "\n";
// cout << ans + cc.size() << "\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
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
7 ms |
604 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
604 KB |
Output is correct |
12 |
Correct |
15 ms |
600 KB |
Output is correct |
13 |
Correct |
14 ms |
604 KB |
Output is correct |
14 |
Correct |
14 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
600 KB |
Output is correct |
17 |
Correct |
6 ms |
632 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
604 KB |
Output is correct |
23 |
Correct |
6 ms |
600 KB |
Output is correct |
24 |
Correct |
6 ms |
716 KB |
Output is correct |
25 |
Correct |
6 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
7 ms |
604 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
604 KB |
Output is correct |
12 |
Correct |
15 ms |
600 KB |
Output is correct |
13 |
Correct |
14 ms |
604 KB |
Output is correct |
14 |
Correct |
14 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
600 KB |
Output is correct |
17 |
Correct |
6 ms |
632 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
604 KB |
Output is correct |
23 |
Correct |
6 ms |
600 KB |
Output is correct |
24 |
Correct |
6 ms |
716 KB |
Output is correct |
25 |
Correct |
6 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
142 ms |
1624 KB |
Output is correct |
31 |
Correct |
197 ms |
1372 KB |
Output is correct |
32 |
Correct |
297 ms |
2048 KB |
Output is correct |
33 |
Correct |
294 ms |
1880 KB |
Output is correct |
34 |
Correct |
292 ms |
2040 KB |
Output is correct |
35 |
Correct |
765 ms |
2292 KB |
Output is correct |
36 |
Correct |
756 ms |
2292 KB |
Output is correct |
37 |
Correct |
756 ms |
2052 KB |
Output is correct |
38 |
Correct |
237 ms |
2392 KB |
Output is correct |
39 |
Correct |
225 ms |
2140 KB |
Output is correct |
40 |
Correct |
230 ms |
2252 KB |
Output is correct |
41 |
Correct |
632 ms |
2136 KB |
Output is correct |
42 |
Correct |
633 ms |
2284 KB |
Output is correct |
43 |
Correct |
637 ms |
2136 KB |
Output is correct |
44 |
Correct |
235 ms |
2140 KB |
Output is correct |
45 |
Correct |
250 ms |
2088 KB |
Output is correct |
46 |
Correct |
245 ms |
2140 KB |
Output is correct |
47 |
Correct |
664 ms |
2132 KB |
Output is correct |
48 |
Correct |
673 ms |
2096 KB |
Output is correct |
49 |
Correct |
655 ms |
1884 KB |
Output is correct |
50 |
Correct |
261 ms |
1880 KB |
Output is correct |
51 |
Correct |
258 ms |
2040 KB |
Output is correct |
52 |
Correct |
267 ms |
2308 KB |
Output is correct |
53 |
Correct |
250 ms |
1880 KB |
Output is correct |
54 |
Correct |
259 ms |
2040 KB |
Output is correct |
55 |
Correct |
255 ms |
1880 KB |
Output is correct |
56 |
Correct |
49 ms |
516 KB |
Output is correct |
57 |
Correct |
2 ms |
1880 KB |
Output is correct |
58 |
Correct |
2 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
46 ms |
544 KB |
Output is correct |
4 |
Execution timed out |
5027 ms |
52768 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
57 ms |
43160 KB |
Output is correct |
3 |
Correct |
76 ms |
46792 KB |
Output is correct |
4 |
Correct |
74 ms |
53432 KB |
Output is correct |
5 |
Correct |
97 ms |
78516 KB |
Output is correct |
6 |
Correct |
113 ms |
78672 KB |
Output is correct |
7 |
Correct |
119 ms |
78516 KB |
Output is correct |
8 |
Correct |
120 ms |
78476 KB |
Output is correct |
9 |
Correct |
124 ms |
78520 KB |
Output is correct |
10 |
Correct |
119 ms |
78516 KB |
Output is correct |
11 |
Correct |
129 ms |
78396 KB |
Output is correct |
12 |
Correct |
127 ms |
78516 KB |
Output is correct |
13 |
Correct |
116 ms |
78516 KB |
Output is correct |
14 |
Correct |
106 ms |
78516 KB |
Output is correct |
15 |
Correct |
98 ms |
78520 KB |
Output is correct |
16 |
Correct |
109 ms |
78516 KB |
Output is correct |
17 |
Correct |
106 ms |
78644 KB |
Output is correct |
18 |
Correct |
108 ms |
78688 KB |
Output is correct |
19 |
Correct |
102 ms |
78516 KB |
Output is correct |
20 |
Correct |
92 ms |
78264 KB |
Output is correct |
21 |
Correct |
96 ms |
78264 KB |
Output is correct |
22 |
Correct |
98 ms |
78232 KB |
Output is correct |
23 |
Correct |
119 ms |
78260 KB |
Output is correct |
24 |
Correct |
100 ms |
78260 KB |
Output is correct |
25 |
Correct |
106 ms |
78260 KB |
Output is correct |
26 |
Correct |
113 ms |
78264 KB |
Output is correct |
27 |
Correct |
110 ms |
78272 KB |
Output is correct |
28 |
Correct |
108 ms |
78068 KB |
Output is correct |
29 |
Correct |
107 ms |
78260 KB |
Output is correct |
30 |
Correct |
119 ms |
78236 KB |
Output is correct |
31 |
Correct |
110 ms |
78264 KB |
Output is correct |
32 |
Correct |
105 ms |
78264 KB |
Output is correct |
33 |
Correct |
98 ms |
78012 KB |
Output is correct |
34 |
Correct |
93 ms |
78256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
48 ms |
672 KB |
Output is correct |
4 |
Execution timed out |
5048 ms |
44308 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
7 ms |
604 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
604 KB |
Output is correct |
12 |
Correct |
15 ms |
600 KB |
Output is correct |
13 |
Correct |
14 ms |
604 KB |
Output is correct |
14 |
Correct |
14 ms |
604 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
600 KB |
Output is correct |
17 |
Correct |
6 ms |
632 KB |
Output is correct |
18 |
Correct |
6 ms |
600 KB |
Output is correct |
19 |
Correct |
5 ms |
600 KB |
Output is correct |
20 |
Correct |
5 ms |
604 KB |
Output is correct |
21 |
Correct |
6 ms |
604 KB |
Output is correct |
22 |
Correct |
5 ms |
604 KB |
Output is correct |
23 |
Correct |
6 ms |
600 KB |
Output is correct |
24 |
Correct |
6 ms |
716 KB |
Output is correct |
25 |
Correct |
6 ms |
604 KB |
Output is correct |
26 |
Correct |
5 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
142 ms |
1624 KB |
Output is correct |
31 |
Correct |
197 ms |
1372 KB |
Output is correct |
32 |
Correct |
297 ms |
2048 KB |
Output is correct |
33 |
Correct |
294 ms |
1880 KB |
Output is correct |
34 |
Correct |
292 ms |
2040 KB |
Output is correct |
35 |
Correct |
765 ms |
2292 KB |
Output is correct |
36 |
Correct |
756 ms |
2292 KB |
Output is correct |
37 |
Correct |
756 ms |
2052 KB |
Output is correct |
38 |
Correct |
237 ms |
2392 KB |
Output is correct |
39 |
Correct |
225 ms |
2140 KB |
Output is correct |
40 |
Correct |
230 ms |
2252 KB |
Output is correct |
41 |
Correct |
632 ms |
2136 KB |
Output is correct |
42 |
Correct |
633 ms |
2284 KB |
Output is correct |
43 |
Correct |
637 ms |
2136 KB |
Output is correct |
44 |
Correct |
235 ms |
2140 KB |
Output is correct |
45 |
Correct |
250 ms |
2088 KB |
Output is correct |
46 |
Correct |
245 ms |
2140 KB |
Output is correct |
47 |
Correct |
664 ms |
2132 KB |
Output is correct |
48 |
Correct |
673 ms |
2096 KB |
Output is correct |
49 |
Correct |
655 ms |
1884 KB |
Output is correct |
50 |
Correct |
261 ms |
1880 KB |
Output is correct |
51 |
Correct |
258 ms |
2040 KB |
Output is correct |
52 |
Correct |
267 ms |
2308 KB |
Output is correct |
53 |
Correct |
250 ms |
1880 KB |
Output is correct |
54 |
Correct |
259 ms |
2040 KB |
Output is correct |
55 |
Correct |
255 ms |
1880 KB |
Output is correct |
56 |
Correct |
49 ms |
516 KB |
Output is correct |
57 |
Correct |
2 ms |
1880 KB |
Output is correct |
58 |
Correct |
2 ms |
1880 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
46 ms |
544 KB |
Output is correct |
62 |
Execution timed out |
5027 ms |
52768 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |