답안 #938983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
938983 2024-03-06T02:40:21 Z juliany2 Tourism (JOI23_tourism) C++17
100 / 100
922 ms 32904 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()

template<class T> struct BIT {
    int n;
    vector<T> bit;

    void init(int sz) { bit = vector<T> (n = sz + 7); }

    void upd(int i, T x) { 
        for (i++; i < n; i += i & -i) bit[i] += x;
    }

    T query(int i) {
        T ret = 0;
        for (i++; i > 0; i -= i & -i) ret += bit[i];
        return ret;
    }

    T query(int l, int r) { return query(r) - query(l - 1); };
};

const int N = 1e5 + 7, L = 20;
int n, m, q;
int c[N];
int lift[N][L], dep[N], sz[N], ans[N];
int head[N], pos[N], timer;
vector<int> adj[N];
vector<array<int, 2>> ask[N];
set<array<int, 3>> s;
BIT<int> bit;

void dfs(int v = 1, int p = 0) {
    sz[v] = 1;
    lift[v][0] = p;
    for (int i = 1; i < L; i++)
        lift[v][i] = lift[lift[v][i - 1]][i - 1];

    for (int &u : adj[v]) {
        if (u != p) {
            dep[u] = dep[v] + 1;
            dfs(u, v);

            sz[v] += sz[u];
            if (adj[v][0] == p || sz[u] > sz[adj[v][0]])
                swap(u, adj[v][0]);
        }
    }
}

void dfs_hld(int v = 1, int p = 0) {
    pos[v] = ++timer;
    for (int u : adj[v]) {
        if (u != p) {
            head[u] = (u == adj[v][0] ? head[v] : u);
            dfs_hld(u, v);
        }
    }
}

void add(int l, int r, int x) {
    array<int, 3> comp = {l, -1, -1};
    while (s.lower_bound(comp) != s.end() && (*s.lower_bound(comp))[0] <= r) {
        auto [a, b, y] = *s.lower_bound({l, -1, -1});
        if (b <= r) {
            bit.upd(y, -(b - a + 1));
            s.erase({a, b, y});
        }
        else {
            bit.upd(y, -(r - a + 1));
            s.insert({r + 1, b, y});
            s.erase({a, b, y});
        }
    }

    bit.upd(x, r - l + 1);
    s.insert({l, r, x});
}

void query(int v, int x) {
    for (; v > 0; v = lift[head[v]][0])
        add(pos[head[v]], pos[v], x);
}

int lca(int u, int v) {
    if (u == 0 || v == 0)
        return max(u, v);
    if (dep[u] > dep[v])
        swap(u, v);

    for (int i = L - 1; ~i; --i)
        if (dep[v] - (1 << i) >= dep[u])
            v = lift[v][i];

    if (u == v)
        return u;

    for (int i = L - 1; ~i; --i)
        if (lift[v][i] != lift[u][i])
            v = lift[v][i], u = lift[u][i];
    return lift[u][0];
}

template<class T> struct ST_LCA {
    static constexpr T ID = 0; // or whatever ID
    inline T comb(T a, T b) { return lca(a, b); } // or whatever function

    int sz;
    vector<T> t;

    void init(int _sz, T val = ID) {
        t.assign((sz = _sz) * 2, ID);
    }
    void init(vector<T> &v) {
        t.resize((sz = v.size()) * 2);
        for (int i = 0; i < sz; ++i)
            t[i + sz] = v[i];
        for (int i = sz - 1; i; --i)
            t[i] = comb(t[i * 2], t[(i * 2) | 1]);
    }
    void upd(int i, T x) {
        for (t[i += sz] = x; i > 1; i >>= 1)
            t[i >> 1] = comb(t[i], t[i ^ 1]);
    }
    T query(int l, int r) {
        T ql = ID, qr = ID;
        for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) ql = comb(ql, t[l++]);
            if (r & 1) qr = comb(t[--r], qr);
        }
        return comb(ql, qr);
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(false);

    cin >> n >> m >> q;

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for (int i = 1; i <= m; i++)
        cin >> c[i];

    for (int i = 1; i <= q; i++) {
        int l, r;
        cin >> l >> r;

        ask[l].push_back({r, i});
    }

    dfs();

    head[1] = 1;

    dfs_hld();

    ST_LCA<int> lcas;
    lcas.init(m + 1);
    for (int i = 1; i <= m; i++)
        lcas.upd(i, c[i]);

    bit.init(m + 1);

    for (int i = m; i >= 1; i--) {
        query(c[i], i);

        for (auto &[j, idx] : ask[i])
            ans[idx] = bit.query(i, j) - dep[lcas.query(i, j)];
    }

    for (int i = 1; i <= q; i++)
        cout << ans[i] << '\n';

    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 10888 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10840 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 10896 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10848 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 3 ms 10840 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
21 Correct 3 ms 10844 KB Output is correct
22 Correct 3 ms 10840 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10848 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 3 ms 10996 KB Output is correct
29 Correct 3 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 10888 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10840 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 10896 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10848 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 3 ms 10840 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
21 Correct 3 ms 10844 KB Output is correct
22 Correct 3 ms 10840 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10848 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 3 ms 10996 KB Output is correct
29 Correct 3 ms 10844 KB Output is correct
30 Correct 8 ms 10844 KB Output is correct
31 Correct 9 ms 10904 KB Output is correct
32 Correct 8 ms 11100 KB Output is correct
33 Correct 9 ms 11100 KB Output is correct
34 Correct 7 ms 11100 KB Output is correct
35 Correct 7 ms 10988 KB Output is correct
36 Correct 8 ms 11096 KB Output is correct
37 Correct 7 ms 11096 KB Output is correct
38 Correct 5 ms 11180 KB Output is correct
39 Correct 6 ms 11096 KB Output is correct
40 Correct 5 ms 11096 KB Output is correct
41 Correct 5 ms 11100 KB Output is correct
42 Correct 5 ms 11100 KB Output is correct
43 Correct 5 ms 11100 KB Output is correct
44 Correct 6 ms 11100 KB Output is correct
45 Correct 6 ms 11100 KB Output is correct
46 Correct 6 ms 11100 KB Output is correct
47 Correct 6 ms 11080 KB Output is correct
48 Correct 6 ms 10844 KB Output is correct
49 Correct 6 ms 11100 KB Output is correct
50 Correct 6 ms 11056 KB Output is correct
51 Correct 5 ms 11100 KB Output is correct
52 Correct 6 ms 11112 KB Output is correct
53 Correct 5 ms 11060 KB Output is correct
54 Correct 6 ms 11100 KB Output is correct
55 Correct 5 ms 11100 KB Output is correct
56 Correct 3 ms 10844 KB Output is correct
57 Correct 3 ms 10844 KB Output is correct
58 Correct 7 ms 10992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 207 ms 26196 KB Output is correct
5 Correct 193 ms 28200 KB Output is correct
6 Correct 213 ms 29924 KB Output is correct
7 Correct 283 ms 32848 KB Output is correct
8 Correct 297 ms 32892 KB Output is correct
9 Correct 292 ms 32828 KB Output is correct
10 Correct 289 ms 32836 KB Output is correct
11 Correct 283 ms 32904 KB Output is correct
12 Correct 223 ms 32220 KB Output is correct
13 Correct 226 ms 32300 KB Output is correct
14 Correct 225 ms 32384 KB Output is correct
15 Correct 46 ms 28356 KB Output is correct
16 Correct 158 ms 32572 KB Output is correct
17 Correct 140 ms 15856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 250 ms 20304 KB Output is correct
3 Correct 406 ms 22160 KB Output is correct
4 Correct 311 ms 21552 KB Output is correct
5 Correct 511 ms 24672 KB Output is correct
6 Correct 538 ms 24636 KB Output is correct
7 Correct 499 ms 24912 KB Output is correct
8 Correct 509 ms 24656 KB Output is correct
9 Correct 504 ms 24656 KB Output is correct
10 Correct 514 ms 24688 KB Output is correct
11 Correct 533 ms 24928 KB Output is correct
12 Correct 506 ms 24916 KB Output is correct
13 Correct 520 ms 25320 KB Output is correct
14 Correct 550 ms 26172 KB Output is correct
15 Correct 565 ms 29056 KB Output is correct
16 Correct 508 ms 25264 KB Output is correct
17 Correct 528 ms 25428 KB Output is correct
18 Correct 486 ms 25140 KB Output is correct
19 Correct 383 ms 21576 KB Output is correct
20 Correct 346 ms 21628 KB Output is correct
21 Correct 376 ms 21572 KB Output is correct
22 Correct 377 ms 21588 KB Output is correct
23 Correct 350 ms 21832 KB Output is correct
24 Correct 366 ms 21588 KB Output is correct
25 Correct 356 ms 21736 KB Output is correct
26 Correct 358 ms 21672 KB Output is correct
27 Correct 376 ms 21772 KB Output is correct
28 Correct 358 ms 21724 KB Output is correct
29 Correct 355 ms 21748 KB Output is correct
30 Correct 383 ms 21840 KB Output is correct
31 Correct 389 ms 22384 KB Output is correct
32 Correct 388 ms 22700 KB Output is correct
33 Correct 405 ms 23864 KB Output is correct
34 Correct 376 ms 26160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 5 ms 10844 KB Output is correct
4 Correct 727 ms 25100 KB Output is correct
5 Correct 748 ms 25252 KB Output is correct
6 Correct 833 ms 27692 KB Output is correct
7 Correct 922 ms 28692 KB Output is correct
8 Correct 870 ms 28752 KB Output is correct
9 Correct 860 ms 28880 KB Output is correct
10 Correct 908 ms 28956 KB Output is correct
11 Correct 912 ms 28672 KB Output is correct
12 Correct 878 ms 28900 KB Output is correct
13 Correct 878 ms 28712 KB Output is correct
14 Correct 108 ms 15708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 3 ms 10888 KB Output is correct
4 Correct 2 ms 10840 KB Output is correct
5 Correct 2 ms 10840 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 10896 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10848 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 3 ms 10840 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
21 Correct 3 ms 10844 KB Output is correct
22 Correct 3 ms 10840 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10848 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 3 ms 10996 KB Output is correct
29 Correct 3 ms 10844 KB Output is correct
30 Correct 8 ms 10844 KB Output is correct
31 Correct 9 ms 10904 KB Output is correct
32 Correct 8 ms 11100 KB Output is correct
33 Correct 9 ms 11100 KB Output is correct
34 Correct 7 ms 11100 KB Output is correct
35 Correct 7 ms 10988 KB Output is correct
36 Correct 8 ms 11096 KB Output is correct
37 Correct 7 ms 11096 KB Output is correct
38 Correct 5 ms 11180 KB Output is correct
39 Correct 6 ms 11096 KB Output is correct
40 Correct 5 ms 11096 KB Output is correct
41 Correct 5 ms 11100 KB Output is correct
42 Correct 5 ms 11100 KB Output is correct
43 Correct 5 ms 11100 KB Output is correct
44 Correct 6 ms 11100 KB Output is correct
45 Correct 6 ms 11100 KB Output is correct
46 Correct 6 ms 11100 KB Output is correct
47 Correct 6 ms 11080 KB Output is correct
48 Correct 6 ms 10844 KB Output is correct
49 Correct 6 ms 11100 KB Output is correct
50 Correct 6 ms 11056 KB Output is correct
51 Correct 5 ms 11100 KB Output is correct
52 Correct 6 ms 11112 KB Output is correct
53 Correct 5 ms 11060 KB Output is correct
54 Correct 6 ms 11100 KB Output is correct
55 Correct 5 ms 11100 KB Output is correct
56 Correct 3 ms 10844 KB Output is correct
57 Correct 3 ms 10844 KB Output is correct
58 Correct 7 ms 10992 KB Output is correct
59 Correct 2 ms 10840 KB Output is correct
60 Correct 2 ms 10844 KB Output is correct
61 Correct 3 ms 10844 KB Output is correct
62 Correct 207 ms 26196 KB Output is correct
63 Correct 193 ms 28200 KB Output is correct
64 Correct 213 ms 29924 KB Output is correct
65 Correct 283 ms 32848 KB Output is correct
66 Correct 297 ms 32892 KB Output is correct
67 Correct 292 ms 32828 KB Output is correct
68 Correct 289 ms 32836 KB Output is correct
69 Correct 283 ms 32904 KB Output is correct
70 Correct 223 ms 32220 KB Output is correct
71 Correct 226 ms 32300 KB Output is correct
72 Correct 225 ms 32384 KB Output is correct
73 Correct 46 ms 28356 KB Output is correct
74 Correct 158 ms 32572 KB Output is correct
75 Correct 140 ms 15856 KB Output is correct
76 Correct 2 ms 10840 KB Output is correct
77 Correct 250 ms 20304 KB Output is correct
78 Correct 406 ms 22160 KB Output is correct
79 Correct 311 ms 21552 KB Output is correct
80 Correct 511 ms 24672 KB Output is correct
81 Correct 538 ms 24636 KB Output is correct
82 Correct 499 ms 24912 KB Output is correct
83 Correct 509 ms 24656 KB Output is correct
84 Correct 504 ms 24656 KB Output is correct
85 Correct 514 ms 24688 KB Output is correct
86 Correct 533 ms 24928 KB Output is correct
87 Correct 506 ms 24916 KB Output is correct
88 Correct 520 ms 25320 KB Output is correct
89 Correct 550 ms 26172 KB Output is correct
90 Correct 565 ms 29056 KB Output is correct
91 Correct 508 ms 25264 KB Output is correct
92 Correct 528 ms 25428 KB Output is correct
93 Correct 486 ms 25140 KB Output is correct
94 Correct 383 ms 21576 KB Output is correct
95 Correct 346 ms 21628 KB Output is correct
96 Correct 376 ms 21572 KB Output is correct
97 Correct 377 ms 21588 KB Output is correct
98 Correct 350 ms 21832 KB Output is correct
99 Correct 366 ms 21588 KB Output is correct
100 Correct 356 ms 21736 KB Output is correct
101 Correct 358 ms 21672 KB Output is correct
102 Correct 376 ms 21772 KB Output is correct
103 Correct 358 ms 21724 KB Output is correct
104 Correct 355 ms 21748 KB Output is correct
105 Correct 383 ms 21840 KB Output is correct
106 Correct 389 ms 22384 KB Output is correct
107 Correct 388 ms 22700 KB Output is correct
108 Correct 405 ms 23864 KB Output is correct
109 Correct 376 ms 26160 KB Output is correct
110 Correct 2 ms 10844 KB Output is correct
111 Correct 3 ms 10844 KB Output is correct
112 Correct 5 ms 10844 KB Output is correct
113 Correct 727 ms 25100 KB Output is correct
114 Correct 748 ms 25252 KB Output is correct
115 Correct 833 ms 27692 KB Output is correct
116 Correct 922 ms 28692 KB Output is correct
117 Correct 870 ms 28752 KB Output is correct
118 Correct 860 ms 28880 KB Output is correct
119 Correct 908 ms 28956 KB Output is correct
120 Correct 912 ms 28672 KB Output is correct
121 Correct 878 ms 28900 KB Output is correct
122 Correct 878 ms 28712 KB Output is correct
123 Correct 108 ms 15708 KB Output is correct
124 Correct 502 ms 27684 KB Output is correct
125 Correct 381 ms 25168 KB Output is correct
126 Correct 609 ms 28396 KB Output is correct
127 Correct 628 ms 28344 KB Output is correct
128 Correct 626 ms 28732 KB Output is correct
129 Correct 608 ms 28500 KB Output is correct
130 Correct 625 ms 28336 KB Output is correct
131 Correct 277 ms 31728 KB Output is correct
132 Correct 361 ms 32652 KB Output is correct
133 Correct 290 ms 28880 KB Output is correct
134 Correct 457 ms 25428 KB Output is correct
135 Correct 497 ms 25424 KB Output is correct
136 Correct 557 ms 25436 KB Output is correct
137 Correct 245 ms 29392 KB Output is correct
138 Correct 271 ms 29324 KB Output is correct
139 Correct 278 ms 29380 KB Output is correct
140 Correct 257 ms 29368 KB Output is correct
141 Correct 262 ms 29360 KB Output is correct
142 Correct 266 ms 29384 KB Output is correct
143 Correct 74 ms 20944 KB Output is correct
144 Correct 538 ms 28240 KB Output is correct