# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1119293 |
2024-11-26T18:14:48 Z |
MrAndria |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
33908 KB |
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#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++;
}
inline bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
inline 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];
}
inline set <int> :: iterator pre(set <int> :: iterator it){
it--;
return it;
}
inline 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);
}
inline 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;
}
signed main() {
scanf("%d%d%d",&n,&m,&q);
lg = ceil(log2(n));
k = ceil(sqrt(m));
for (int i = 1; i < n; i++) {
scanf("%d%d",&u,&v);
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= m; i++) {
scanf("%d",&a[i]);
}
for (int i = 1; i <= q; i++) {
scanf("%d%d",&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++) {
printf("%d ",ans[i]);
}
}
Compilation message
tourism.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
tourism.cpp: In function 'int main()':
tourism.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d%d%d",&n,&m,&q);
| ~~~~~^~~~~~~~~~~~~~~~~~~
tourism.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
tourism.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | scanf("%d",&a[i]);
| ~~~~~^~~~~~~~~~~~
tourism.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d%d",&query[i].ff.ff,&query[i].ff.ss);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5148 KB |
Output is correct |
2 |
Correct |
6 ms |
4944 KB |
Output is correct |
3 |
Correct |
6 ms |
4944 KB |
Output is correct |
4 |
Correct |
7 ms |
5224 KB |
Output is correct |
5 |
Correct |
6 ms |
5032 KB |
Output is correct |
6 |
Correct |
8 ms |
5372 KB |
Output is correct |
7 |
Correct |
5 ms |
5200 KB |
Output is correct |
8 |
Correct |
7 ms |
5068 KB |
Output is correct |
9 |
Correct |
8 ms |
5200 KB |
Output is correct |
10 |
Correct |
8 ms |
5152 KB |
Output is correct |
11 |
Correct |
5 ms |
5200 KB |
Output is correct |
12 |
Correct |
7 ms |
5200 KB |
Output is correct |
13 |
Correct |
6 ms |
5200 KB |
Output is correct |
14 |
Correct |
6 ms |
5200 KB |
Output is correct |
15 |
Correct |
6 ms |
5152 KB |
Output is correct |
16 |
Correct |
6 ms |
5200 KB |
Output is correct |
17 |
Correct |
6 ms |
5200 KB |
Output is correct |
18 |
Correct |
5 ms |
5200 KB |
Output is correct |
19 |
Correct |
9 ms |
5200 KB |
Output is correct |
20 |
Correct |
6 ms |
5200 KB |
Output is correct |
21 |
Correct |
7 ms |
5200 KB |
Output is correct |
22 |
Correct |
7 ms |
5208 KB |
Output is correct |
23 |
Correct |
8 ms |
5200 KB |
Output is correct |
24 |
Correct |
5 ms |
5200 KB |
Output is correct |
25 |
Correct |
6 ms |
5200 KB |
Output is correct |
26 |
Correct |
7 ms |
5200 KB |
Output is correct |
27 |
Correct |
5 ms |
5200 KB |
Output is correct |
28 |
Correct |
6 ms |
5200 KB |
Output is correct |
29 |
Correct |
6 ms |
5200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5148 KB |
Output is correct |
2 |
Correct |
6 ms |
4944 KB |
Output is correct |
3 |
Correct |
6 ms |
4944 KB |
Output is correct |
4 |
Correct |
7 ms |
5224 KB |
Output is correct |
5 |
Correct |
6 ms |
5032 KB |
Output is correct |
6 |
Correct |
8 ms |
5372 KB |
Output is correct |
7 |
Correct |
5 ms |
5200 KB |
Output is correct |
8 |
Correct |
7 ms |
5068 KB |
Output is correct |
9 |
Correct |
8 ms |
5200 KB |
Output is correct |
10 |
Correct |
8 ms |
5152 KB |
Output is correct |
11 |
Correct |
5 ms |
5200 KB |
Output is correct |
12 |
Correct |
7 ms |
5200 KB |
Output is correct |
13 |
Correct |
6 ms |
5200 KB |
Output is correct |
14 |
Correct |
6 ms |
5200 KB |
Output is correct |
15 |
Correct |
6 ms |
5152 KB |
Output is correct |
16 |
Correct |
6 ms |
5200 KB |
Output is correct |
17 |
Correct |
6 ms |
5200 KB |
Output is correct |
18 |
Correct |
5 ms |
5200 KB |
Output is correct |
19 |
Correct |
9 ms |
5200 KB |
Output is correct |
20 |
Correct |
6 ms |
5200 KB |
Output is correct |
21 |
Correct |
7 ms |
5200 KB |
Output is correct |
22 |
Correct |
7 ms |
5208 KB |
Output is correct |
23 |
Correct |
8 ms |
5200 KB |
Output is correct |
24 |
Correct |
5 ms |
5200 KB |
Output is correct |
25 |
Correct |
6 ms |
5200 KB |
Output is correct |
26 |
Correct |
7 ms |
5200 KB |
Output is correct |
27 |
Correct |
5 ms |
5200 KB |
Output is correct |
28 |
Correct |
6 ms |
5200 KB |
Output is correct |
29 |
Correct |
6 ms |
5200 KB |
Output is correct |
30 |
Correct |
12 ms |
5308 KB |
Output is correct |
31 |
Correct |
19 ms |
5456 KB |
Output is correct |
32 |
Correct |
21 ms |
5648 KB |
Output is correct |
33 |
Correct |
17 ms |
5640 KB |
Output is correct |
34 |
Correct |
19 ms |
5500 KB |
Output is correct |
35 |
Correct |
9 ms |
5456 KB |
Output is correct |
36 |
Correct |
9 ms |
5456 KB |
Output is correct |
37 |
Correct |
8 ms |
5456 KB |
Output is correct |
38 |
Correct |
15 ms |
5724 KB |
Output is correct |
39 |
Correct |
11 ms |
5712 KB |
Output is correct |
40 |
Correct |
15 ms |
5900 KB |
Output is correct |
41 |
Correct |
9 ms |
5716 KB |
Output is correct |
42 |
Correct |
8 ms |
5712 KB |
Output is correct |
43 |
Correct |
7 ms |
5712 KB |
Output is correct |
44 |
Correct |
16 ms |
5928 KB |
Output is correct |
45 |
Correct |
12 ms |
5456 KB |
Output is correct |
46 |
Correct |
14 ms |
5592 KB |
Output is correct |
47 |
Correct |
7 ms |
5456 KB |
Output is correct |
48 |
Correct |
8 ms |
5456 KB |
Output is correct |
49 |
Correct |
8 ms |
5456 KB |
Output is correct |
50 |
Correct |
19 ms |
5648 KB |
Output is correct |
51 |
Correct |
26 ms |
5412 KB |
Output is correct |
52 |
Correct |
18 ms |
5456 KB |
Output is correct |
53 |
Correct |
22 ms |
5456 KB |
Output is correct |
54 |
Correct |
23 ms |
5416 KB |
Output is correct |
55 |
Correct |
22 ms |
5456 KB |
Output is correct |
56 |
Correct |
7 ms |
5200 KB |
Output is correct |
57 |
Correct |
8 ms |
5484 KB |
Output is correct |
58 |
Correct |
10 ms |
5456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4944 KB |
Output is correct |
2 |
Correct |
5 ms |
5200 KB |
Output is correct |
3 |
Correct |
9 ms |
5200 KB |
Output is correct |
4 |
Correct |
3364 ms |
22708 KB |
Output is correct |
5 |
Correct |
2013 ms |
27652 KB |
Output is correct |
6 |
Correct |
3333 ms |
30540 KB |
Output is correct |
7 |
Execution timed out |
5045 ms |
33908 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4944 KB |
Output is correct |
2 |
Correct |
149 ms |
14548 KB |
Output is correct |
3 |
Correct |
227 ms |
15872 KB |
Output is correct |
4 |
Correct |
189 ms |
17048 KB |
Output is correct |
5 |
Correct |
169 ms |
25416 KB |
Output is correct |
6 |
Correct |
199 ms |
24868 KB |
Output is correct |
7 |
Correct |
237 ms |
23456 KB |
Output is correct |
8 |
Correct |
263 ms |
22808 KB |
Output is correct |
9 |
Correct |
228 ms |
22600 KB |
Output is correct |
10 |
Correct |
211 ms |
22688 KB |
Output is correct |
11 |
Correct |
277 ms |
22616 KB |
Output is correct |
12 |
Correct |
300 ms |
22768 KB |
Output is correct |
13 |
Correct |
263 ms |
22856 KB |
Output is correct |
14 |
Correct |
296 ms |
23360 KB |
Output is correct |
15 |
Correct |
309 ms |
25360 KB |
Output is correct |
16 |
Correct |
251 ms |
24456 KB |
Output is correct |
17 |
Correct |
243 ms |
24588 KB |
Output is correct |
18 |
Correct |
237 ms |
24616 KB |
Output is correct |
19 |
Correct |
86 ms |
25416 KB |
Output is correct |
20 |
Correct |
103 ms |
25528 KB |
Output is correct |
21 |
Correct |
113 ms |
23956 KB |
Output is correct |
22 |
Correct |
141 ms |
23292 KB |
Output is correct |
23 |
Correct |
128 ms |
23112 KB |
Output is correct |
24 |
Correct |
144 ms |
22756 KB |
Output is correct |
25 |
Correct |
145 ms |
22664 KB |
Output is correct |
26 |
Correct |
185 ms |
22512 KB |
Output is correct |
27 |
Correct |
216 ms |
22540 KB |
Output is correct |
28 |
Correct |
288 ms |
22680 KB |
Output is correct |
29 |
Correct |
273 ms |
22632 KB |
Output is correct |
30 |
Correct |
300 ms |
22856 KB |
Output is correct |
31 |
Correct |
269 ms |
22856 KB |
Output is correct |
32 |
Correct |
282 ms |
23120 KB |
Output is correct |
33 |
Correct |
338 ms |
24136 KB |
Output is correct |
34 |
Correct |
360 ms |
25504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4944 KB |
Output is correct |
2 |
Correct |
6 ms |
5200 KB |
Output is correct |
3 |
Correct |
7 ms |
5200 KB |
Output is correct |
4 |
Execution timed out |
5089 ms |
19664 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5148 KB |
Output is correct |
2 |
Correct |
6 ms |
4944 KB |
Output is correct |
3 |
Correct |
6 ms |
4944 KB |
Output is correct |
4 |
Correct |
7 ms |
5224 KB |
Output is correct |
5 |
Correct |
6 ms |
5032 KB |
Output is correct |
6 |
Correct |
8 ms |
5372 KB |
Output is correct |
7 |
Correct |
5 ms |
5200 KB |
Output is correct |
8 |
Correct |
7 ms |
5068 KB |
Output is correct |
9 |
Correct |
8 ms |
5200 KB |
Output is correct |
10 |
Correct |
8 ms |
5152 KB |
Output is correct |
11 |
Correct |
5 ms |
5200 KB |
Output is correct |
12 |
Correct |
7 ms |
5200 KB |
Output is correct |
13 |
Correct |
6 ms |
5200 KB |
Output is correct |
14 |
Correct |
6 ms |
5200 KB |
Output is correct |
15 |
Correct |
6 ms |
5152 KB |
Output is correct |
16 |
Correct |
6 ms |
5200 KB |
Output is correct |
17 |
Correct |
6 ms |
5200 KB |
Output is correct |
18 |
Correct |
5 ms |
5200 KB |
Output is correct |
19 |
Correct |
9 ms |
5200 KB |
Output is correct |
20 |
Correct |
6 ms |
5200 KB |
Output is correct |
21 |
Correct |
7 ms |
5200 KB |
Output is correct |
22 |
Correct |
7 ms |
5208 KB |
Output is correct |
23 |
Correct |
8 ms |
5200 KB |
Output is correct |
24 |
Correct |
5 ms |
5200 KB |
Output is correct |
25 |
Correct |
6 ms |
5200 KB |
Output is correct |
26 |
Correct |
7 ms |
5200 KB |
Output is correct |
27 |
Correct |
5 ms |
5200 KB |
Output is correct |
28 |
Correct |
6 ms |
5200 KB |
Output is correct |
29 |
Correct |
6 ms |
5200 KB |
Output is correct |
30 |
Correct |
12 ms |
5308 KB |
Output is correct |
31 |
Correct |
19 ms |
5456 KB |
Output is correct |
32 |
Correct |
21 ms |
5648 KB |
Output is correct |
33 |
Correct |
17 ms |
5640 KB |
Output is correct |
34 |
Correct |
19 ms |
5500 KB |
Output is correct |
35 |
Correct |
9 ms |
5456 KB |
Output is correct |
36 |
Correct |
9 ms |
5456 KB |
Output is correct |
37 |
Correct |
8 ms |
5456 KB |
Output is correct |
38 |
Correct |
15 ms |
5724 KB |
Output is correct |
39 |
Correct |
11 ms |
5712 KB |
Output is correct |
40 |
Correct |
15 ms |
5900 KB |
Output is correct |
41 |
Correct |
9 ms |
5716 KB |
Output is correct |
42 |
Correct |
8 ms |
5712 KB |
Output is correct |
43 |
Correct |
7 ms |
5712 KB |
Output is correct |
44 |
Correct |
16 ms |
5928 KB |
Output is correct |
45 |
Correct |
12 ms |
5456 KB |
Output is correct |
46 |
Correct |
14 ms |
5592 KB |
Output is correct |
47 |
Correct |
7 ms |
5456 KB |
Output is correct |
48 |
Correct |
8 ms |
5456 KB |
Output is correct |
49 |
Correct |
8 ms |
5456 KB |
Output is correct |
50 |
Correct |
19 ms |
5648 KB |
Output is correct |
51 |
Correct |
26 ms |
5412 KB |
Output is correct |
52 |
Correct |
18 ms |
5456 KB |
Output is correct |
53 |
Correct |
22 ms |
5456 KB |
Output is correct |
54 |
Correct |
23 ms |
5416 KB |
Output is correct |
55 |
Correct |
22 ms |
5456 KB |
Output is correct |
56 |
Correct |
7 ms |
5200 KB |
Output is correct |
57 |
Correct |
8 ms |
5484 KB |
Output is correct |
58 |
Correct |
10 ms |
5456 KB |
Output is correct |
59 |
Correct |
6 ms |
4944 KB |
Output is correct |
60 |
Correct |
5 ms |
5200 KB |
Output is correct |
61 |
Correct |
9 ms |
5200 KB |
Output is correct |
62 |
Correct |
3364 ms |
22708 KB |
Output is correct |
63 |
Correct |
2013 ms |
27652 KB |
Output is correct |
64 |
Correct |
3333 ms |
30540 KB |
Output is correct |
65 |
Execution timed out |
5045 ms |
33908 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |