Submission #1031418

# Submission time Handle Problem Language Result Execution time Memory
1031418 2024-07-22T20:14:02 Z Wael Tourism (JOI23_tourism) C++17
34 / 100
2068 ms 1048576 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> belong(m);
    vector<vector<int>> st(n);
    vector<vector<pair<int, int>>> update(m);
    
    auto addRange = [&](int l, int r) {
        if (l > r) return;
        update[l].push_back({l, 1});
        if (r + 1 < m) {
          update[r + 1].push_back({l, -1});
        }
    };

    function<void(int, int)> dfs = [&](int u, int p) {
        for (auto v : adj[u]) {
            if (v == p) continue;
            dfs(v, u);
            for (auto i : st[v]) {
                st[u].push_back(i);
            }
        }
        for (auto i : indices[u]) {
            st[u].push_back(i);
            belong[i] = u;
        }
        sort(st[u].begin(), st[u].end());

        if (st[u].size()) {
            addRange(0, st[u][0] - 1);
        } else {
            addRange(0, m - 1);
        }

        auto checkNext = [&](int i) {
            int next = (i + 1 < st[u].size() ? st[u][i + 1] - 1 : m - 1);
            addRange(st[u][i] + 1, next);
        };

        for (int i = 0; i < st[u].size(); ++i) {
            checkNext(i);
            if (belong[st[u][i]] == u) continue;
            int j = i;
            while (j + 1 < st[u].size() && st[u][j] + 1 == st[u][j + 1] && belong[st[u][j]] == belong[st[u][j + 1]]) {
                ++j;
            }
            addRange(st[u][i], st[u][j]);
            if (j != i) {
                checkNext(j);
            }
            i = j;
        }

        for (auto i : st[u]) {
            belong[i] = u;
        }
    };      
    dfs(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:100:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |             int next = (i + 1 < st[u].size() ? st[u][i + 1] - 1 : m - 1);
      |                         ~~~~~~^~~~~~~~~~~~~~
tourism.cpp: In lambda function:
tourism.cpp:104:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |         for (int i = 0; i < st[u].size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~
tourism.cpp:108:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |             while (j + 1 < st[u].size() && st[u][j] + 1 == st[u][j + 1] && belong[st[u][j]] == belong[st[u][j + 1]]) {
      |                    ~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 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 604 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 600 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 3 ms 1372 KB Output is correct
17 Correct 3 ms 1368 KB Output is correct
18 Correct 2 ms 1112 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 2 ms 1188 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 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 344 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 348 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 604 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 600 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 3 ms 1372 KB Output is correct
17 Correct 3 ms 1368 KB Output is correct
18 Correct 2 ms 1112 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 2 ms 1188 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 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 1164 KB Output is correct
31 Correct 4 ms 1416 KB Output is correct
32 Correct 5 ms 1704 KB Output is correct
33 Correct 7 ms 1628 KB Output is correct
34 Correct 5 ms 1628 KB Output is correct
35 Correct 5 ms 1628 KB Output is correct
36 Correct 5 ms 1628 KB Output is correct
37 Correct 5 ms 1624 KB Output is correct
38 Correct 113 ms 34132 KB Output is correct
39 Correct 112 ms 36436 KB Output is correct
40 Correct 109 ms 35988 KB Output is correct
41 Correct 115 ms 37712 KB Output is correct
42 Correct 126 ms 38728 KB Output is correct
43 Correct 96 ms 34128 KB Output is correct
44 Correct 53 ms 18524 KB Output is correct
45 Correct 38 ms 13916 KB Output is correct
46 Correct 68 ms 18416 KB Output is correct
47 Correct 45 ms 15960 KB Output is correct
48 Correct 36 ms 12892 KB Output is correct
49 Correct 50 ms 17612 KB Output is correct
50 Correct 2 ms 1116 KB Output is correct
51 Correct 2 ms 1116 KB Output is correct
52 Correct 3 ms 1116 KB Output is correct
53 Correct 2 ms 1116 KB Output is correct
54 Correct 2 ms 1116 KB Output is correct
55 Correct 3 ms 1112 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 5 ms 1628 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 Runtime error 2068 ms 1048576 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 208 ms 43924 KB Output is correct
3 Correct 358 ms 68448 KB Output is correct
4 Correct 263 ms 56076 KB Output is correct
5 Correct 422 ms 82564 KB Output is correct
6 Correct 453 ms 84424 KB Output is correct
7 Correct 388 ms 82628 KB Output is correct
8 Correct 370 ms 82112 KB Output is correct
9 Correct 383 ms 83656 KB Output is correct
10 Correct 391 ms 84676 KB Output is correct
11 Correct 378 ms 82372 KB Output is correct
12 Correct 430 ms 86064 KB Output is correct
13 Correct 375 ms 82380 KB Output is correct
14 Correct 390 ms 81864 KB Output is correct
15 Correct 462 ms 87672 KB Output is correct
16 Correct 410 ms 84644 KB Output is correct
17 Correct 420 ms 84448 KB Output is correct
18 Correct 422 ms 82628 KB Output is correct
19 Runtime error 1867 ms 1048576 KB Execution killed with signal 9
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 401 ms 73856 KB Output is correct
5 Correct 419 ms 76424 KB Output is correct
6 Correct 441 ms 86636 KB Output is correct
7 Correct 520 ms 88708 KB Output is correct
8 Correct 463 ms 89184 KB Output is correct
9 Correct 478 ms 89032 KB Output is correct
10 Correct 485 ms 89012 KB Output is correct
11 Correct 501 ms 88752 KB Output is correct
12 Correct 461 ms 89164 KB Output is correct
13 Correct 486 ms 88808 KB Output is correct
14 Correct 46 ms 12620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 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 604 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 600 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 1116 KB Output is correct
16 Correct 3 ms 1372 KB Output is correct
17 Correct 3 ms 1368 KB Output is correct
18 Correct 2 ms 1112 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 2 ms 1188 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 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 1164 KB Output is correct
31 Correct 4 ms 1416 KB Output is correct
32 Correct 5 ms 1704 KB Output is correct
33 Correct 7 ms 1628 KB Output is correct
34 Correct 5 ms 1628 KB Output is correct
35 Correct 5 ms 1628 KB Output is correct
36 Correct 5 ms 1628 KB Output is correct
37 Correct 5 ms 1624 KB Output is correct
38 Correct 113 ms 34132 KB Output is correct
39 Correct 112 ms 36436 KB Output is correct
40 Correct 109 ms 35988 KB Output is correct
41 Correct 115 ms 37712 KB Output is correct
42 Correct 126 ms 38728 KB Output is correct
43 Correct 96 ms 34128 KB Output is correct
44 Correct 53 ms 18524 KB Output is correct
45 Correct 38 ms 13916 KB Output is correct
46 Correct 68 ms 18416 KB Output is correct
47 Correct 45 ms 15960 KB Output is correct
48 Correct 36 ms 12892 KB Output is correct
49 Correct 50 ms 17612 KB Output is correct
50 Correct 2 ms 1116 KB Output is correct
51 Correct 2 ms 1116 KB Output is correct
52 Correct 3 ms 1116 KB Output is correct
53 Correct 2 ms 1116 KB Output is correct
54 Correct 2 ms 1116 KB Output is correct
55 Correct 3 ms 1112 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 5 ms 1628 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 Runtime error 2068 ms 1048576 KB Execution killed with signal 9
63 Halted 0 ms 0 KB -