Submission #1031514

# Submission time Handle Problem Language Result Execution time Memory
1031514 2024-07-22T23:00:50 Z Wael Tourism (JOI23_tourism) C++17
100 / 100
695 ms 88256 KB
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

struct SegmentTree {
    int n;
    vector<int> sum;
    SegmentTree(int n) : n(n) {
        sum.assign(4 * n, 0);
    }

    void update(int i, int v) {
        update(i, v, 0, 0, n - 1);
    }   
    void update(int i, int v, int x, int lx, int rx) {
        if (lx == rx) {
            sum[x] += v;
            return;
        }
        int mid = (lx + rx) / 2;
        if (i <= mid) {
            update(i, v, 2 * x + 1, lx, mid);            
        } else {
            update(i, v, 2 * x + 2, mid + 1, rx);
        }
        sum[x] = sum[2 * x + 1] + sum[2 * x + 2];
    }

    int get(int l, int r) {
        return get(l, r, 0, 0, n - 1);
    }
    int get(int l, int r, int x, int lx, int rx) {
        if (lx > r || rx < l) return 0;
        if (l <= lx && rx <= r) return sum[x];
        int mid = (lx + rx) / 2;
        return get(l, r, 2 * x + 1, lx, mid) + get(l, r, 2 * x + 2, mid + 1, rx);
    }
};

void solve() {
    int n, m, q;
    cin >> n >> m >> q;

    vector<vector<int>> adj(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    vector<int> c(m);
    vector<vector<int>> indices(n);
    for (int i = 0; i < m; ++i) {
        cin >> c[i];
        --c[i];
        indices[c[i]].push_back(i);
    }

    vector<int> l(q), r(q);
    for (int i = 0; i < q; ++i) {
        cin >> l[i] >> r[i];
        --l[i], --r[i];
    }

    vector<int> dep(n), sz(n);
    function<void(int, int)> calc = [&](int u, int p) {
        for (int i = 0; i < adj[u].size(); ++i) {
            if (adj[u][i] == p) {
                swap(adj[u][i], adj[u].back());
                adj[u].pop_back();
                break;
            }
        }

        sz[u] = 1;
        for (auto v : adj[u]) {
            dep[v] = dep[u] + 1;
            calc(v, u);
            sz[u] += sz[v];
        }

        for (int i = 1; i < adj[u].size(); ++i) {
            if (sz[adj[u][i]] > sz[adj[u][0]]) {
                swap(adj[u][i], adj[u][0]);
            }
        }
    };
    calc(0, -1);

    vector<set<array<int, 4>>> st(n);
    vector<vector<pair<int, int>>> update(m);
    
    auto addRange = [&](int l, int r, int v) {
        if (l > r) return;
        update[l].push_back({l, v});
        if (r + 1 < m) {
          update[r + 1].push_back({l, -v});
        }
    };

    int const inf = 1e9;
    auto append = [&](int u, int l, int r) {
        auto it = prev(st[u].upper_bound(array{l, inf, inf, inf}));
        auto [L, R, d, t] = *it;
        addRange(L, R, d - dep[u]);
        if (L == l && r == R && l > 0 && r + 1 < m) {
            auto itL = prev(it);
            auto itR = next(it);
            addRange((*itL)[0], (*itL)[1], (*itL)[2] - dep[u] + 1);
            addRange((*itR)[0], (*itR)[1], (*itR)[2] - dep[u] + 1);
            int nl = (*itL)[0], nr = (*itR)[1];
            st[u].erase(it);
            st[u].erase(itL);
            st[u].erase(itR);
            st[u].insert(array{nl, nr, dep[u] - 1, 1});
        } else if (L == l && l > 0) {
            auto itL = prev(it);
            auto [cl, cr, cd, ct] = *itL;
            addRange(cl, cr, cd - dep[u] + 1);
            int nl = cl, nr = r;
            st[u].erase(it);
            st[u].erase(itL);
            st[u].insert(array{nl, nr, dep[u] - 1, 1});
            if (r < R) {
                st[u].insert(array{r + 1, R, dep[u], 0});
            }
        } else if (r == R && r + 1 < m) {
            auto itR = next(it);
            auto [cl, cr, cd, ct] = *itR;
            addRange(cl, cr, cd - dep[u] + 1);
            int nl = l, nr = cr;
            st[u].erase(it);
            st[u].erase(itR);
            st[u].insert(array{nl, nr, dep[u] - 1, 1});
            if (L < l) {
                st[u].insert(array{L, l - 1, dep[u], 0});
            }
        } else {
            st[u].erase(it);
            st[u].insert(array{l, r, dep[u] - 1, 1});
            if (L < l) {
                st[u].insert(array{L, l - 1, dep[u], 0});
            }
            if (r < R) {
                st[u].insert(array{r + 1, R, dep[u], 0});
            }
        }
    };

    function<void(int)> dfs = [&](int u) {
        for (int i = 0; i < adj[u].size(); ++i) {
            int v = adj[u][i];
            dfs(v);
            if (i == 0) {
                swap(st[u], st[v]);
            } else {
                for (auto [l, r, d, t] : st[v]) {
                    if (t == 1) {
                        addRange(l, r, d - dep[u] + 1);
                        append(u, l, r);
                    } else {
                        addRange(l, r, d - dep[v] + 1);
                    }
                }
                set<array<int, 4>>().swap(st[v]);
            }
        }
        if (st[u].size() == 0) {
            st[u] = {{0, m - 1, dep[u], 0}};
        }
        for (auto i : indices[u]) {
            append(u, i, i);
        }
    };      
    dfs(0);
    for (auto [l, r, d, t] : st[0]) {
        addRange(l, r, d - dep[0] + 1);
    }

    vector<vector<int>> query(m);
    for (int i = 0; i < q; ++i) {
        query[r[i]].push_back(i);
    }

    vector<int> ans(q);
    SegmentTree seg(m);
    for (int i = 0; i < m; ++i) {
        for (auto [j, v] : update[i]) {
            seg.update(j, v);
        }
        for (auto j : query[i]) {
            ans[j] = n - seg.get(0, l[j]);
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    //cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Compilation message

tourism.cpp: In lambda function:
tourism.cpp:69:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         for (int i = 0; i < adj[u].size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~
tourism.cpp:84:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |         for (int i = 1; i < adj[u].size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~
tourism.cpp: In lambda function:
tourism.cpp:153:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |         for (int i = 0; i < adj[u].size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 608 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 556 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 552 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 608 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 556 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 552 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 4 ms 1112 KB Output is correct
31 Correct 5 ms 1116 KB Output is correct
32 Correct 6 ms 1372 KB Output is correct
33 Correct 6 ms 1372 KB Output is correct
34 Correct 6 ms 1476 KB Output is correct
35 Correct 5 ms 1372 KB Output is correct
36 Correct 7 ms 1380 KB Output is correct
37 Correct 5 ms 1368 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 3 ms 1372 KB Output is correct
41 Correct 3 ms 1372 KB Output is correct
42 Correct 3 ms 1372 KB Output is correct
43 Correct 3 ms 1492 KB Output is correct
44 Correct 4 ms 1372 KB Output is correct
45 Correct 4 ms 1372 KB Output is correct
46 Correct 4 ms 1448 KB Output is correct
47 Correct 4 ms 1372 KB Output is correct
48 Correct 4 ms 1368 KB Output is correct
49 Correct 4 ms 1372 KB Output is correct
50 Correct 3 ms 1260 KB Output is correct
51 Correct 4 ms 1116 KB Output is correct
52 Correct 4 ms 1372 KB Output is correct
53 Correct 3 ms 1116 KB Output is correct
54 Correct 4 ms 1112 KB Output is correct
55 Correct 4 ms 1112 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 1 ms 592 KB Output is correct
58 Correct 5 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 143 ms 36692 KB Output is correct
5 Correct 115 ms 41152 KB Output is correct
6 Correct 152 ms 48624 KB Output is correct
7 Correct 185 ms 56144 KB Output is correct
8 Correct 189 ms 56148 KB Output is correct
9 Correct 173 ms 56148 KB Output is correct
10 Correct 189 ms 56144 KB Output is correct
11 Correct 181 ms 56040 KB Output is correct
12 Correct 128 ms 54356 KB Output is correct
13 Correct 122 ms 54356 KB Output is correct
14 Correct 141 ms 54292 KB Output is correct
15 Correct 46 ms 39048 KB Output is correct
16 Correct 165 ms 55892 KB Output is correct
17 Correct 58 ms 15828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 202 ms 31076 KB Output is correct
3 Correct 333 ms 45252 KB Output is correct
4 Correct 291 ms 37572 KB Output is correct
5 Correct 454 ms 58188 KB Output is correct
6 Correct 412 ms 57032 KB Output is correct
7 Correct 426 ms 57796 KB Output is correct
8 Correct 439 ms 58232 KB Output is correct
9 Correct 425 ms 57796 KB Output is correct
10 Correct 448 ms 57288 KB Output is correct
11 Correct 443 ms 56832 KB Output is correct
12 Correct 450 ms 57288 KB Output is correct
13 Correct 496 ms 57300 KB Output is correct
14 Correct 493 ms 58312 KB Output is correct
15 Correct 534 ms 60360 KB Output is correct
16 Correct 476 ms 55628 KB Output is correct
17 Correct 444 ms 57032 KB Output is correct
18 Correct 429 ms 56360 KB Output is correct
19 Correct 322 ms 50000 KB Output is correct
20 Correct 353 ms 51396 KB Output is correct
21 Correct 370 ms 53088 KB Output is correct
22 Correct 352 ms 50580 KB Output is correct
23 Correct 336 ms 49232 KB Output is correct
24 Correct 363 ms 49232 KB Output is correct
25 Correct 333 ms 50060 KB Output is correct
26 Correct 392 ms 51796 KB Output is correct
27 Correct 335 ms 50176 KB Output is correct
28 Correct 391 ms 50424 KB Output is correct
29 Correct 350 ms 51792 KB Output is correct
30 Correct 367 ms 51284 KB Output is correct
31 Correct 362 ms 50768 KB Output is correct
32 Correct 363 ms 51532 KB Output is correct
33 Correct 351 ms 53328 KB Output is correct
34 Correct 368 ms 51536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 760 KB Output is correct
4 Correct 551 ms 72600 KB Output is correct
5 Correct 601 ms 74952 KB Output is correct
6 Correct 638 ms 84548 KB Output is correct
7 Correct 695 ms 88004 KB Output is correct
8 Correct 669 ms 88108 KB Output is correct
9 Correct 679 ms 88256 KB Output is correct
10 Correct 687 ms 88004 KB Output is correct
11 Correct 662 ms 87864 KB Output is correct
12 Correct 689 ms 87924 KB Output is correct
13 Correct 663 ms 88060 KB Output is correct
14 Correct 66 ms 15820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 560 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 608 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 556 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 552 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 4 ms 1112 KB Output is correct
31 Correct 5 ms 1116 KB Output is correct
32 Correct 6 ms 1372 KB Output is correct
33 Correct 6 ms 1372 KB Output is correct
34 Correct 6 ms 1476 KB Output is correct
35 Correct 5 ms 1372 KB Output is correct
36 Correct 7 ms 1380 KB Output is correct
37 Correct 5 ms 1368 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 3 ms 1372 KB Output is correct
40 Correct 3 ms 1372 KB Output is correct
41 Correct 3 ms 1372 KB Output is correct
42 Correct 3 ms 1372 KB Output is correct
43 Correct 3 ms 1492 KB Output is correct
44 Correct 4 ms 1372 KB Output is correct
45 Correct 4 ms 1372 KB Output is correct
46 Correct 4 ms 1448 KB Output is correct
47 Correct 4 ms 1372 KB Output is correct
48 Correct 4 ms 1368 KB Output is correct
49 Correct 4 ms 1372 KB Output is correct
50 Correct 3 ms 1260 KB Output is correct
51 Correct 4 ms 1116 KB Output is correct
52 Correct 4 ms 1372 KB Output is correct
53 Correct 3 ms 1116 KB Output is correct
54 Correct 4 ms 1112 KB Output is correct
55 Correct 4 ms 1112 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 1 ms 592 KB Output is correct
58 Correct 5 ms 1372 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 143 ms 36692 KB Output is correct
63 Correct 115 ms 41152 KB Output is correct
64 Correct 152 ms 48624 KB Output is correct
65 Correct 185 ms 56144 KB Output is correct
66 Correct 189 ms 56148 KB Output is correct
67 Correct 173 ms 56148 KB Output is correct
68 Correct 189 ms 56144 KB Output is correct
69 Correct 181 ms 56040 KB Output is correct
70 Correct 128 ms 54356 KB Output is correct
71 Correct 122 ms 54356 KB Output is correct
72 Correct 141 ms 54292 KB Output is correct
73 Correct 46 ms 39048 KB Output is correct
74 Correct 165 ms 55892 KB Output is correct
75 Correct 58 ms 15828 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 202 ms 31076 KB Output is correct
78 Correct 333 ms 45252 KB Output is correct
79 Correct 291 ms 37572 KB Output is correct
80 Correct 454 ms 58188 KB Output is correct
81 Correct 412 ms 57032 KB Output is correct
82 Correct 426 ms 57796 KB Output is correct
83 Correct 439 ms 58232 KB Output is correct
84 Correct 425 ms 57796 KB Output is correct
85 Correct 448 ms 57288 KB Output is correct
86 Correct 443 ms 56832 KB Output is correct
87 Correct 450 ms 57288 KB Output is correct
88 Correct 496 ms 57300 KB Output is correct
89 Correct 493 ms 58312 KB Output is correct
90 Correct 534 ms 60360 KB Output is correct
91 Correct 476 ms 55628 KB Output is correct
92 Correct 444 ms 57032 KB Output is correct
93 Correct 429 ms 56360 KB Output is correct
94 Correct 322 ms 50000 KB Output is correct
95 Correct 353 ms 51396 KB Output is correct
96 Correct 370 ms 53088 KB Output is correct
97 Correct 352 ms 50580 KB Output is correct
98 Correct 336 ms 49232 KB Output is correct
99 Correct 363 ms 49232 KB Output is correct
100 Correct 333 ms 50060 KB Output is correct
101 Correct 392 ms 51796 KB Output is correct
102 Correct 335 ms 50176 KB Output is correct
103 Correct 391 ms 50424 KB Output is correct
104 Correct 350 ms 51792 KB Output is correct
105 Correct 367 ms 51284 KB Output is correct
106 Correct 362 ms 50768 KB Output is correct
107 Correct 363 ms 51532 KB Output is correct
108 Correct 351 ms 53328 KB Output is correct
109 Correct 368 ms 51536 KB Output is correct
110 Correct 0 ms 600 KB Output is correct
111 Correct 0 ms 348 KB Output is correct
112 Correct 1 ms 760 KB Output is correct
113 Correct 551 ms 72600 KB Output is correct
114 Correct 601 ms 74952 KB Output is correct
115 Correct 638 ms 84548 KB Output is correct
116 Correct 695 ms 88004 KB Output is correct
117 Correct 669 ms 88108 KB Output is correct
118 Correct 679 ms 88256 KB Output is correct
119 Correct 687 ms 88004 KB Output is correct
120 Correct 662 ms 87864 KB Output is correct
121 Correct 689 ms 87924 KB Output is correct
122 Correct 663 ms 88060 KB Output is correct
123 Correct 66 ms 15820 KB Output is correct
124 Correct 431 ms 52524 KB Output is correct
125 Correct 306 ms 42948 KB Output is correct
126 Correct 484 ms 61384 KB Output is correct
127 Correct 471 ms 61500 KB Output is correct
128 Correct 478 ms 60612 KB Output is correct
129 Correct 494 ms 61492 KB Output is correct
130 Correct 475 ms 59092 KB Output is correct
131 Correct 266 ms 54860 KB Output is correct
132 Correct 209 ms 55764 KB Output is correct
133 Correct 209 ms 49616 KB Output is correct
134 Correct 391 ms 52828 KB Output is correct
135 Correct 385 ms 54160 KB Output is correct
136 Correct 440 ms 53996 KB Output is correct
137 Correct 233 ms 40644 KB Output is correct
138 Correct 263 ms 40568 KB Output is correct
139 Correct 236 ms 40560 KB Output is correct
140 Correct 245 ms 40640 KB Output is correct
141 Correct 248 ms 40644 KB Output is correct
142 Correct 242 ms 40592 KB Output is correct
143 Correct 49 ms 17484 KB Output is correct
144 Correct 512 ms 59332 KB Output is correct