# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
763576 |
2023-06-22T13:11:18 Z |
CDuong |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
50224 KB |
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
using namespace std;
const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const int block = 350;
const ll oo = 1e18;
struct query {
int l, r, idx;
query() {};
query(int l, int r, int idx) : l(l), r(r), idx(idx) {}
bool operator < (const query &o) const {
int cur_blk = l / block;
if(cur_blk != o.l / block) return l < o.l;
return (cur_blk & 1 ? r > o.r : r < o.r);
}
};
vi G[mxN];
set<pii> cur_tree;
pii euler_tour[18][2 * mxN];
int n, m, q, c[mxN], res, ans[mxN], cnt[mxN];
int tin[mxN], tout[mxN], dep[mxN], frst_ap[mxN], euler_cnt, timer;
vector<query> queries;
void dfs(int v, int p) {
tin[v] = ++timer;
dep[v] = dep[p] + 1;
frst_ap[v] = ++euler_cnt;
euler_tour[0][euler_cnt] = {dep[v], v};
for(int u : G[v]) {
if(u == p) continue;
dfs(u, v);
euler_tour[0][++euler_cnt] = {dep[v], v};
}
tout[v] = timer;
}
void build_sparse() {
for(int i = 1; (1 << i) <= euler_cnt; ++i) {
for(int j = 1; j + (1 << i) - 1 <= euler_cnt; ++j) {
euler_tour[i][j] = min(euler_tour[i - 1][j], euler_tour[i - 1][j + (1 << (i - 1))]);
}
}
}
int lca(int u, int v) {
int l = frst_ap[u], r = frst_ap[v];
if(l > r) swap(l, r);
int len = r - l + 1;
int lg_len = 31 - __builtin_clz(len);
return min(euler_tour[lg_len][l], euler_tour[lg_len][r - (1 << lg_len) + 1]).ss;
}
int dis(int u, int v) {
int acl = lca(u, v);
return (dep[u] + dep[v] - 2 * dep[acl]);
}
int ll_nxt[mxN], ll_pre[mxN];
void add_linked_list(int idx, int nxt) {
int pre = ll_pre[nxt];
res += dis(pre, idx);
res += dis(nxt, idx);
res -= dis(pre, nxt);
ll_nxt[pre] = ll_pre[nxt] = idx;
ll_nxt[idx] = nxt, ll_pre[idx] = pre;
}
void del_linked_list(int idx) {
int pre = ll_pre[idx], nxt = ll_nxt[idx];
res -= dis(pre, idx);
res -= dis(nxt, idx);
res += dis(pre, nxt);
ll_nxt[pre] = nxt, ll_pre[nxt] = pre;
}
void add(int idx) {
++cnt[c[idx]];
if(cnt[c[idx]] > 1) return;
int val = tin[c[idx]];
if(cur_tree.empty()) {
cur_tree.insert({val, c[idx]});
ll_nxt[c[idx]] = ll_pre[c[idx]] = c[idx];
return;
}
auto it = cur_tree.lower_bound({val, 0});
int nxt = (it == cur_tree.end() ? (*cur_tree.begin()).ss : (*it).ss);
add_linked_list(c[idx], nxt);
cur_tree.insert({val, c[idx]});
}
void del(int idx) {
--cnt[c[idx]];
if(cnt[c[idx]]) return;
int val = tin[c[idx]];
cur_tree.erase({val, c[idx]});
if(cur_tree.empty()) return;
del_linked_list(c[idx]);
}
void solve() {
cin >> n >> m >> q;
for(int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
dfs(1, 0);
build_sparse();
for(int i = 1; i <= m; ++i) {
cin >> c[i];
}
for(int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
queries.emplace_back(l, r, i);
}
sort(all(queries));
int cur_l = 1, cur_r = 0;
for(auto &[l, r, idx] : queries) {
while(cur_l > l) add(--cur_l);
while(cur_r < r) add(++cur_r);
while(cur_l < l) del(cur_l++);
while(cur_r > r) del(cur_r--);
// cout << res << endl;
// for(auto tmp : cur_tree) {
// cout << tmp.ff << " " << tmp.ss << endl;
// }
// cout << endl;
ans[idx] = res / 2 + 1;
}
for(int i = 1; i <= q; ++i) {
cout << ans[i] << "\n";
}
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2812 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
4 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2772 KB |
Output is correct |
11 |
Correct |
4 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2820 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2820 KB |
Output is correct |
15 |
Correct |
4 ms |
2824 KB |
Output is correct |
16 |
Correct |
4 ms |
2820 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
4 ms |
2828 KB |
Output is correct |
20 |
Correct |
4 ms |
2876 KB |
Output is correct |
21 |
Correct |
4 ms |
2824 KB |
Output is correct |
22 |
Correct |
4 ms |
2824 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
5 ms |
2772 KB |
Output is correct |
26 |
Correct |
3 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2820 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2812 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
4 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2772 KB |
Output is correct |
11 |
Correct |
4 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2820 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2820 KB |
Output is correct |
15 |
Correct |
4 ms |
2824 KB |
Output is correct |
16 |
Correct |
4 ms |
2820 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
4 ms |
2828 KB |
Output is correct |
20 |
Correct |
4 ms |
2876 KB |
Output is correct |
21 |
Correct |
4 ms |
2824 KB |
Output is correct |
22 |
Correct |
4 ms |
2824 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
5 ms |
2772 KB |
Output is correct |
26 |
Correct |
3 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2820 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
21 ms |
3156 KB |
Output is correct |
31 |
Correct |
28 ms |
3224 KB |
Output is correct |
32 |
Correct |
25 ms |
3384 KB |
Output is correct |
33 |
Correct |
25 ms |
3396 KB |
Output is correct |
34 |
Correct |
25 ms |
3380 KB |
Output is correct |
35 |
Correct |
4 ms |
3340 KB |
Output is correct |
36 |
Correct |
6 ms |
3412 KB |
Output is correct |
37 |
Correct |
4 ms |
3412 KB |
Output is correct |
38 |
Correct |
26 ms |
3500 KB |
Output is correct |
39 |
Correct |
25 ms |
3500 KB |
Output is correct |
40 |
Correct |
26 ms |
3472 KB |
Output is correct |
41 |
Correct |
4 ms |
3540 KB |
Output is correct |
42 |
Correct |
5 ms |
3540 KB |
Output is correct |
43 |
Correct |
4 ms |
3412 KB |
Output is correct |
44 |
Correct |
33 ms |
3452 KB |
Output is correct |
45 |
Correct |
34 ms |
3416 KB |
Output is correct |
46 |
Correct |
33 ms |
3456 KB |
Output is correct |
47 |
Correct |
4 ms |
3440 KB |
Output is correct |
48 |
Correct |
5 ms |
3344 KB |
Output is correct |
49 |
Correct |
4 ms |
3348 KB |
Output is correct |
50 |
Correct |
24 ms |
3412 KB |
Output is correct |
51 |
Correct |
23 ms |
3284 KB |
Output is correct |
52 |
Correct |
22 ms |
3284 KB |
Output is correct |
53 |
Correct |
28 ms |
3412 KB |
Output is correct |
54 |
Correct |
32 ms |
3392 KB |
Output is correct |
55 |
Correct |
24 ms |
3408 KB |
Output is correct |
56 |
Correct |
3 ms |
2772 KB |
Output is correct |
57 |
Correct |
3 ms |
3284 KB |
Output is correct |
58 |
Correct |
4 ms |
3296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
3711 ms |
30068 KB |
Output is correct |
5 |
Correct |
2796 ms |
40116 KB |
Output is correct |
6 |
Correct |
4073 ms |
45360 KB |
Output is correct |
7 |
Execution timed out |
5066 ms |
50224 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
81 ms |
20820 KB |
Output is correct |
3 |
Correct |
109 ms |
22692 KB |
Output is correct |
4 |
Correct |
97 ms |
25320 KB |
Output is correct |
5 |
Correct |
100 ms |
39816 KB |
Output is correct |
6 |
Correct |
108 ms |
39220 KB |
Output is correct |
7 |
Correct |
125 ms |
37676 KB |
Output is correct |
8 |
Correct |
118 ms |
37196 KB |
Output is correct |
9 |
Correct |
126 ms |
37036 KB |
Output is correct |
10 |
Correct |
102 ms |
36892 KB |
Output is correct |
11 |
Correct |
149 ms |
36980 KB |
Output is correct |
12 |
Correct |
171 ms |
36972 KB |
Output is correct |
13 |
Correct |
184 ms |
37324 KB |
Output is correct |
14 |
Correct |
199 ms |
37944 KB |
Output is correct |
15 |
Correct |
169 ms |
40000 KB |
Output is correct |
16 |
Correct |
128 ms |
38884 KB |
Output is correct |
17 |
Correct |
180 ms |
38904 KB |
Output is correct |
18 |
Correct |
145 ms |
38936 KB |
Output is correct |
19 |
Correct |
89 ms |
39856 KB |
Output is correct |
20 |
Correct |
88 ms |
39820 KB |
Output is correct |
21 |
Correct |
103 ms |
38484 KB |
Output is correct |
22 |
Correct |
108 ms |
37800 KB |
Output is correct |
23 |
Correct |
112 ms |
37356 KB |
Output is correct |
24 |
Correct |
110 ms |
37152 KB |
Output is correct |
25 |
Correct |
101 ms |
37048 KB |
Output is correct |
26 |
Correct |
107 ms |
36992 KB |
Output is correct |
27 |
Correct |
110 ms |
36928 KB |
Output is correct |
28 |
Correct |
131 ms |
36984 KB |
Output is correct |
29 |
Correct |
143 ms |
37044 KB |
Output is correct |
30 |
Correct |
161 ms |
37228 KB |
Output is correct |
31 |
Correct |
161 ms |
37408 KB |
Output is correct |
32 |
Correct |
151 ms |
37624 KB |
Output is correct |
33 |
Correct |
144 ms |
38496 KB |
Output is correct |
34 |
Correct |
157 ms |
39972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Correct |
4808 ms |
26656 KB |
Output is correct |
5 |
Correct |
4881 ms |
27328 KB |
Output is correct |
6 |
Execution timed out |
5098 ms |
36796 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2812 KB |
Output is correct |
8 |
Correct |
3 ms |
2816 KB |
Output is correct |
9 |
Correct |
4 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2772 KB |
Output is correct |
11 |
Correct |
4 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2820 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2820 KB |
Output is correct |
15 |
Correct |
4 ms |
2824 KB |
Output is correct |
16 |
Correct |
4 ms |
2820 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
4 ms |
2828 KB |
Output is correct |
20 |
Correct |
4 ms |
2876 KB |
Output is correct |
21 |
Correct |
4 ms |
2824 KB |
Output is correct |
22 |
Correct |
4 ms |
2824 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
5 ms |
2772 KB |
Output is correct |
26 |
Correct |
3 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2820 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
21 ms |
3156 KB |
Output is correct |
31 |
Correct |
28 ms |
3224 KB |
Output is correct |
32 |
Correct |
25 ms |
3384 KB |
Output is correct |
33 |
Correct |
25 ms |
3396 KB |
Output is correct |
34 |
Correct |
25 ms |
3380 KB |
Output is correct |
35 |
Correct |
4 ms |
3340 KB |
Output is correct |
36 |
Correct |
6 ms |
3412 KB |
Output is correct |
37 |
Correct |
4 ms |
3412 KB |
Output is correct |
38 |
Correct |
26 ms |
3500 KB |
Output is correct |
39 |
Correct |
25 ms |
3500 KB |
Output is correct |
40 |
Correct |
26 ms |
3472 KB |
Output is correct |
41 |
Correct |
4 ms |
3540 KB |
Output is correct |
42 |
Correct |
5 ms |
3540 KB |
Output is correct |
43 |
Correct |
4 ms |
3412 KB |
Output is correct |
44 |
Correct |
33 ms |
3452 KB |
Output is correct |
45 |
Correct |
34 ms |
3416 KB |
Output is correct |
46 |
Correct |
33 ms |
3456 KB |
Output is correct |
47 |
Correct |
4 ms |
3440 KB |
Output is correct |
48 |
Correct |
5 ms |
3344 KB |
Output is correct |
49 |
Correct |
4 ms |
3348 KB |
Output is correct |
50 |
Correct |
24 ms |
3412 KB |
Output is correct |
51 |
Correct |
23 ms |
3284 KB |
Output is correct |
52 |
Correct |
22 ms |
3284 KB |
Output is correct |
53 |
Correct |
28 ms |
3412 KB |
Output is correct |
54 |
Correct |
32 ms |
3392 KB |
Output is correct |
55 |
Correct |
24 ms |
3408 KB |
Output is correct |
56 |
Correct |
3 ms |
2772 KB |
Output is correct |
57 |
Correct |
3 ms |
3284 KB |
Output is correct |
58 |
Correct |
4 ms |
3296 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
3 ms |
2772 KB |
Output is correct |
62 |
Correct |
3711 ms |
30068 KB |
Output is correct |
63 |
Correct |
2796 ms |
40116 KB |
Output is correct |
64 |
Correct |
4073 ms |
45360 KB |
Output is correct |
65 |
Execution timed out |
5066 ms |
50224 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |