Submission #763636

# Submission time Handle Problem Language Result Execution time Memory
763636 2023-06-22T14:31:27 Z vjudge1 Tourism (JOI23_tourism) C++17
100 / 100
3558 ms 46060 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<int> cur_tree;
pii euler_tour[18][2 * mxN];
int n, m, q, c[mxN], res, ans[mxN], cnt[mxN], cur_tree_sz, fenw[2 * mxN];
int tin[mxN], rev_tin[mxN], tout[mxN], dep[mxN], frst_ap[mxN], euler_cnt, timer;
vector<query> queries;

void dfs(int v, int p) {
    tin[v] = ++timer;
    rev_tin[timer] = v;
    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 update(int idx, int val) {
    while(idx <= 2 * n) {
        fenw[idx] += val;
        idx += (idx & (-idx));
    }
}

int get(int idx) {
    int res = 0;
    while(idx) {
        res += fenw[idx];
        idx -= (idx & (-idx));
    }
    return res;
}

int find(int val) {
    int res = 0, mx = 2 * n + 1;
    for(int i = 18; i >= 0; --i) {
        if(res + (1 << i) >= mx) continue;
        if(val > fenw[res + (1 << i)]) {
            val -= fenw[res + (1 << i)];
            res |= (1 << i);
        }
    }
    return res + 1;
}

void add(int idx) {
    ++cnt[c[idx]];
    if(cnt[c[idx]] > 1) return;
    int val = tin[c[idx]];
    if(cur_tree_sz == 0) {
        update(val, 1); ++cur_tree_sz;
        ll_nxt[c[idx]] = ll_pre[c[idx]] = c[idx];
        return;
    }
    int tom = get(val);
    int nxt = find((tom + 1 > cur_tree_sz ? 1 : tom + 1));
    add_linked_list(c[idx], rev_tin[nxt]);
    update(val, 1); ++cur_tree_sz;
}

void del(int idx) {
    --cnt[c[idx]];
    if(cnt[c[idx]]) return;
    int val = tin[c[idx]];
    update(val, -1);
    --cur_tree_sz;
    if(!cur_tree_sz) 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--);

        ans[idx] = (res >> 1) + 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 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 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 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 1 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 3 ms 2772 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2772 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 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 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 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 1 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 3 ms 2772 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 9 ms 3156 KB Output is correct
31 Correct 10 ms 3212 KB Output is correct
32 Correct 14 ms 3348 KB Output is correct
33 Correct 13 ms 3352 KB Output is correct
34 Correct 13 ms 3344 KB Output is correct
35 Correct 3 ms 3284 KB Output is correct
36 Correct 3 ms 3284 KB Output is correct
37 Correct 3 ms 3284 KB Output is correct
38 Correct 13 ms 3448 KB Output is correct
39 Correct 12 ms 3452 KB Output is correct
40 Correct 12 ms 3412 KB Output is correct
41 Correct 3 ms 3412 KB Output is correct
42 Correct 3 ms 3412 KB Output is correct
43 Correct 3 ms 3412 KB Output is correct
44 Correct 13 ms 3412 KB Output is correct
45 Correct 16 ms 3380 KB Output is correct
46 Correct 13 ms 3408 KB Output is correct
47 Correct 3 ms 3284 KB Output is correct
48 Correct 3 ms 3284 KB Output is correct
49 Correct 3 ms 3412 KB Output is correct
50 Correct 12 ms 3352 KB Output is correct
51 Correct 12 ms 3284 KB Output is correct
52 Correct 11 ms 3360 KB Output is correct
53 Correct 13 ms 3284 KB Output is correct
54 Correct 13 ms 3284 KB Output is correct
55 Correct 12 ms 3360 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 3284 KB Output is correct
58 Correct 3 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1760 ms 26632 KB Output is correct
5 Correct 1362 ms 37084 KB Output is correct
6 Correct 1836 ms 41524 KB Output is correct
7 Correct 2870 ms 45944 KB Output is correct
8 Correct 2832 ms 46020 KB Output is correct
9 Correct 2815 ms 45976 KB Output is correct
10 Correct 2809 ms 46060 KB Output is correct
11 Correct 2856 ms 46016 KB Output is correct
12 Correct 263 ms 45940 KB Output is correct
13 Correct 248 ms 45936 KB Output is correct
14 Correct 245 ms 45932 KB Output is correct
15 Correct 57 ms 43696 KB Output is correct
16 Correct 135 ms 45528 KB Output is correct
17 Correct 92 ms 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 61 ms 20432 KB Output is correct
3 Correct 85 ms 21964 KB Output is correct
4 Correct 77 ms 24668 KB Output is correct
5 Correct 73 ms 36008 KB Output is correct
6 Correct 85 ms 36008 KB Output is correct
7 Correct 89 ms 35960 KB Output is correct
8 Correct 92 ms 35916 KB Output is correct
9 Correct 98 ms 35932 KB Output is correct
10 Correct 98 ms 35972 KB Output is correct
11 Correct 121 ms 35988 KB Output is correct
12 Correct 131 ms 36036 KB Output is correct
13 Correct 139 ms 36272 KB Output is correct
14 Correct 148 ms 36680 KB Output is correct
15 Correct 153 ms 37900 KB Output is correct
16 Correct 129 ms 36376 KB Output is correct
17 Correct 118 ms 36304 KB Output is correct
18 Correct 118 ms 36236 KB Output is correct
19 Correct 65 ms 36012 KB Output is correct
20 Correct 68 ms 36048 KB Output is correct
21 Correct 83 ms 35960 KB Output is correct
22 Correct 83 ms 36052 KB Output is correct
23 Correct 83 ms 35932 KB Output is correct
24 Correct 85 ms 36040 KB Output is correct
25 Correct 88 ms 35940 KB Output is correct
26 Correct 89 ms 36036 KB Output is correct
27 Correct 103 ms 35952 KB Output is correct
28 Correct 114 ms 35944 KB Output is correct
29 Correct 119 ms 35992 KB Output is correct
30 Correct 135 ms 36240 KB Output is correct
31 Correct 144 ms 36496 KB Output is correct
32 Correct 132 ms 36464 KB Output is correct
33 Correct 138 ms 37064 KB Output is correct
34 Correct 147 ms 37892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2107 ms 22960 KB Output is correct
5 Correct 2195 ms 23556 KB Output is correct
6 Correct 3043 ms 32848 KB Output is correct
7 Correct 3522 ms 38120 KB Output is correct
8 Correct 3412 ms 38112 KB Output is correct
9 Correct 3462 ms 38108 KB Output is correct
10 Correct 3436 ms 38112 KB Output is correct
11 Correct 3370 ms 38116 KB Output is correct
12 Correct 3389 ms 38116 KB Output is correct
13 Correct 3440 ms 38124 KB Output is correct
14 Correct 92 ms 4936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 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 2 ms 2772 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 1 ms 2772 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 2 ms 2772 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 2 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 3 ms 2772 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 9 ms 3156 KB Output is correct
31 Correct 10 ms 3212 KB Output is correct
32 Correct 14 ms 3348 KB Output is correct
33 Correct 13 ms 3352 KB Output is correct
34 Correct 13 ms 3344 KB Output is correct
35 Correct 3 ms 3284 KB Output is correct
36 Correct 3 ms 3284 KB Output is correct
37 Correct 3 ms 3284 KB Output is correct
38 Correct 13 ms 3448 KB Output is correct
39 Correct 12 ms 3452 KB Output is correct
40 Correct 12 ms 3412 KB Output is correct
41 Correct 3 ms 3412 KB Output is correct
42 Correct 3 ms 3412 KB Output is correct
43 Correct 3 ms 3412 KB Output is correct
44 Correct 13 ms 3412 KB Output is correct
45 Correct 16 ms 3380 KB Output is correct
46 Correct 13 ms 3408 KB Output is correct
47 Correct 3 ms 3284 KB Output is correct
48 Correct 3 ms 3284 KB Output is correct
49 Correct 3 ms 3412 KB Output is correct
50 Correct 12 ms 3352 KB Output is correct
51 Correct 12 ms 3284 KB Output is correct
52 Correct 11 ms 3360 KB Output is correct
53 Correct 13 ms 3284 KB Output is correct
54 Correct 13 ms 3284 KB Output is correct
55 Correct 12 ms 3360 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 3284 KB Output is correct
58 Correct 3 ms 3284 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2772 KB Output is correct
62 Correct 1760 ms 26632 KB Output is correct
63 Correct 1362 ms 37084 KB Output is correct
64 Correct 1836 ms 41524 KB Output is correct
65 Correct 2870 ms 45944 KB Output is correct
66 Correct 2832 ms 46020 KB Output is correct
67 Correct 2815 ms 45976 KB Output is correct
68 Correct 2809 ms 46060 KB Output is correct
69 Correct 2856 ms 46016 KB Output is correct
70 Correct 263 ms 45940 KB Output is correct
71 Correct 248 ms 45936 KB Output is correct
72 Correct 245 ms 45932 KB Output is correct
73 Correct 57 ms 43696 KB Output is correct
74 Correct 135 ms 45528 KB Output is correct
75 Correct 92 ms 4944 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 61 ms 20432 KB Output is correct
78 Correct 85 ms 21964 KB Output is correct
79 Correct 77 ms 24668 KB Output is correct
80 Correct 73 ms 36008 KB Output is correct
81 Correct 85 ms 36008 KB Output is correct
82 Correct 89 ms 35960 KB Output is correct
83 Correct 92 ms 35916 KB Output is correct
84 Correct 98 ms 35932 KB Output is correct
85 Correct 98 ms 35972 KB Output is correct
86 Correct 121 ms 35988 KB Output is correct
87 Correct 131 ms 36036 KB Output is correct
88 Correct 139 ms 36272 KB Output is correct
89 Correct 148 ms 36680 KB Output is correct
90 Correct 153 ms 37900 KB Output is correct
91 Correct 129 ms 36376 KB Output is correct
92 Correct 118 ms 36304 KB Output is correct
93 Correct 118 ms 36236 KB Output is correct
94 Correct 65 ms 36012 KB Output is correct
95 Correct 68 ms 36048 KB Output is correct
96 Correct 83 ms 35960 KB Output is correct
97 Correct 83 ms 36052 KB Output is correct
98 Correct 83 ms 35932 KB Output is correct
99 Correct 85 ms 36040 KB Output is correct
100 Correct 88 ms 35940 KB Output is correct
101 Correct 89 ms 36036 KB Output is correct
102 Correct 103 ms 35952 KB Output is correct
103 Correct 114 ms 35944 KB Output is correct
104 Correct 119 ms 35992 KB Output is correct
105 Correct 135 ms 36240 KB Output is correct
106 Correct 144 ms 36496 KB Output is correct
107 Correct 132 ms 36464 KB Output is correct
108 Correct 138 ms 37064 KB Output is correct
109 Correct 147 ms 37892 KB Output is correct
110 Correct 1 ms 2644 KB Output is correct
111 Correct 2 ms 2644 KB Output is correct
112 Correct 2 ms 2772 KB Output is correct
113 Correct 2107 ms 22960 KB Output is correct
114 Correct 2195 ms 23556 KB Output is correct
115 Correct 3043 ms 32848 KB Output is correct
116 Correct 3522 ms 38120 KB Output is correct
117 Correct 3412 ms 38112 KB Output is correct
118 Correct 3462 ms 38108 KB Output is correct
119 Correct 3436 ms 38112 KB Output is correct
120 Correct 3370 ms 38116 KB Output is correct
121 Correct 3389 ms 38116 KB Output is correct
122 Correct 3440 ms 38124 KB Output is correct
123 Correct 92 ms 4936 KB Output is correct
124 Correct 2937 ms 38104 KB Output is correct
125 Correct 1803 ms 36236 KB Output is correct
126 Correct 3436 ms 38308 KB Output is correct
127 Correct 3558 ms 38228 KB Output is correct
128 Correct 3531 ms 38328 KB Output is correct
129 Correct 3392 ms 38224 KB Output is correct
130 Correct 3525 ms 38224 KB Output is correct
131 Correct 3050 ms 44760 KB Output is correct
132 Correct 2960 ms 45912 KB Output is correct
133 Correct 2977 ms 42036 KB Output is correct
134 Correct 3232 ms 38312 KB Output is correct
135 Correct 3271 ms 38272 KB Output is correct
136 Correct 3452 ms 38264 KB Output is correct
137 Correct 3230 ms 38768 KB Output is correct
138 Correct 3198 ms 38764 KB Output is correct
139 Correct 3187 ms 38764 KB Output is correct
140 Correct 3223 ms 38764 KB Output is correct
141 Correct 3167 ms 38768 KB Output is correct
142 Correct 3239 ms 38712 KB Output is correct
143 Correct 60 ms 35896 KB Output is correct
144 Correct 160 ms 37840 KB Output is correct