답안 #763626

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
763626 2023-06-22T14:24:26 Z CDuong Tourism (JOI23_tourism) C++17
100 / 100
3508 ms 46068 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

}
# 결과 실행 시간 메모리 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 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 2860 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 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 3 ms 2900 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 3 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 2856 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2872 KB Output is correct
24 Correct 3 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 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 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 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 2860 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 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 3 ms 2900 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 3 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 2856 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2872 KB Output is correct
24 Correct 3 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 2 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 3216 KB Output is correct
31 Correct 11 ms 3212 KB Output is correct
32 Correct 13 ms 3276 KB Output is correct
33 Correct 13 ms 3412 KB Output is correct
34 Correct 12 ms 3352 KB Output is correct
35 Correct 5 ms 3284 KB Output is correct
36 Correct 4 ms 3284 KB Output is correct
37 Correct 3 ms 3284 KB Output is correct
38 Correct 12 ms 3448 KB Output is correct
39 Correct 12 ms 3456 KB Output is correct
40 Correct 12 ms 3468 KB Output is correct
41 Correct 4 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 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 15 ms 3360 KB Output is correct
52 Correct 11 ms 3356 KB Output is correct
53 Correct 11 ms 3284 KB Output is correct
54 Correct 12 ms 3356 KB Output is correct
55 Correct 12 ms 3356 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 3352 KB Output is correct
# 결과 실행 시간 메모리 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 1631 ms 26588 KB Output is correct
5 Correct 1207 ms 36984 KB Output is correct
6 Correct 1819 ms 41528 KB Output is correct
7 Correct 2758 ms 45940 KB Output is correct
8 Correct 2692 ms 45940 KB Output is correct
9 Correct 2794 ms 45932 KB Output is correct
10 Correct 2742 ms 46068 KB Output is correct
11 Correct 2825 ms 45936 KB Output is correct
12 Correct 261 ms 45944 KB Output is correct
13 Correct 266 ms 45940 KB Output is correct
14 Correct 275 ms 45944 KB Output is correct
15 Correct 62 ms 43652 KB Output is correct
16 Correct 174 ms 45504 KB Output is correct
17 Correct 94 ms 4996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 62 ms 20264 KB Output is correct
3 Correct 87 ms 21972 KB Output is correct
4 Correct 87 ms 24728 KB Output is correct
5 Correct 73 ms 36004 KB Output is correct
6 Correct 81 ms 35912 KB Output is correct
7 Correct 86 ms 36004 KB Output is correct
8 Correct 96 ms 36004 KB Output is correct
9 Correct 100 ms 35960 KB Output is correct
10 Correct 106 ms 35948 KB Output is correct
11 Correct 123 ms 35952 KB Output is correct
12 Correct 128 ms 36044 KB Output is correct
13 Correct 137 ms 36276 KB Output is correct
14 Correct 157 ms 36592 KB Output is correct
15 Correct 154 ms 37900 KB Output is correct
16 Correct 119 ms 36264 KB Output is correct
17 Correct 128 ms 36272 KB Output is correct
18 Correct 130 ms 36240 KB Output is correct
19 Correct 81 ms 36064 KB Output is correct
20 Correct 72 ms 35936 KB Output is correct
21 Correct 85 ms 35916 KB Output is correct
22 Correct 82 ms 35944 KB Output is correct
23 Correct 82 ms 36052 KB Output is correct
24 Correct 98 ms 36076 KB Output is correct
25 Correct 86 ms 35976 KB Output is correct
26 Correct 99 ms 35988 KB Output is correct
27 Correct 105 ms 35984 KB Output is correct
28 Correct 114 ms 35916 KB Output is correct
29 Correct 128 ms 36172 KB Output is correct
30 Correct 127 ms 36220 KB Output is correct
31 Correct 131 ms 36312 KB Output is correct
32 Correct 132 ms 36468 KB Output is correct
33 Correct 139 ms 37040 KB Output is correct
34 Correct 145 ms 38004 KB Output is correct
# 결과 실행 시간 메모리 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 2118 ms 22908 KB Output is correct
5 Correct 2110 ms 23552 KB Output is correct
6 Correct 3043 ms 32844 KB Output is correct
7 Correct 3508 ms 38084 KB Output is correct
8 Correct 3466 ms 38112 KB Output is correct
9 Correct 3442 ms 38120 KB Output is correct
10 Correct 3317 ms 38116 KB Output is correct
11 Correct 3219 ms 38112 KB Output is correct
12 Correct 3186 ms 38116 KB Output is correct
13 Correct 3483 ms 38108 KB Output is correct
14 Correct 96 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 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 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 2860 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 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 3 ms 2900 KB Output is correct
16 Correct 3 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 3 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 2856 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2872 KB Output is correct
24 Correct 3 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 2 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 3216 KB Output is correct
31 Correct 11 ms 3212 KB Output is correct
32 Correct 13 ms 3276 KB Output is correct
33 Correct 13 ms 3412 KB Output is correct
34 Correct 12 ms 3352 KB Output is correct
35 Correct 5 ms 3284 KB Output is correct
36 Correct 4 ms 3284 KB Output is correct
37 Correct 3 ms 3284 KB Output is correct
38 Correct 12 ms 3448 KB Output is correct
39 Correct 12 ms 3456 KB Output is correct
40 Correct 12 ms 3468 KB Output is correct
41 Correct 4 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 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 15 ms 3360 KB Output is correct
52 Correct 11 ms 3356 KB Output is correct
53 Correct 11 ms 3284 KB Output is correct
54 Correct 12 ms 3356 KB Output is correct
55 Correct 12 ms 3356 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 3352 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 1631 ms 26588 KB Output is correct
63 Correct 1207 ms 36984 KB Output is correct
64 Correct 1819 ms 41528 KB Output is correct
65 Correct 2758 ms 45940 KB Output is correct
66 Correct 2692 ms 45940 KB Output is correct
67 Correct 2794 ms 45932 KB Output is correct
68 Correct 2742 ms 46068 KB Output is correct
69 Correct 2825 ms 45936 KB Output is correct
70 Correct 261 ms 45944 KB Output is correct
71 Correct 266 ms 45940 KB Output is correct
72 Correct 275 ms 45944 KB Output is correct
73 Correct 62 ms 43652 KB Output is correct
74 Correct 174 ms 45504 KB Output is correct
75 Correct 94 ms 4996 KB Output is correct
76 Correct 1 ms 2644 KB Output is correct
77 Correct 62 ms 20264 KB Output is correct
78 Correct 87 ms 21972 KB Output is correct
79 Correct 87 ms 24728 KB Output is correct
80 Correct 73 ms 36004 KB Output is correct
81 Correct 81 ms 35912 KB Output is correct
82 Correct 86 ms 36004 KB Output is correct
83 Correct 96 ms 36004 KB Output is correct
84 Correct 100 ms 35960 KB Output is correct
85 Correct 106 ms 35948 KB Output is correct
86 Correct 123 ms 35952 KB Output is correct
87 Correct 128 ms 36044 KB Output is correct
88 Correct 137 ms 36276 KB Output is correct
89 Correct 157 ms 36592 KB Output is correct
90 Correct 154 ms 37900 KB Output is correct
91 Correct 119 ms 36264 KB Output is correct
92 Correct 128 ms 36272 KB Output is correct
93 Correct 130 ms 36240 KB Output is correct
94 Correct 81 ms 36064 KB Output is correct
95 Correct 72 ms 35936 KB Output is correct
96 Correct 85 ms 35916 KB Output is correct
97 Correct 82 ms 35944 KB Output is correct
98 Correct 82 ms 36052 KB Output is correct
99 Correct 98 ms 36076 KB Output is correct
100 Correct 86 ms 35976 KB Output is correct
101 Correct 99 ms 35988 KB Output is correct
102 Correct 105 ms 35984 KB Output is correct
103 Correct 114 ms 35916 KB Output is correct
104 Correct 128 ms 36172 KB Output is correct
105 Correct 127 ms 36220 KB Output is correct
106 Correct 131 ms 36312 KB Output is correct
107 Correct 132 ms 36468 KB Output is correct
108 Correct 139 ms 37040 KB Output is correct
109 Correct 145 ms 38004 KB Output is correct
110 Correct 2 ms 2644 KB Output is correct
111 Correct 2 ms 2644 KB Output is correct
112 Correct 3 ms 2772 KB Output is correct
113 Correct 2118 ms 22908 KB Output is correct
114 Correct 2110 ms 23552 KB Output is correct
115 Correct 3043 ms 32844 KB Output is correct
116 Correct 3508 ms 38084 KB Output is correct
117 Correct 3466 ms 38112 KB Output is correct
118 Correct 3442 ms 38120 KB Output is correct
119 Correct 3317 ms 38116 KB Output is correct
120 Correct 3219 ms 38112 KB Output is correct
121 Correct 3186 ms 38116 KB Output is correct
122 Correct 3483 ms 38108 KB Output is correct
123 Correct 96 ms 4940 KB Output is correct
124 Correct 2778 ms 38100 KB Output is correct
125 Correct 1645 ms 36344 KB Output is correct
126 Correct 3309 ms 38224 KB Output is correct
127 Correct 3268 ms 38220 KB Output is correct
128 Correct 3268 ms 38220 KB Output is correct
129 Correct 3243 ms 38284 KB Output is correct
130 Correct 3237 ms 38216 KB Output is correct
131 Correct 2841 ms 44756 KB Output is correct
132 Correct 3038 ms 45828 KB Output is correct
133 Correct 2822 ms 42044 KB Output is correct
134 Correct 3068 ms 38320 KB Output is correct
135 Correct 3121 ms 38268 KB Output is correct
136 Correct 3083 ms 38268 KB Output is correct
137 Correct 3019 ms 38856 KB Output is correct
138 Correct 3028 ms 38772 KB Output is correct
139 Correct 3024 ms 38804 KB Output is correct
140 Correct 3279 ms 38764 KB Output is correct
141 Correct 3078 ms 38764 KB Output is correct
142 Correct 2920 ms 38764 KB Output is correct
143 Correct 66 ms 35908 KB Output is correct
144 Correct 184 ms 37832 KB Output is correct