# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
898030 |
2024-01-04T08:22:24 Z |
vjudge1 |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
67496 KB |
// MDSPro
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
const ld PI = 3.141592653589793;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFLL = 4e18;
const double EPS = 1e-9;
const int MAXN = 1000*1007;
template<typename T>
struct SparseTable {
vector<vector<T>> sparse;
function<T(const T&, const T&)> merge;
SparseTable(const vector<T>& arr, const function<T(const T&, const T&)>& func) : merge(func) {
int n = arr.size();
int logn = 32 - __builtin_clz(n);
sparse.resize(logn, vector<T>(n));
sparse[0] = arr;
for (int lg = 1; lg < logn; lg++) {
for (int i = 0; i + (1 << lg) <= n; i++) {
sparse[lg][i] = merge(sparse[lg - 1][i], sparse[lg - 1][i + (1 << (lg - 1))]);
}
}
}
T get(int l, int r) { // [l, r)
int cur_log = 31 - __builtin_clz(r - l);
return merge(sparse[cur_log][l], sparse[cur_log][r - (1 << cur_log)]);
}
};
void solve(int NT){
int n, m, q; cin >> n >> m >> q;
vector<vector<int>> g(n + 1);
int ok = 1;
for(int i = 1; i < n; ++i) {
int x, y; cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
ok &= (x == i) & (y == i + 1);
}
int timer = 1;
vector<pair<int, int>> euler_tour;
vector<int> t_in(n + 1), dep(n + 1), t_out(n + 1), occ(n + 1);
auto dfs = [&](auto dfs, int x, int d = 1) -> void {
t_in[x] = ++timer;
dep[x] = d;
occ[x] = euler_tour.size();
euler_tour.emplace_back(x, dep[x]);
for(int z: g[x]) {
if(dep[z] == 0) {
dfs(dfs, z, d + 1);
euler_tour.emplace_back(x, dep[x]);
}
}
t_out[x] = timer;
};
auto upper = [&](int x, int y) -> bool {
return t_in[x] <= t_in[y] && t_out[y] <= t_out[x];
};
dfs(dfs, 1);
SparseTable<pair<int, int>> sp(euler_tour, [&](auto x, auto y) {
if(x.second < y.second) return x;
return y;
});
auto lca = [&](int x, int y) -> int {
return sp.get(min(occ[x], occ[y]), max(occ[x], occ[y]) + 1).first;
};
vector<int> c(m);
for(int i = 0; i < m; ++i) cin >> c[i];
auto virt_tree = [&](int l, int r) -> int {
vector<int> v;
for(int i = l; i < r; ++i) v.emplace_back(c[i]);
sort(v.begin(), v.end(), [&](int x, int y){return t_in[x] < t_in[y];});
for(int i = 1; i < r - l; ++i) {
int nvert = lca(v[i - 1], v[i]);
v.emplace_back(nvert);
}
sort(v.begin(), v.end(), [&](int x, int y){return t_in[x] < t_in[y];});
v.erase(unique(v.begin(), v.end()), v.end());
int sz = v.size(), ans = 0;
vector<int> st;
st.emplace_back(v[0]);
for(int i = 1; i < sz; ++i) {
int x = v[i];
while(st.size() >= 2 && !upper(st.back(), x)) {
ans += dep[st.back()] - dep[st[st.size() - 2]];
st.pop_back();
}
st.emplace_back(x);
}
while (st.size() >= 2) {
ans += dep[st.back()] - dep[st[st.size() - 2]];
st.pop_back();
}
return ans + 1;
};
if(ok == 1) {
SparseTable<int> sp_mn(c, [&](int x, int y){return min(x, y);});
SparseTable<int> sp_mx(c, [&](int x, int y){return max(x, y);});
while(q--) {
int l, r; cin >> l >> r; l--;
int dis = sp_mx.get(l, r) - sp_mn.get(l, r) + 1;
cout << dis << '\n';
}
} else {
while(q--) {
int l, r; cin >> l >> r; l--;
cout << virt_tree(l, r) << '\n';
}
}
}
// #define TESTCASES
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
#ifdef TESTCASES
cin >> t;
#endif
for(int i = 1; i <= t; ++i){
solve(i);
cout << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
540 KB |
Output is correct |
12 |
Correct |
10 ms |
344 KB |
Output is correct |
13 |
Correct |
10 ms |
540 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
572 KB |
Output is correct |
17 |
Correct |
4 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
348 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
344 KB |
Output is correct |
23 |
Correct |
4 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
540 KB |
Output is correct |
12 |
Correct |
10 ms |
344 KB |
Output is correct |
13 |
Correct |
10 ms |
540 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
572 KB |
Output is correct |
17 |
Correct |
4 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
348 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
344 KB |
Output is correct |
23 |
Correct |
4 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
91 ms |
860 KB |
Output is correct |
31 |
Correct |
136 ms |
856 KB |
Output is correct |
32 |
Correct |
193 ms |
1048 KB |
Output is correct |
33 |
Correct |
192 ms |
856 KB |
Output is correct |
34 |
Correct |
208 ms |
860 KB |
Output is correct |
35 |
Correct |
620 ms |
1060 KB |
Output is correct |
36 |
Correct |
592 ms |
1312 KB |
Output is correct |
37 |
Correct |
609 ms |
1108 KB |
Output is correct |
38 |
Correct |
189 ms |
1112 KB |
Output is correct |
39 |
Correct |
188 ms |
1116 KB |
Output is correct |
40 |
Correct |
185 ms |
1488 KB |
Output is correct |
41 |
Correct |
670 ms |
1272 KB |
Output is correct |
42 |
Correct |
633 ms |
1288 KB |
Output is correct |
43 |
Correct |
653 ms |
1204 KB |
Output is correct |
44 |
Correct |
185 ms |
1148 KB |
Output is correct |
45 |
Correct |
205 ms |
1112 KB |
Output is correct |
46 |
Correct |
188 ms |
1116 KB |
Output is correct |
47 |
Correct |
638 ms |
1144 KB |
Output is correct |
48 |
Correct |
608 ms |
860 KB |
Output is correct |
49 |
Correct |
650 ms |
1144 KB |
Output is correct |
50 |
Correct |
140 ms |
1056 KB |
Output is correct |
51 |
Correct |
143 ms |
1056 KB |
Output is correct |
52 |
Correct |
145 ms |
856 KB |
Output is correct |
53 |
Correct |
133 ms |
1052 KB |
Output is correct |
54 |
Correct |
138 ms |
860 KB |
Output is correct |
55 |
Correct |
143 ms |
1052 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
2 ms |
1112 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
60 ms |
40344 KB |
Output is correct |
5 |
Correct |
62 ms |
50136 KB |
Output is correct |
6 |
Correct |
71 ms |
59860 KB |
Output is correct |
7 |
Correct |
91 ms |
67240 KB |
Output is correct |
8 |
Correct |
87 ms |
67296 KB |
Output is correct |
9 |
Correct |
88 ms |
67236 KB |
Output is correct |
10 |
Correct |
86 ms |
67236 KB |
Output is correct |
11 |
Correct |
87 ms |
67496 KB |
Output is correct |
12 |
Correct |
86 ms |
67116 KB |
Output is correct |
13 |
Correct |
84 ms |
66948 KB |
Output is correct |
14 |
Correct |
83 ms |
66984 KB |
Output is correct |
15 |
Correct |
62 ms |
52756 KB |
Output is correct |
16 |
Correct |
89 ms |
66844 KB |
Output is correct |
17 |
Correct |
38 ms |
16072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
36 ms |
20688 KB |
Output is correct |
3 |
Correct |
50 ms |
22480 KB |
Output is correct |
4 |
Correct |
48 ms |
26572 KB |
Output is correct |
5 |
Correct |
95 ms |
39880 KB |
Output is correct |
6 |
Correct |
82 ms |
38864 KB |
Output is correct |
7 |
Correct |
84 ms |
38888 KB |
Output is correct |
8 |
Correct |
78 ms |
38848 KB |
Output is correct |
9 |
Correct |
75 ms |
38840 KB |
Output is correct |
10 |
Correct |
74 ms |
38852 KB |
Output is correct |
11 |
Correct |
72 ms |
38848 KB |
Output is correct |
12 |
Correct |
71 ms |
38852 KB |
Output is correct |
13 |
Correct |
73 ms |
38848 KB |
Output is correct |
14 |
Correct |
86 ms |
39020 KB |
Output is correct |
15 |
Correct |
98 ms |
39060 KB |
Output is correct |
16 |
Correct |
78 ms |
38860 KB |
Output is correct |
17 |
Correct |
77 ms |
38852 KB |
Output is correct |
18 |
Correct |
82 ms |
38856 KB |
Output is correct |
19 |
Correct |
80 ms |
39588 KB |
Output is correct |
20 |
Correct |
78 ms |
39116 KB |
Output is correct |
21 |
Correct |
77 ms |
39108 KB |
Output is correct |
22 |
Correct |
85 ms |
38960 KB |
Output is correct |
23 |
Correct |
74 ms |
39104 KB |
Output is correct |
24 |
Correct |
76 ms |
39048 KB |
Output is correct |
25 |
Correct |
71 ms |
39104 KB |
Output is correct |
26 |
Correct |
75 ms |
39228 KB |
Output is correct |
27 |
Correct |
72 ms |
39112 KB |
Output is correct |
28 |
Correct |
81 ms |
39364 KB |
Output is correct |
29 |
Correct |
69 ms |
39108 KB |
Output is correct |
30 |
Correct |
70 ms |
39212 KB |
Output is correct |
31 |
Correct |
69 ms |
39112 KB |
Output is correct |
32 |
Correct |
72 ms |
39108 KB |
Output is correct |
33 |
Correct |
71 ms |
39104 KB |
Output is correct |
34 |
Correct |
75 ms |
39104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Execution timed out |
5076 ms |
22592 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
540 KB |
Output is correct |
12 |
Correct |
10 ms |
344 KB |
Output is correct |
13 |
Correct |
10 ms |
540 KB |
Output is correct |
14 |
Correct |
10 ms |
344 KB |
Output is correct |
15 |
Correct |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
572 KB |
Output is correct |
17 |
Correct |
4 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
348 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
344 KB |
Output is correct |
23 |
Correct |
4 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
3 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
91 ms |
860 KB |
Output is correct |
31 |
Correct |
136 ms |
856 KB |
Output is correct |
32 |
Correct |
193 ms |
1048 KB |
Output is correct |
33 |
Correct |
192 ms |
856 KB |
Output is correct |
34 |
Correct |
208 ms |
860 KB |
Output is correct |
35 |
Correct |
620 ms |
1060 KB |
Output is correct |
36 |
Correct |
592 ms |
1312 KB |
Output is correct |
37 |
Correct |
609 ms |
1108 KB |
Output is correct |
38 |
Correct |
189 ms |
1112 KB |
Output is correct |
39 |
Correct |
188 ms |
1116 KB |
Output is correct |
40 |
Correct |
185 ms |
1488 KB |
Output is correct |
41 |
Correct |
670 ms |
1272 KB |
Output is correct |
42 |
Correct |
633 ms |
1288 KB |
Output is correct |
43 |
Correct |
653 ms |
1204 KB |
Output is correct |
44 |
Correct |
185 ms |
1148 KB |
Output is correct |
45 |
Correct |
205 ms |
1112 KB |
Output is correct |
46 |
Correct |
188 ms |
1116 KB |
Output is correct |
47 |
Correct |
638 ms |
1144 KB |
Output is correct |
48 |
Correct |
608 ms |
860 KB |
Output is correct |
49 |
Correct |
650 ms |
1144 KB |
Output is correct |
50 |
Correct |
140 ms |
1056 KB |
Output is correct |
51 |
Correct |
143 ms |
1056 KB |
Output is correct |
52 |
Correct |
145 ms |
856 KB |
Output is correct |
53 |
Correct |
133 ms |
1052 KB |
Output is correct |
54 |
Correct |
138 ms |
860 KB |
Output is correct |
55 |
Correct |
143 ms |
1052 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
2 ms |
1112 KB |
Output is correct |
58 |
Correct |
2 ms |
860 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
60 ms |
40344 KB |
Output is correct |
63 |
Correct |
62 ms |
50136 KB |
Output is correct |
64 |
Correct |
71 ms |
59860 KB |
Output is correct |
65 |
Correct |
91 ms |
67240 KB |
Output is correct |
66 |
Correct |
87 ms |
67296 KB |
Output is correct |
67 |
Correct |
88 ms |
67236 KB |
Output is correct |
68 |
Correct |
86 ms |
67236 KB |
Output is correct |
69 |
Correct |
87 ms |
67496 KB |
Output is correct |
70 |
Correct |
86 ms |
67116 KB |
Output is correct |
71 |
Correct |
84 ms |
66948 KB |
Output is correct |
72 |
Correct |
83 ms |
66984 KB |
Output is correct |
73 |
Correct |
62 ms |
52756 KB |
Output is correct |
74 |
Correct |
89 ms |
66844 KB |
Output is correct |
75 |
Correct |
38 ms |
16072 KB |
Output is correct |
76 |
Correct |
0 ms |
344 KB |
Output is correct |
77 |
Correct |
36 ms |
20688 KB |
Output is correct |
78 |
Correct |
50 ms |
22480 KB |
Output is correct |
79 |
Correct |
48 ms |
26572 KB |
Output is correct |
80 |
Correct |
95 ms |
39880 KB |
Output is correct |
81 |
Correct |
82 ms |
38864 KB |
Output is correct |
82 |
Correct |
84 ms |
38888 KB |
Output is correct |
83 |
Correct |
78 ms |
38848 KB |
Output is correct |
84 |
Correct |
75 ms |
38840 KB |
Output is correct |
85 |
Correct |
74 ms |
38852 KB |
Output is correct |
86 |
Correct |
72 ms |
38848 KB |
Output is correct |
87 |
Correct |
71 ms |
38852 KB |
Output is correct |
88 |
Correct |
73 ms |
38848 KB |
Output is correct |
89 |
Correct |
86 ms |
39020 KB |
Output is correct |
90 |
Correct |
98 ms |
39060 KB |
Output is correct |
91 |
Correct |
78 ms |
38860 KB |
Output is correct |
92 |
Correct |
77 ms |
38852 KB |
Output is correct |
93 |
Correct |
82 ms |
38856 KB |
Output is correct |
94 |
Correct |
80 ms |
39588 KB |
Output is correct |
95 |
Correct |
78 ms |
39116 KB |
Output is correct |
96 |
Correct |
77 ms |
39108 KB |
Output is correct |
97 |
Correct |
85 ms |
38960 KB |
Output is correct |
98 |
Correct |
74 ms |
39104 KB |
Output is correct |
99 |
Correct |
76 ms |
39048 KB |
Output is correct |
100 |
Correct |
71 ms |
39104 KB |
Output is correct |
101 |
Correct |
75 ms |
39228 KB |
Output is correct |
102 |
Correct |
72 ms |
39112 KB |
Output is correct |
103 |
Correct |
81 ms |
39364 KB |
Output is correct |
104 |
Correct |
69 ms |
39108 KB |
Output is correct |
105 |
Correct |
70 ms |
39212 KB |
Output is correct |
106 |
Correct |
69 ms |
39112 KB |
Output is correct |
107 |
Correct |
72 ms |
39108 KB |
Output is correct |
108 |
Correct |
71 ms |
39104 KB |
Output is correct |
109 |
Correct |
75 ms |
39104 KB |
Output is correct |
110 |
Correct |
0 ms |
348 KB |
Output is correct |
111 |
Correct |
0 ms |
348 KB |
Output is correct |
112 |
Correct |
1 ms |
604 KB |
Output is correct |
113 |
Execution timed out |
5076 ms |
22592 KB |
Time limit exceeded |
114 |
Halted |
0 ms |
0 KB |
- |