답안 #1031440

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031440 2024-07-22T20:42:26 Z mdn2002 Tourism (JOI23_tourism) C++17
34 / 100
5000 ms 28256 KB
/*
Mayoeba Yabureru
*/
#include<bits/stdc++.h>
using namespace std;
struct FenwickTree {
    vector<int> bit;  // binary indexed tree
    int n;

    FenwickTree(int n) {
        this->n = n;
        bit.assign(n, 0);
    }

    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }
};

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    FenwickTree bit(m + 1);
    vector<vector<int>> gr(n + 1);
    vector st(n + 1, vector<int> (20));
    vector<int> dp(n + 1);
    for (int i = 1; i < n; i ++) {
        int x, y;
        cin >> x >> y;
        gr[x].push_back(y);
        gr[y].push_back(x);
    }
    function<void(int, int)> dfs = [&] (int x, int p) {
        st[x][0] = p;
        dp[x] = dp[p] + 1;
        for (auto u : gr[x]) {
            if (u == p) continue;
            dfs(u, x);
        }
    };
    dfs(1, 0);
    for (int j = 1; j <= 19; j ++) {
        for (int i = 1; i <= n; i ++) st[i][j] = st[st[i][j - 1]][j - 1];
    }
    function lca = [&] (int x, int y) {
        if (dp[x] > dp[y]) swap(x, y);
        int dif = dp[y] - dp[x], bt = 1;
        for (int i = 0; i < 20; i ++) {
            if ((dif & bt)) y = st[y][i];
            bt *= 2;
        }
        if (x == y) return x;
        for (int i = 19; i >= 0; i --) {
            if (st[x][i] != st[y][i]) {
                x = st[x][i];
                y = st[y][i];
            }
        }
        return st[x][0];
    };

    vector<int> c(m + 1), mx(n + 1), ans(q + 1);
    for (int i = 1; i <= m; i ++) cin >> c[i];
    vector qr(m + 1, vector<pair<int, int>>());
    for (int i = 0; i < q; i ++) {
        int l, r;
        cin >> l >> r;
        qr[r].push_back({l, i});
    }
    for (int r = 1; r <= m; r ++) {
        if (r != 1) {
            int z = lca(c[r], c[r - 1]), x = c[r];
            while (true) {
                if (mx[x]) bit.add(mx[x], -1);
                mx[x] = r;
                bit.add(mx[x], 1);
                if (x == z) break;
                x = st[x][0];
            }
            x = c[r - 1];
            while (true) {
                if (x == z) break;
                if (mx[x]) bit.add(mx[x], -1);
                mx[x] = r;
                bit.add(mx[x], 1);
                x = st[x][0];
            }
        }
        for (auto [l, i] : qr[r]) {
            if (l == r) ans[i] = 1;
            else ans[i] = bit.sum(r) - bit.sum(l);
        }
    }

    for (int i = 0; i < q; i ++) cout << ans[i] << endl;
}

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

    int T = 1;
    for (int I = 0; I < T; I ++){
        solve();
    }
}
# 결과 실행 시간 메모리 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 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 600 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 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 604 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 1 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 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 600 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 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 604 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 1 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 6 ms 604 KB Output is correct
32 Correct 4 ms 860 KB Output is correct
33 Correct 4 ms 972 KB Output is correct
34 Correct 4 ms 856 KB Output is correct
35 Correct 4 ms 860 KB Output is correct
36 Correct 4 ms 860 KB Output is correct
37 Correct 4 ms 860 KB Output is correct
38 Correct 21 ms 1072 KB Output is correct
39 Correct 21 ms 1080 KB Output is correct
40 Correct 22 ms 856 KB Output is correct
41 Correct 20 ms 860 KB Output is correct
42 Correct 21 ms 860 KB Output is correct
43 Correct 21 ms 860 KB Output is correct
44 Correct 12 ms 984 KB Output is correct
45 Correct 10 ms 860 KB Output is correct
46 Correct 11 ms 860 KB Output is correct
47 Correct 10 ms 1000 KB Output is correct
48 Correct 9 ms 860 KB Output is correct
49 Correct 12 ms 860 KB Output is correct
50 Correct 3 ms 992 KB Output is correct
51 Correct 3 ms 868 KB Output is correct
52 Correct 4 ms 860 KB Output is correct
53 Correct 4 ms 860 KB Output is correct
54 Correct 5 ms 860 KB Output is correct
55 Correct 3 ms 856 KB Output is correct
56 Correct 3 ms 348 KB Output is correct
57 Correct 3 ms 640 KB Output is correct
58 Correct 4 ms 784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Execution timed out 5086 ms 21228 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 78 ms 12140 KB Output is correct
3 Correct 105 ms 13884 KB Output is correct
4 Correct 81 ms 15956 KB Output is correct
5 Correct 124 ms 23380 KB Output is correct
6 Correct 131 ms 23244 KB Output is correct
7 Correct 150 ms 23396 KB Output is correct
8 Correct 124 ms 23212 KB Output is correct
9 Correct 134 ms 23280 KB Output is correct
10 Correct 131 ms 23380 KB Output is correct
11 Correct 134 ms 23892 KB Output is correct
12 Correct 137 ms 23376 KB Output is correct
13 Correct 140 ms 23888 KB Output is correct
14 Correct 169 ms 24660 KB Output is correct
15 Correct 238 ms 28256 KB Output is correct
16 Correct 139 ms 23888 KB Output is correct
17 Correct 148 ms 23936 KB Output is correct
18 Correct 137 ms 23896 KB Output is correct
19 Correct 4515 ms 23476 KB Output is correct
20 Execution timed out 5021 ms 23376 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 3 ms 588 KB Output is correct
4 Correct 207 ms 18256 KB Output is correct
5 Correct 204 ms 18680 KB Output is correct
6 Correct 231 ms 24144 KB Output is correct
7 Correct 302 ms 27220 KB Output is correct
8 Correct 250 ms 27440 KB Output is correct
9 Correct 246 ms 27220 KB Output is correct
10 Correct 249 ms 27220 KB Output is correct
11 Correct 251 ms 27224 KB Output is correct
12 Correct 243 ms 27296 KB Output is correct
13 Correct 261 ms 27424 KB Output is correct
14 Correct 125 ms 7504 KB Output is correct
# 결과 실행 시간 메모리 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 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 600 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 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 604 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 1 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 6 ms 604 KB Output is correct
32 Correct 4 ms 860 KB Output is correct
33 Correct 4 ms 972 KB Output is correct
34 Correct 4 ms 856 KB Output is correct
35 Correct 4 ms 860 KB Output is correct
36 Correct 4 ms 860 KB Output is correct
37 Correct 4 ms 860 KB Output is correct
38 Correct 21 ms 1072 KB Output is correct
39 Correct 21 ms 1080 KB Output is correct
40 Correct 22 ms 856 KB Output is correct
41 Correct 20 ms 860 KB Output is correct
42 Correct 21 ms 860 KB Output is correct
43 Correct 21 ms 860 KB Output is correct
44 Correct 12 ms 984 KB Output is correct
45 Correct 10 ms 860 KB Output is correct
46 Correct 11 ms 860 KB Output is correct
47 Correct 10 ms 1000 KB Output is correct
48 Correct 9 ms 860 KB Output is correct
49 Correct 12 ms 860 KB Output is correct
50 Correct 3 ms 992 KB Output is correct
51 Correct 3 ms 868 KB Output is correct
52 Correct 4 ms 860 KB Output is correct
53 Correct 4 ms 860 KB Output is correct
54 Correct 5 ms 860 KB Output is correct
55 Correct 3 ms 856 KB Output is correct
56 Correct 3 ms 348 KB Output is correct
57 Correct 3 ms 640 KB Output is correct
58 Correct 4 ms 784 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 3 ms 348 KB Output is correct
62 Execution timed out 5086 ms 21228 KB Time limit exceeded
63 Halted 0 ms 0 KB -