Submission #1031512

# Submission time Handle Problem Language Result Execution time Memory
1031512 2024-07-22T22:51:16 Z Wael Tourism (JOI23_tourism) C++17
100 / 100
790 ms 170276 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);
                    }
                }
            }
        }
        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 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 512 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 464 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 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 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 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 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 512 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 464 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 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 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 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 5 ms 1628 KB Output is correct
31 Correct 5 ms 1760 KB Output is correct
32 Correct 7 ms 2140 KB Output is correct
33 Correct 6 ms 2044 KB Output is correct
34 Correct 6 ms 2020 KB Output is correct
35 Correct 6 ms 1884 KB Output is correct
36 Correct 6 ms 1884 KB Output is correct
37 Correct 6 ms 1884 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 3 ms 1464 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 6 ms 1368 KB Output is correct
44 Correct 4 ms 1372 KB Output is correct
45 Correct 4 ms 1576 KB Output is correct
46 Correct 4 ms 1372 KB Output is correct
47 Correct 5 ms 1372 KB Output is correct
48 Correct 4 ms 1372 KB Output is correct
49 Correct 4 ms 1372 KB Output is correct
50 Correct 4 ms 1628 KB Output is correct
51 Correct 4 ms 1624 KB Output is correct
52 Correct 4 ms 1628 KB Output is correct
53 Correct 4 ms 1628 KB Output is correct
54 Correct 4 ms 1628 KB Output is correct
55 Correct 4 ms 1628 KB Output is correct
56 Correct 1 ms 720 KB Output is correct
57 Correct 1 ms 860 KB Output is correct
58 Correct 6 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 149 ms 34300 KB Output is correct
5 Correct 111 ms 37456 KB Output is correct
6 Correct 148 ms 44528 KB Output is correct
7 Correct 190 ms 51792 KB Output is correct
8 Correct 221 ms 51656 KB Output is correct
9 Correct 181 ms 51756 KB Output is correct
10 Correct 187 ms 51792 KB Output is correct
11 Correct 177 ms 51560 KB Output is correct
12 Correct 131 ms 50000 KB Output is correct
13 Correct 133 ms 49928 KB Output is correct
14 Correct 133 ms 50004 KB Output is correct
15 Correct 43 ms 34256 KB Output is correct
16 Correct 185 ms 51412 KB Output is correct
17 Correct 71 ms 15820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 258 ms 55824 KB Output is correct
3 Correct 428 ms 84936 KB Output is correct
4 Correct 302 ms 68040 KB Output is correct
5 Correct 550 ms 106876 KB Output is correct
6 Correct 515 ms 104900 KB Output is correct
7 Correct 526 ms 105068 KB Output is correct
8 Correct 505 ms 104504 KB Output is correct
9 Correct 503 ms 105868 KB Output is correct
10 Correct 513 ms 105928 KB Output is correct
11 Correct 472 ms 104312 KB Output is correct
12 Correct 474 ms 105860 KB Output is correct
13 Correct 507 ms 104996 KB Output is correct
14 Correct 521 ms 106832 KB Output is correct
15 Correct 572 ms 112796 KB Output is correct
16 Correct 518 ms 101844 KB Output is correct
17 Correct 527 ms 104908 KB Output is correct
18 Correct 529 ms 102700 KB Output is correct
19 Correct 341 ms 76116 KB Output is correct
20 Correct 354 ms 76624 KB Output is correct
21 Correct 384 ms 79444 KB Output is correct
22 Correct 378 ms 76624 KB Output is correct
23 Correct 356 ms 74064 KB Output is correct
24 Correct 424 ms 75260 KB Output is correct
25 Correct 354 ms 76116 KB Output is correct
26 Correct 385 ms 77040 KB Output is correct
27 Correct 351 ms 76372 KB Output is correct
28 Correct 364 ms 77136 KB Output is correct
29 Correct 384 ms 78052 KB Output is correct
30 Correct 389 ms 79240 KB Output is correct
31 Correct 365 ms 76624 KB Output is correct
32 Correct 484 ms 78932 KB Output is correct
33 Correct 392 ms 82004 KB Output is correct
34 Correct 377 ms 77648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 674 ms 141076 KB Output is correct
5 Correct 708 ms 145976 KB Output is correct
6 Correct 790 ms 163780 KB Output is correct
7 Correct 728 ms 170036 KB Output is correct
8 Correct 704 ms 170176 KB Output is correct
9 Correct 745 ms 169924 KB Output is correct
10 Correct 779 ms 170276 KB Output is correct
11 Correct 755 ms 169936 KB Output is correct
12 Correct 748 ms 169924 KB Output is correct
13 Correct 783 ms 170184 KB Output is correct
14 Correct 66 ms 15748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 512 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 464 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 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 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 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 5 ms 1628 KB Output is correct
31 Correct 5 ms 1760 KB Output is correct
32 Correct 7 ms 2140 KB Output is correct
33 Correct 6 ms 2044 KB Output is correct
34 Correct 6 ms 2020 KB Output is correct
35 Correct 6 ms 1884 KB Output is correct
36 Correct 6 ms 1884 KB Output is correct
37 Correct 6 ms 1884 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 3 ms 1464 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 6 ms 1368 KB Output is correct
44 Correct 4 ms 1372 KB Output is correct
45 Correct 4 ms 1576 KB Output is correct
46 Correct 4 ms 1372 KB Output is correct
47 Correct 5 ms 1372 KB Output is correct
48 Correct 4 ms 1372 KB Output is correct
49 Correct 4 ms 1372 KB Output is correct
50 Correct 4 ms 1628 KB Output is correct
51 Correct 4 ms 1624 KB Output is correct
52 Correct 4 ms 1628 KB Output is correct
53 Correct 4 ms 1628 KB Output is correct
54 Correct 4 ms 1628 KB Output is correct
55 Correct 4 ms 1628 KB Output is correct
56 Correct 1 ms 720 KB Output is correct
57 Correct 1 ms 860 KB Output is correct
58 Correct 6 ms 1884 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 149 ms 34300 KB Output is correct
63 Correct 111 ms 37456 KB Output is correct
64 Correct 148 ms 44528 KB Output is correct
65 Correct 190 ms 51792 KB Output is correct
66 Correct 221 ms 51656 KB Output is correct
67 Correct 181 ms 51756 KB Output is correct
68 Correct 187 ms 51792 KB Output is correct
69 Correct 177 ms 51560 KB Output is correct
70 Correct 131 ms 50000 KB Output is correct
71 Correct 133 ms 49928 KB Output is correct
72 Correct 133 ms 50004 KB Output is correct
73 Correct 43 ms 34256 KB Output is correct
74 Correct 185 ms 51412 KB Output is correct
75 Correct 71 ms 15820 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 258 ms 55824 KB Output is correct
78 Correct 428 ms 84936 KB Output is correct
79 Correct 302 ms 68040 KB Output is correct
80 Correct 550 ms 106876 KB Output is correct
81 Correct 515 ms 104900 KB Output is correct
82 Correct 526 ms 105068 KB Output is correct
83 Correct 505 ms 104504 KB Output is correct
84 Correct 503 ms 105868 KB Output is correct
85 Correct 513 ms 105928 KB Output is correct
86 Correct 472 ms 104312 KB Output is correct
87 Correct 474 ms 105860 KB Output is correct
88 Correct 507 ms 104996 KB Output is correct
89 Correct 521 ms 106832 KB Output is correct
90 Correct 572 ms 112796 KB Output is correct
91 Correct 518 ms 101844 KB Output is correct
92 Correct 527 ms 104908 KB Output is correct
93 Correct 529 ms 102700 KB Output is correct
94 Correct 341 ms 76116 KB Output is correct
95 Correct 354 ms 76624 KB Output is correct
96 Correct 384 ms 79444 KB Output is correct
97 Correct 378 ms 76624 KB Output is correct
98 Correct 356 ms 74064 KB Output is correct
99 Correct 424 ms 75260 KB Output is correct
100 Correct 354 ms 76116 KB Output is correct
101 Correct 385 ms 77040 KB Output is correct
102 Correct 351 ms 76372 KB Output is correct
103 Correct 364 ms 77136 KB Output is correct
104 Correct 384 ms 78052 KB Output is correct
105 Correct 389 ms 79240 KB Output is correct
106 Correct 365 ms 76624 KB Output is correct
107 Correct 484 ms 78932 KB Output is correct
108 Correct 392 ms 82004 KB Output is correct
109 Correct 377 ms 77648 KB Output is correct
110 Correct 0 ms 348 KB Output is correct
111 Correct 1 ms 348 KB Output is correct
112 Correct 1 ms 604 KB Output is correct
113 Correct 674 ms 141076 KB Output is correct
114 Correct 708 ms 145976 KB Output is correct
115 Correct 790 ms 163780 KB Output is correct
116 Correct 728 ms 170036 KB Output is correct
117 Correct 704 ms 170176 KB Output is correct
118 Correct 745 ms 169924 KB Output is correct
119 Correct 779 ms 170276 KB Output is correct
120 Correct 755 ms 169936 KB Output is correct
121 Correct 748 ms 169924 KB Output is correct
122 Correct 783 ms 170184 KB Output is correct
123 Correct 66 ms 15748 KB Output is correct
124 Correct 470 ms 94236 KB Output is correct
125 Correct 367 ms 75424 KB Output is correct
126 Correct 568 ms 111484 KB Output is correct
127 Correct 563 ms 112908 KB Output is correct
128 Correct 523 ms 111428 KB Output is correct
129 Correct 531 ms 110696 KB Output is correct
130 Correct 583 ms 108260 KB Output is correct
131 Correct 192 ms 51536 KB Output is correct
132 Correct 222 ms 51728 KB Output is correct
133 Correct 212 ms 47956 KB Output is correct
134 Correct 429 ms 78064 KB Output is correct
135 Correct 401 ms 81048 KB Output is correct
136 Correct 422 ms 80708 KB Output is correct
137 Correct 314 ms 59588 KB Output is correct
138 Correct 298 ms 59708 KB Output is correct
139 Correct 276 ms 59588 KB Output is correct
140 Correct 294 ms 59588 KB Output is correct
141 Correct 289 ms 59520 KB Output is correct
142 Correct 266 ms 59588 KB Output is correct
143 Correct 50 ms 20812 KB Output is correct
144 Correct 544 ms 109952 KB Output is correct