# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1119286 |
2024-11-26T18:08:20 Z |
MrAndria |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
34124 KB |
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
const int MAXN = 200005;
int n, m, q, k, u, v, lg;
int tin[MAXN], tout[MAXN], val[MAXN], dst[MAXN], timer = 0;
int up[MAXN][25], a[MAXN], num[MAXN], ans[MAXN];
pair<pair<int, int>, int> query[MAXN];
vector<int> adj[MAXN];
set<int> active;
int sz = 0;
void dfs(int x, int p, int depth) {
tin[x] = timer++;
val[tin[x]] = x;
dst[x] = depth;
up[x][0] = p;
for (int i = 1; i <= lg; ++i)
up[x][i] = up[up[x][i - 1]][i - 1];
for (int neighbor : adj[x]) {
if (neighbor != p) {
dfs(neighbor, x, depth + 1);
}
}
tout[x] = timer++;
}
bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int i = lg; i >= 0; --i) {
if (!is_ancestor(up[u][i], v)) {
u = up[u][i];
}
}
return up[u][0];
}
set <int> :: iterator pre(set <int> :: iterator it){
it--;
return it;
}
set <int> :: iterator nex(set <int> :: iterator it){
it++;
return it;
}
inline int dist(int x, int y) {
return dst[x] + dst[y] - 2 * dst[lca(x, y)];
}
inline void add(int x) {
num[x]++;
if (num[x] > 1) return;
if (active.empty()) {
active.insert(tin[x]);
return;
}
auto it = active.insert(tin[x]).first;
auto prev = it == active.begin() ? --active.end() : pre(it);
auto next = nex(it) == active.end() ? active.begin() : nex(it);
sz += dist(val[*prev], val[*it]) + dist(val[*it], val[*next]) - dist(val[*prev], val[*next]);
}
inline void remove(int x) {
num[x]--;
if (num[x] > 0) return;
auto it = active.find(tin[x]);
auto prev = it == active.begin() ? --active.end() : pre(it);
auto next = nex(it) == active.end() ? active.begin() : nex(it);
sz -= dist(val[*prev], val[*it]) + dist(val[*it], val[*next]) - dist(val[*prev], val[*next]);
active.erase(it);
}
bool compare_queries(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
int block_a = a.ff.ff / k, block_b = b.ff.ff / k;
if (block_a != block_b) return block_a < block_b;
return block_a % 2 ? a.ff.ss > b.ff.ss : a.ff.ss < b.ff.ss;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> q;
lg = ceil(log2(n));
k = ceil(sqrt(m));
for (int i = 1; i < n; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
for (int i = 1; i <= q; i++) {
cin >> query[i].ff.ff >> query[i].ff.ss;
query[i].ss = i;
}
dfs(1, 1, 0);
sort(query + 1, query + q + 1, compare_queries);
int tl = 1, tr = 1;
add(a[tl]);
for (int i = 1; i <= q; i++) {
int l = query[i].ff.ff, r = query[i].ff.ss;
while (tr < r) add(a[++tr]);
while (tl > l) add(a[--tl]);
while (tr > r) remove(a[tr--]);
while (tl < l) remove(a[tl++]);
ans[query[i].ss] = (sz / 2) + 1;
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << " ";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5084 KB |
Output is correct |
2 |
Correct |
6 ms |
5168 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Correct |
7 ms |
5200 KB |
Output is correct |
5 |
Correct |
7 ms |
5200 KB |
Output is correct |
6 |
Correct |
6 ms |
5200 KB |
Output is correct |
7 |
Correct |
7 ms |
5200 KB |
Output is correct |
8 |
Correct |
6 ms |
5220 KB |
Output is correct |
9 |
Correct |
7 ms |
5200 KB |
Output is correct |
10 |
Correct |
5 ms |
5176 KB |
Output is correct |
11 |
Correct |
8 ms |
5200 KB |
Output is correct |
12 |
Correct |
5 ms |
5200 KB |
Output is correct |
13 |
Correct |
5 ms |
5200 KB |
Output is correct |
14 |
Correct |
5 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5200 KB |
Output is correct |
16 |
Correct |
5 ms |
5200 KB |
Output is correct |
17 |
Correct |
6 ms |
5388 KB |
Output is correct |
18 |
Correct |
6 ms |
5200 KB |
Output is correct |
19 |
Correct |
6 ms |
5320 KB |
Output is correct |
20 |
Correct |
6 ms |
5200 KB |
Output is correct |
21 |
Correct |
5 ms |
5204 KB |
Output is correct |
22 |
Correct |
5 ms |
5208 KB |
Output is correct |
23 |
Correct |
5 ms |
5212 KB |
Output is correct |
24 |
Correct |
5 ms |
5200 KB |
Output is correct |
25 |
Correct |
5 ms |
5204 KB |
Output is correct |
26 |
Correct |
6 ms |
5212 KB |
Output is correct |
27 |
Correct |
5 ms |
5200 KB |
Output is correct |
28 |
Correct |
4 ms |
5200 KB |
Output is correct |
29 |
Correct |
6 ms |
5284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5084 KB |
Output is correct |
2 |
Correct |
6 ms |
5168 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Correct |
7 ms |
5200 KB |
Output is correct |
5 |
Correct |
7 ms |
5200 KB |
Output is correct |
6 |
Correct |
6 ms |
5200 KB |
Output is correct |
7 |
Correct |
7 ms |
5200 KB |
Output is correct |
8 |
Correct |
6 ms |
5220 KB |
Output is correct |
9 |
Correct |
7 ms |
5200 KB |
Output is correct |
10 |
Correct |
5 ms |
5176 KB |
Output is correct |
11 |
Correct |
8 ms |
5200 KB |
Output is correct |
12 |
Correct |
5 ms |
5200 KB |
Output is correct |
13 |
Correct |
5 ms |
5200 KB |
Output is correct |
14 |
Correct |
5 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5200 KB |
Output is correct |
16 |
Correct |
5 ms |
5200 KB |
Output is correct |
17 |
Correct |
6 ms |
5388 KB |
Output is correct |
18 |
Correct |
6 ms |
5200 KB |
Output is correct |
19 |
Correct |
6 ms |
5320 KB |
Output is correct |
20 |
Correct |
6 ms |
5200 KB |
Output is correct |
21 |
Correct |
5 ms |
5204 KB |
Output is correct |
22 |
Correct |
5 ms |
5208 KB |
Output is correct |
23 |
Correct |
5 ms |
5212 KB |
Output is correct |
24 |
Correct |
5 ms |
5200 KB |
Output is correct |
25 |
Correct |
5 ms |
5204 KB |
Output is correct |
26 |
Correct |
6 ms |
5212 KB |
Output is correct |
27 |
Correct |
5 ms |
5200 KB |
Output is correct |
28 |
Correct |
4 ms |
5200 KB |
Output is correct |
29 |
Correct |
6 ms |
5284 KB |
Output is correct |
30 |
Correct |
14 ms |
5456 KB |
Output is correct |
31 |
Correct |
14 ms |
5552 KB |
Output is correct |
32 |
Correct |
17 ms |
5468 KB |
Output is correct |
33 |
Correct |
20 ms |
5676 KB |
Output is correct |
34 |
Correct |
26 ms |
5556 KB |
Output is correct |
35 |
Correct |
10 ms |
5468 KB |
Output is correct |
36 |
Correct |
9 ms |
5528 KB |
Output is correct |
37 |
Correct |
11 ms |
5456 KB |
Output is correct |
38 |
Correct |
16 ms |
5712 KB |
Output is correct |
39 |
Correct |
13 ms |
5596 KB |
Output is correct |
40 |
Correct |
16 ms |
5740 KB |
Output is correct |
41 |
Correct |
10 ms |
5712 KB |
Output is correct |
42 |
Correct |
7 ms |
5712 KB |
Output is correct |
43 |
Correct |
10 ms |
5712 KB |
Output is correct |
44 |
Correct |
17 ms |
5520 KB |
Output is correct |
45 |
Correct |
14 ms |
5520 KB |
Output is correct |
46 |
Correct |
16 ms |
5712 KB |
Output is correct |
47 |
Correct |
10 ms |
5756 KB |
Output is correct |
48 |
Correct |
9 ms |
5692 KB |
Output is correct |
49 |
Correct |
7 ms |
5784 KB |
Output is correct |
50 |
Correct |
20 ms |
5672 KB |
Output is correct |
51 |
Correct |
24 ms |
5696 KB |
Output is correct |
52 |
Correct |
24 ms |
5668 KB |
Output is correct |
53 |
Correct |
22 ms |
5676 KB |
Output is correct |
54 |
Correct |
19 ms |
5672 KB |
Output is correct |
55 |
Correct |
26 ms |
5456 KB |
Output is correct |
56 |
Correct |
6 ms |
5088 KB |
Output is correct |
57 |
Correct |
7 ms |
5456 KB |
Output is correct |
58 |
Correct |
11 ms |
5456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5180 KB |
Output is correct |
2 |
Correct |
5 ms |
5200 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Correct |
3292 ms |
22728 KB |
Output is correct |
5 |
Correct |
2281 ms |
27688 KB |
Output is correct |
6 |
Correct |
3227 ms |
30740 KB |
Output is correct |
7 |
Execution timed out |
5054 ms |
34124 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5200 KB |
Output is correct |
2 |
Correct |
119 ms |
14812 KB |
Output is correct |
3 |
Correct |
190 ms |
15904 KB |
Output is correct |
4 |
Correct |
156 ms |
17000 KB |
Output is correct |
5 |
Correct |
132 ms |
25456 KB |
Output is correct |
6 |
Correct |
182 ms |
24776 KB |
Output is correct |
7 |
Correct |
246 ms |
23536 KB |
Output is correct |
8 |
Correct |
246 ms |
22792 KB |
Output is correct |
9 |
Correct |
211 ms |
22776 KB |
Output is correct |
10 |
Correct |
230 ms |
22576 KB |
Output is correct |
11 |
Correct |
261 ms |
22532 KB |
Output is correct |
12 |
Correct |
263 ms |
22764 KB |
Output is correct |
13 |
Correct |
281 ms |
22856 KB |
Output is correct |
14 |
Correct |
314 ms |
23368 KB |
Output is correct |
15 |
Correct |
280 ms |
25588 KB |
Output is correct |
16 |
Correct |
250 ms |
24496 KB |
Output is correct |
17 |
Correct |
223 ms |
24660 KB |
Output is correct |
18 |
Correct |
307 ms |
24880 KB |
Output is correct |
19 |
Correct |
112 ms |
25484 KB |
Output is correct |
20 |
Correct |
105 ms |
25524 KB |
Output is correct |
21 |
Correct |
133 ms |
23980 KB |
Output is correct |
22 |
Correct |
142 ms |
23348 KB |
Output is correct |
23 |
Correct |
124 ms |
22992 KB |
Output is correct |
24 |
Correct |
133 ms |
22836 KB |
Output is correct |
25 |
Correct |
168 ms |
22600 KB |
Output is correct |
26 |
Correct |
163 ms |
22596 KB |
Output is correct |
27 |
Correct |
221 ms |
22600 KB |
Output is correct |
28 |
Correct |
235 ms |
22356 KB |
Output is correct |
29 |
Correct |
263 ms |
22596 KB |
Output is correct |
30 |
Correct |
284 ms |
22600 KB |
Output is correct |
31 |
Correct |
295 ms |
22856 KB |
Output is correct |
32 |
Correct |
274 ms |
23264 KB |
Output is correct |
33 |
Correct |
298 ms |
24136 KB |
Output is correct |
34 |
Correct |
306 ms |
25652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5200 KB |
Output is correct |
2 |
Correct |
6 ms |
5200 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Execution timed out |
5097 ms |
19800 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5084 KB |
Output is correct |
2 |
Correct |
6 ms |
5168 KB |
Output is correct |
3 |
Correct |
5 ms |
5200 KB |
Output is correct |
4 |
Correct |
7 ms |
5200 KB |
Output is correct |
5 |
Correct |
7 ms |
5200 KB |
Output is correct |
6 |
Correct |
6 ms |
5200 KB |
Output is correct |
7 |
Correct |
7 ms |
5200 KB |
Output is correct |
8 |
Correct |
6 ms |
5220 KB |
Output is correct |
9 |
Correct |
7 ms |
5200 KB |
Output is correct |
10 |
Correct |
5 ms |
5176 KB |
Output is correct |
11 |
Correct |
8 ms |
5200 KB |
Output is correct |
12 |
Correct |
5 ms |
5200 KB |
Output is correct |
13 |
Correct |
5 ms |
5200 KB |
Output is correct |
14 |
Correct |
5 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5200 KB |
Output is correct |
16 |
Correct |
5 ms |
5200 KB |
Output is correct |
17 |
Correct |
6 ms |
5388 KB |
Output is correct |
18 |
Correct |
6 ms |
5200 KB |
Output is correct |
19 |
Correct |
6 ms |
5320 KB |
Output is correct |
20 |
Correct |
6 ms |
5200 KB |
Output is correct |
21 |
Correct |
5 ms |
5204 KB |
Output is correct |
22 |
Correct |
5 ms |
5208 KB |
Output is correct |
23 |
Correct |
5 ms |
5212 KB |
Output is correct |
24 |
Correct |
5 ms |
5200 KB |
Output is correct |
25 |
Correct |
5 ms |
5204 KB |
Output is correct |
26 |
Correct |
6 ms |
5212 KB |
Output is correct |
27 |
Correct |
5 ms |
5200 KB |
Output is correct |
28 |
Correct |
4 ms |
5200 KB |
Output is correct |
29 |
Correct |
6 ms |
5284 KB |
Output is correct |
30 |
Correct |
14 ms |
5456 KB |
Output is correct |
31 |
Correct |
14 ms |
5552 KB |
Output is correct |
32 |
Correct |
17 ms |
5468 KB |
Output is correct |
33 |
Correct |
20 ms |
5676 KB |
Output is correct |
34 |
Correct |
26 ms |
5556 KB |
Output is correct |
35 |
Correct |
10 ms |
5468 KB |
Output is correct |
36 |
Correct |
9 ms |
5528 KB |
Output is correct |
37 |
Correct |
11 ms |
5456 KB |
Output is correct |
38 |
Correct |
16 ms |
5712 KB |
Output is correct |
39 |
Correct |
13 ms |
5596 KB |
Output is correct |
40 |
Correct |
16 ms |
5740 KB |
Output is correct |
41 |
Correct |
10 ms |
5712 KB |
Output is correct |
42 |
Correct |
7 ms |
5712 KB |
Output is correct |
43 |
Correct |
10 ms |
5712 KB |
Output is correct |
44 |
Correct |
17 ms |
5520 KB |
Output is correct |
45 |
Correct |
14 ms |
5520 KB |
Output is correct |
46 |
Correct |
16 ms |
5712 KB |
Output is correct |
47 |
Correct |
10 ms |
5756 KB |
Output is correct |
48 |
Correct |
9 ms |
5692 KB |
Output is correct |
49 |
Correct |
7 ms |
5784 KB |
Output is correct |
50 |
Correct |
20 ms |
5672 KB |
Output is correct |
51 |
Correct |
24 ms |
5696 KB |
Output is correct |
52 |
Correct |
24 ms |
5668 KB |
Output is correct |
53 |
Correct |
22 ms |
5676 KB |
Output is correct |
54 |
Correct |
19 ms |
5672 KB |
Output is correct |
55 |
Correct |
26 ms |
5456 KB |
Output is correct |
56 |
Correct |
6 ms |
5088 KB |
Output is correct |
57 |
Correct |
7 ms |
5456 KB |
Output is correct |
58 |
Correct |
11 ms |
5456 KB |
Output is correct |
59 |
Correct |
7 ms |
5180 KB |
Output is correct |
60 |
Correct |
5 ms |
5200 KB |
Output is correct |
61 |
Correct |
5 ms |
5200 KB |
Output is correct |
62 |
Correct |
3292 ms |
22728 KB |
Output is correct |
63 |
Correct |
2281 ms |
27688 KB |
Output is correct |
64 |
Correct |
3227 ms |
30740 KB |
Output is correct |
65 |
Execution timed out |
5054 ms |
34124 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |