Submission #763630

# Submission time Handle Problem Language Result Execution time Memory
763630 2023-06-22T14:27:15 Z CDuong Tourism (JOI23_tourism) C++17
100 / 100
3786 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 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2772 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 3 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 3 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2860 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 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 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2772 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 3 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 3 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2860 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 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 2 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 11 ms 3156 KB Output is correct
32 Correct 13 ms 3348 KB Output is correct
33 Correct 14 ms 3284 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 3352 KB Output is correct
38 Correct 13 ms 3448 KB Output is correct
39 Correct 12 ms 3456 KB Output is correct
40 Correct 15 ms 3468 KB Output is correct
41 Correct 3 ms 3412 KB Output is correct
42 Correct 3 ms 3412 KB Output is correct
43 Correct 7 ms 3340 KB Output is correct
44 Correct 13 ms 3400 KB Output is correct
45 Correct 13 ms 3284 KB Output is correct
46 Correct 13 ms 3412 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 3284 KB Output is correct
51 Correct 11 ms 3284 KB Output is correct
52 Correct 11 ms 3284 KB Output is correct
53 Correct 12 ms 3284 KB Output is correct
54 Correct 12 ms 3284 KB Output is correct
55 Correct 12 ms 3284 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 3284 KB Output is correct
58 Correct 4 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 3 ms 2772 KB Output is correct
4 Correct 1777 ms 26576 KB Output is correct
5 Correct 1310 ms 37000 KB Output is correct
6 Correct 1868 ms 41528 KB Output is correct
7 Correct 2916 ms 45944 KB Output is correct
8 Correct 2995 ms 45936 KB Output is correct
9 Correct 2965 ms 45940 KB Output is correct
10 Correct 3054 ms 46060 KB Output is correct
11 Correct 3309 ms 45952 KB Output is correct
12 Correct 252 ms 45932 KB Output is correct
13 Correct 269 ms 45936 KB Output is correct
14 Correct 263 ms 45940 KB Output is correct
15 Correct 55 ms 43596 KB Output is correct
16 Correct 162 ms 45548 KB Output is correct
17 Correct 93 ms 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 72 ms 20244 KB Output is correct
3 Correct 91 ms 21876 KB Output is correct
4 Correct 91 ms 24624 KB Output is correct
5 Correct 72 ms 36008 KB Output is correct
6 Correct 81 ms 35912 KB Output is correct
7 Correct 117 ms 35924 KB Output is correct
8 Correct 111 ms 36132 KB Output is correct
9 Correct 121 ms 35956 KB Output is correct
10 Correct 102 ms 36024 KB Output is correct
11 Correct 127 ms 36044 KB Output is correct
12 Correct 137 ms 36116 KB Output is correct
13 Correct 135 ms 36300 KB Output is correct
14 Correct 142 ms 36636 KB Output is correct
15 Correct 160 ms 37788 KB Output is correct
16 Correct 118 ms 36312 KB Output is correct
17 Correct 129 ms 36352 KB Output is correct
18 Correct 119 ms 36272 KB Output is correct
19 Correct 66 ms 35952 KB Output is correct
20 Correct 71 ms 36044 KB Output is correct
21 Correct 83 ms 36004 KB Output is correct
22 Correct 82 ms 36052 KB Output is correct
23 Correct 83 ms 35920 KB Output is correct
24 Correct 88 ms 35972 KB Output is correct
25 Correct 91 ms 35928 KB Output is correct
26 Correct 104 ms 36044 KB Output is correct
27 Correct 113 ms 36072 KB Output is correct
28 Correct 115 ms 36004 KB Output is correct
29 Correct 120 ms 36096 KB Output is correct
30 Correct 134 ms 36172 KB Output is correct
31 Correct 162 ms 36324 KB Output is correct
32 Correct 131 ms 36508 KB Output is correct
33 Correct 138 ms 37032 KB Output is correct
34 Correct 145 ms 37948 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 2772 KB Output is correct
4 Correct 2176 ms 22908 KB Output is correct
5 Correct 2171 ms 23548 KB Output is correct
6 Correct 3088 ms 32916 KB Output is correct
7 Correct 3539 ms 38124 KB Output is correct
8 Correct 3496 ms 38180 KB Output is correct
9 Correct 3723 ms 38108 KB Output is correct
10 Correct 3499 ms 38116 KB Output is correct
11 Correct 3587 ms 38108 KB Output is correct
12 Correct 3786 ms 38108 KB Output is correct
13 Correct 3608 ms 38116 KB Output is correct
14 Correct 92 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2772 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 3 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 3 ms 2772 KB Output is correct
20 Correct 2 ms 2772 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2860 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 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 2 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 11 ms 3156 KB Output is correct
32 Correct 13 ms 3348 KB Output is correct
33 Correct 14 ms 3284 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 3352 KB Output is correct
38 Correct 13 ms 3448 KB Output is correct
39 Correct 12 ms 3456 KB Output is correct
40 Correct 15 ms 3468 KB Output is correct
41 Correct 3 ms 3412 KB Output is correct
42 Correct 3 ms 3412 KB Output is correct
43 Correct 7 ms 3340 KB Output is correct
44 Correct 13 ms 3400 KB Output is correct
45 Correct 13 ms 3284 KB Output is correct
46 Correct 13 ms 3412 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 3284 KB Output is correct
51 Correct 11 ms 3284 KB Output is correct
52 Correct 11 ms 3284 KB Output is correct
53 Correct 12 ms 3284 KB Output is correct
54 Correct 12 ms 3284 KB Output is correct
55 Correct 12 ms 3284 KB Output is correct
56 Correct 2 ms 2772 KB Output is correct
57 Correct 2 ms 3284 KB Output is correct
58 Correct 4 ms 3284 KB Output is correct
59 Correct 1 ms 2644 KB Output is correct
60 Correct 1 ms 2644 KB Output is correct
61 Correct 3 ms 2772 KB Output is correct
62 Correct 1777 ms 26576 KB Output is correct
63 Correct 1310 ms 37000 KB Output is correct
64 Correct 1868 ms 41528 KB Output is correct
65 Correct 2916 ms 45944 KB Output is correct
66 Correct 2995 ms 45936 KB Output is correct
67 Correct 2965 ms 45940 KB Output is correct
68 Correct 3054 ms 46060 KB Output is correct
69 Correct 3309 ms 45952 KB Output is correct
70 Correct 252 ms 45932 KB Output is correct
71 Correct 269 ms 45936 KB Output is correct
72 Correct 263 ms 45940 KB Output is correct
73 Correct 55 ms 43596 KB Output is correct
74 Correct 162 ms 45548 KB Output is correct
75 Correct 93 ms 4944 KB Output is correct
76 Correct 1 ms 2644 KB Output is correct
77 Correct 72 ms 20244 KB Output is correct
78 Correct 91 ms 21876 KB Output is correct
79 Correct 91 ms 24624 KB Output is correct
80 Correct 72 ms 36008 KB Output is correct
81 Correct 81 ms 35912 KB Output is correct
82 Correct 117 ms 35924 KB Output is correct
83 Correct 111 ms 36132 KB Output is correct
84 Correct 121 ms 35956 KB Output is correct
85 Correct 102 ms 36024 KB Output is correct
86 Correct 127 ms 36044 KB Output is correct
87 Correct 137 ms 36116 KB Output is correct
88 Correct 135 ms 36300 KB Output is correct
89 Correct 142 ms 36636 KB Output is correct
90 Correct 160 ms 37788 KB Output is correct
91 Correct 118 ms 36312 KB Output is correct
92 Correct 129 ms 36352 KB Output is correct
93 Correct 119 ms 36272 KB Output is correct
94 Correct 66 ms 35952 KB Output is correct
95 Correct 71 ms 36044 KB Output is correct
96 Correct 83 ms 36004 KB Output is correct
97 Correct 82 ms 36052 KB Output is correct
98 Correct 83 ms 35920 KB Output is correct
99 Correct 88 ms 35972 KB Output is correct
100 Correct 91 ms 35928 KB Output is correct
101 Correct 104 ms 36044 KB Output is correct
102 Correct 113 ms 36072 KB Output is correct
103 Correct 115 ms 36004 KB Output is correct
104 Correct 120 ms 36096 KB Output is correct
105 Correct 134 ms 36172 KB Output is correct
106 Correct 162 ms 36324 KB Output is correct
107 Correct 131 ms 36508 KB Output is correct
108 Correct 138 ms 37032 KB Output is correct
109 Correct 145 ms 37948 KB Output is correct
110 Correct 2 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 2176 ms 22908 KB Output is correct
114 Correct 2171 ms 23548 KB Output is correct
115 Correct 3088 ms 32916 KB Output is correct
116 Correct 3539 ms 38124 KB Output is correct
117 Correct 3496 ms 38180 KB Output is correct
118 Correct 3723 ms 38108 KB Output is correct
119 Correct 3499 ms 38116 KB Output is correct
120 Correct 3587 ms 38108 KB Output is correct
121 Correct 3786 ms 38108 KB Output is correct
122 Correct 3608 ms 38116 KB Output is correct
123 Correct 92 ms 4940 KB Output is correct
124 Correct 3012 ms 38092 KB Output is correct
125 Correct 1993 ms 36228 KB Output is correct
126 Correct 3655 ms 38224 KB Output is correct
127 Correct 3734 ms 38220 KB Output is correct
128 Correct 3550 ms 38220 KB Output is correct
129 Correct 3529 ms 38224 KB Output is correct
130 Correct 3491 ms 38216 KB Output is correct
131 Correct 3128 ms 44760 KB Output is correct
132 Correct 3120 ms 45828 KB Output is correct
133 Correct 3252 ms 42044 KB Output is correct
134 Correct 3530 ms 38316 KB Output is correct
135 Correct 3451 ms 38276 KB Output is correct
136 Correct 3383 ms 38264 KB Output is correct
137 Correct 3222 ms 38768 KB Output is correct
138 Correct 3227 ms 38772 KB Output is correct
139 Correct 3445 ms 38740 KB Output is correct
140 Correct 3479 ms 38808 KB Output is correct
141 Correct 3341 ms 38848 KB Output is correct
142 Correct 3294 ms 38760 KB Output is correct
143 Correct 61 ms 35908 KB Output is correct
144 Correct 174 ms 37840 KB Output is correct