# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1001260 |
2024-06-18T17:34:19 Z |
lam |
Tourism (JOI23_tourism) |
C++14 |
|
5000 ms |
42832 KB |
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
const int maxn = 1e5 + 5, bl = sqrt(maxn);
int n, m, q, cnt, cur;
int in[maxn], out[maxn], h[maxn], par[18][maxn], id[maxn], c[maxn], ans[maxn], in_set[maxn];
vector<int> adj[maxn];
struct cmp
{
bool operator() (int x, int y) const
{
return in[x] < in[y];
}
};
set<int, cmp> s;
struct Q{
int l, r, id;
bool operator < (Q b) const
{
if (l / bl != b.l / bl) return l < b.l;
return (l / bl & 1) ? (r < b.r) : (r > b.r);
}
} qr[maxn];
void dfs(int x = 1, int p = 1)
{
par[0][x] = p;
h[x] = h[p] + 1;
for (int i = 1; i < 18; i++)
par[i][x] = par[i - 1][par[i - 1][x]];
in[x] = ++cnt;
for (int i : adj[x]) if (i != p)
dfs(i, x);
out[x] = cnt;
}
inline bool sub(int u, int v) /// u in subtree of v
{
return in[v] <= in[u] && out[u] <= out[v];
}
map<pair<int,int>,int> mp;
int lca(int u, int v)
{
if (u > v) swap(u, v);
pair<int,int> tmp = {u, v};
if (mp.count(tmp)) return mp[tmp];
if (h[u] > h[v]) swap(u, v);
if (sub(v, u)) return mp[tmp] = u;;
for (int i = 17; i >= 0; i--)
if (sub(u, par[i][v]) == 0)
v = par[i][v];
return mp[tmp] = par[0][v];
}
void add(int x)
{
x = c[x];
in_set[x]++;
if (in_set[x] == 1)
{
cur += h[x];
if (s.size() == 0) return s.emplace(x), void();
auto it = s.lower_bound(x);
if (it == s.begin())
cur -= h[lca(x, *it)];
else if (it == s.end())
cur -= h[lca(x, *prev(it))];
else
{
auto u = *it, v = *prev(it);
cur += h[lca(u, v)];
cur -= h[lca(u, x)];
cur -= h[lca(v, x)];
}
}
s.emplace(x);
}
void del(int x)
{
x = c[x];
in_set[x]--;
if (in_set[x] == 0)
{
cur -= h[x];
if (s.size() == 1) return s.clear(), void();
auto it = s.lower_bound(x);
if (it == s.begin())
cur += h[lca(x, *next(it))];
else if (next(it) == s.end())
cur += h[lca(x, *prev(it))];
else
{
auto u = *prev(it), v = *next(it);
cur -= h[lca(u, v)];
cur += h[lca(u, x)];
cur += h[lca(v, x)];
}
s.erase(it);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
cin >> n >> m >> q;
for (int i = 1, u, v; i < n; i++)
cin >> u >> v,
adj[u].emplace_back(v),
adj[v].emplace_back(u);
for (int i = 1; i <= m; i++)
cin >> c[i], id[i] = (i - 1) / bl + 1;
dfs();
for (int i = 1; i <= q; i++)
cin >> qr[i].l >> qr[i].r, qr[i].id = i;
sort(qr + 1, qr + q + 1);
for (int i = 1, l = 1, r = 0; i <= q; i++)
{
auto [lx, rx, id] = qr[i];
while (r < rx) add(++r);
while (l > lx) add(--l);
while (r > rx) del(r--);
while (l < lx) del(l++);
ans[id] = cur - h[lca(*s.begin(), *s.rbegin())] + 1;
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:129:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
129 | auto [lx, rx, id] = qr[i];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
6 ms |
10588 KB |
Output is correct |
8 |
Correct |
4 ms |
10584 KB |
Output is correct |
9 |
Correct |
6 ms |
10840 KB |
Output is correct |
10 |
Correct |
6 ms |
10836 KB |
Output is correct |
11 |
Correct |
8 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10812 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
6 ms |
10824 KB |
Output is correct |
16 |
Correct |
6 ms |
10844 KB |
Output is correct |
17 |
Correct |
6 ms |
10844 KB |
Output is correct |
18 |
Correct |
6 ms |
10868 KB |
Output is correct |
19 |
Correct |
6 ms |
10844 KB |
Output is correct |
20 |
Correct |
6 ms |
10844 KB |
Output is correct |
21 |
Correct |
7 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10844 KB |
Output is correct |
24 |
Correct |
7 ms |
10832 KB |
Output is correct |
25 |
Correct |
7 ms |
10844 KB |
Output is correct |
26 |
Correct |
6 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
6 ms |
10588 KB |
Output is correct |
8 |
Correct |
4 ms |
10584 KB |
Output is correct |
9 |
Correct |
6 ms |
10840 KB |
Output is correct |
10 |
Correct |
6 ms |
10836 KB |
Output is correct |
11 |
Correct |
8 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10812 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
6 ms |
10824 KB |
Output is correct |
16 |
Correct |
6 ms |
10844 KB |
Output is correct |
17 |
Correct |
6 ms |
10844 KB |
Output is correct |
18 |
Correct |
6 ms |
10868 KB |
Output is correct |
19 |
Correct |
6 ms |
10844 KB |
Output is correct |
20 |
Correct |
6 ms |
10844 KB |
Output is correct |
21 |
Correct |
7 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10844 KB |
Output is correct |
24 |
Correct |
7 ms |
10832 KB |
Output is correct |
25 |
Correct |
7 ms |
10844 KB |
Output is correct |
26 |
Correct |
6 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
51 ms |
11228 KB |
Output is correct |
31 |
Correct |
56 ms |
11416 KB |
Output is correct |
32 |
Correct |
74 ms |
11700 KB |
Output is correct |
33 |
Correct |
77 ms |
11604 KB |
Output is correct |
34 |
Correct |
90 ms |
11600 KB |
Output is correct |
35 |
Correct |
6 ms |
11100 KB |
Output is correct |
36 |
Correct |
6 ms |
11124 KB |
Output is correct |
37 |
Correct |
6 ms |
11040 KB |
Output is correct |
38 |
Correct |
88 ms |
11612 KB |
Output is correct |
39 |
Correct |
77 ms |
11724 KB |
Output is correct |
40 |
Correct |
71 ms |
11692 KB |
Output is correct |
41 |
Correct |
6 ms |
11096 KB |
Output is correct |
42 |
Correct |
6 ms |
11096 KB |
Output is correct |
43 |
Correct |
6 ms |
11136 KB |
Output is correct |
44 |
Correct |
78 ms |
11624 KB |
Output is correct |
45 |
Correct |
75 ms |
11580 KB |
Output is correct |
46 |
Correct |
64 ms |
11604 KB |
Output is correct |
47 |
Correct |
6 ms |
11100 KB |
Output is correct |
48 |
Correct |
5 ms |
11100 KB |
Output is correct |
49 |
Correct |
5 ms |
11100 KB |
Output is correct |
50 |
Correct |
64 ms |
11564 KB |
Output is correct |
51 |
Correct |
62 ms |
11600 KB |
Output is correct |
52 |
Correct |
60 ms |
11628 KB |
Output is correct |
53 |
Correct |
63 ms |
11604 KB |
Output is correct |
54 |
Correct |
71 ms |
11604 KB |
Output is correct |
55 |
Correct |
63 ms |
11604 KB |
Output is correct |
56 |
Correct |
4 ms |
10584 KB |
Output is correct |
57 |
Correct |
2 ms |
10844 KB |
Output is correct |
58 |
Correct |
6 ms |
11100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Execution timed out |
5092 ms |
35048 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
273 ms |
27580 KB |
Output is correct |
3 |
Correct |
450 ms |
35924 KB |
Output is correct |
4 |
Correct |
338 ms |
31312 KB |
Output is correct |
5 |
Correct |
176 ms |
27484 KB |
Output is correct |
6 |
Correct |
244 ms |
29544 KB |
Output is correct |
7 |
Correct |
385 ms |
32340 KB |
Output is correct |
8 |
Correct |
372 ms |
34180 KB |
Output is correct |
9 |
Correct |
442 ms |
35152 KB |
Output is correct |
10 |
Correct |
380 ms |
35460 KB |
Output is correct |
11 |
Correct |
573 ms |
39056 KB |
Output is correct |
12 |
Correct |
546 ms |
41808 KB |
Output is correct |
13 |
Correct |
485 ms |
42688 KB |
Output is correct |
14 |
Correct |
483 ms |
40988 KB |
Output is correct |
15 |
Correct |
408 ms |
36548 KB |
Output is correct |
16 |
Correct |
431 ms |
39036 KB |
Output is correct |
17 |
Correct |
426 ms |
38992 KB |
Output is correct |
18 |
Correct |
429 ms |
39000 KB |
Output is correct |
19 |
Correct |
150 ms |
27472 KB |
Output is correct |
20 |
Correct |
200 ms |
28928 KB |
Output is correct |
21 |
Correct |
273 ms |
30548 KB |
Output is correct |
22 |
Correct |
329 ms |
32336 KB |
Output is correct |
23 |
Correct |
336 ms |
33640 KB |
Output is correct |
24 |
Correct |
385 ms |
34528 KB |
Output is correct |
25 |
Correct |
388 ms |
34872 KB |
Output is correct |
26 |
Correct |
376 ms |
35280 KB |
Output is correct |
27 |
Correct |
462 ms |
36848 KB |
Output is correct |
28 |
Correct |
525 ms |
38784 KB |
Output is correct |
29 |
Correct |
636 ms |
40784 KB |
Output is correct |
30 |
Correct |
618 ms |
42472 KB |
Output is correct |
31 |
Correct |
555 ms |
42832 KB |
Output is correct |
32 |
Correct |
531 ms |
42256 KB |
Output is correct |
33 |
Correct |
497 ms |
38988 KB |
Output is correct |
34 |
Correct |
456 ms |
36356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Execution timed out |
5090 ms |
30624 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 KB |
Output is correct |
4 |
Correct |
4 ms |
10588 KB |
Output is correct |
5 |
Correct |
4 ms |
10584 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
6 ms |
10588 KB |
Output is correct |
8 |
Correct |
4 ms |
10584 KB |
Output is correct |
9 |
Correct |
6 ms |
10840 KB |
Output is correct |
10 |
Correct |
6 ms |
10836 KB |
Output is correct |
11 |
Correct |
8 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10812 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
6 ms |
10824 KB |
Output is correct |
16 |
Correct |
6 ms |
10844 KB |
Output is correct |
17 |
Correct |
6 ms |
10844 KB |
Output is correct |
18 |
Correct |
6 ms |
10868 KB |
Output is correct |
19 |
Correct |
6 ms |
10844 KB |
Output is correct |
20 |
Correct |
6 ms |
10844 KB |
Output is correct |
21 |
Correct |
7 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10844 KB |
Output is correct |
24 |
Correct |
7 ms |
10832 KB |
Output is correct |
25 |
Correct |
7 ms |
10844 KB |
Output is correct |
26 |
Correct |
6 ms |
10844 KB |
Output is correct |
27 |
Correct |
1 ms |
10588 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10844 KB |
Output is correct |
30 |
Correct |
51 ms |
11228 KB |
Output is correct |
31 |
Correct |
56 ms |
11416 KB |
Output is correct |
32 |
Correct |
74 ms |
11700 KB |
Output is correct |
33 |
Correct |
77 ms |
11604 KB |
Output is correct |
34 |
Correct |
90 ms |
11600 KB |
Output is correct |
35 |
Correct |
6 ms |
11100 KB |
Output is correct |
36 |
Correct |
6 ms |
11124 KB |
Output is correct |
37 |
Correct |
6 ms |
11040 KB |
Output is correct |
38 |
Correct |
88 ms |
11612 KB |
Output is correct |
39 |
Correct |
77 ms |
11724 KB |
Output is correct |
40 |
Correct |
71 ms |
11692 KB |
Output is correct |
41 |
Correct |
6 ms |
11096 KB |
Output is correct |
42 |
Correct |
6 ms |
11096 KB |
Output is correct |
43 |
Correct |
6 ms |
11136 KB |
Output is correct |
44 |
Correct |
78 ms |
11624 KB |
Output is correct |
45 |
Correct |
75 ms |
11580 KB |
Output is correct |
46 |
Correct |
64 ms |
11604 KB |
Output is correct |
47 |
Correct |
6 ms |
11100 KB |
Output is correct |
48 |
Correct |
5 ms |
11100 KB |
Output is correct |
49 |
Correct |
5 ms |
11100 KB |
Output is correct |
50 |
Correct |
64 ms |
11564 KB |
Output is correct |
51 |
Correct |
62 ms |
11600 KB |
Output is correct |
52 |
Correct |
60 ms |
11628 KB |
Output is correct |
53 |
Correct |
63 ms |
11604 KB |
Output is correct |
54 |
Correct |
71 ms |
11604 KB |
Output is correct |
55 |
Correct |
63 ms |
11604 KB |
Output is correct |
56 |
Correct |
4 ms |
10584 KB |
Output is correct |
57 |
Correct |
2 ms |
10844 KB |
Output is correct |
58 |
Correct |
6 ms |
11100 KB |
Output is correct |
59 |
Correct |
1 ms |
10588 KB |
Output is correct |
60 |
Correct |
2 ms |
10588 KB |
Output is correct |
61 |
Correct |
3 ms |
10588 KB |
Output is correct |
62 |
Execution timed out |
5092 ms |
35048 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |