답안 #939554

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
939554 2024-03-06T13:53:52 Z alextodoran Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 23604 KB
/**
 _  _   __  _ _ _  _  _ _
 |a  ||t  ||o    d | |o  |
| __    _| | _ | __|  _ |
| __ |/_  | __  /__\ / _\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 100000;
const int M_MAX = 100000;
const int Q_MAX = 100000;
const int LOG_N = 17;

int N, M, Q;
vector <int> adj[N_MAX + 2];
int C[M_MAX + 2];
int ql[Q_MAX + 2], qr[Q_MAX + 2];

int parent[N_MAX + 2];
int depth[N_MAX + 2];
int order[N_MAX + 2];
int L[N_MAX + 2], R[N_MAX + 2];
int curr;

int anc[N_MAX + 2][LOG_N];

int ancestor (int u, int k) {
    for (int e = 0; e < LOG_N; e++) {
        if ((k >> e) & 1) {
            u = anc[u][e];
        }
    }
    return u;
}
int lca (int u, int v) {
    if (u == 0 || v == 0) {
        return 0;
    }
    if (depth[u] > depth[v]) {
        u = ancestor(u, depth[u] - depth[v]);
    }
    if (depth[v] > depth[u]) {
        v = ancestor(v, depth[v] - depth[u]);
    }
    if (u == v) {
        return u;
    }
    for (int e = LOG_N - 1; e >= 0; e--) {
        if (anc[u][e] != anc[v][e]) {
            u = anc[u][e];
            v = anc[v][e];
        }
    }
    return parent[u];
}

void dfs (int u) {
    L[u] = ++curr;
    order[curr] = u;
    for (int v : adj[u]) {
        if (v != parent[u]) {
            parent[v] = u;
            depth[v] = depth[u] + 1;
            dfs(v);
        }
    }
    R[u] = curr;
}

int bucket;
int get_bucket (int i) {
    return (i - 1) / bucket + 1;
}
vector <int> queries[N_MAX + 2];

int cnt, root;

int occ[N_MAX + 2];
set <int> S;
void add (int u) {
    // If u is at its first occurrence
    if (++occ[u] == 1) {
        // If u is the only node
        if (S.empty() == true) {
            S.insert(L[u]);
            cnt = 1; root = u;
            return;
        }
        // Otherwise
        set <int> :: iterator it = S.insert(L[u]).first;
        int ul = (it != S.begin() ? order[*prev(it)] : 0);
        int ur = (next(it) != S.end() ? order[*next(it)] : 0);
        // If there are no other nodes under u
        if (ur == 0 || R[u] < L[ur]) {
            ul = lca(u, ul);
            ur = lca(u, ur);
            // Find the lowest common ancestor between u and another node in S
            int v = (depth[ul] > depth[ur] ? ul : ur);
            cnt += depth[u] - depth[v];
        }
        // Find the new root (lowest common ancestor of all nodes in S)
        int v = lca(u, root);
        cnt += depth[root] - depth[v];
        root = v;
    }
}
void del (int u) {
    if (--occ[u] == 0) {
        S.erase(L[u]);
    }
}

int answer[Q_MAX + 2];

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

    cin >> N >> M >> Q;
    for (int i = 1; i <= N - 1; 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++) {
        cin >> ql[i] >> qr[i];
    }
    depth[1] = 1;
    dfs(1);
    for (int u = 1; u <= N; u++) {
        anc[u][0] = parent[u];
    }
    for (int e = 1; e < LOG_N; e++) {
        for (int u = 1; u <= N; u++) {
            anc[u][e] = anc[anc[u][e - 1]][e - 1];
        }
    }
    bucket = max(1, min(M, (int) (M / sqrt(Q))));
    for (int i = 1; i <= Q; i++) {
        int bl = get_bucket(ql[i]), br = get_bucket(qr[i]);
        if (bl < br) {
            queries[bl].push_back(i);
        } else {
            for (int j = ql[i]; j <= qr[i]; j++) {
                add(C[j]);
            }
            answer[i] = cnt;
            for (int j = qr[i]; j >= ql[i]; j--) {
                del(C[j]);
            }
            cnt = root = 0;
        }
    }
    for (int b = 1; b <= get_bucket(M) - 1; b++) {
        sort(queries[b].begin(), queries[b].end(), [&] (const int &i, const int &j) {
            return qr[i] < qr[j];
        });
        int r = b * bucket;
        for (int i : queries[b]) {
            while (r < qr[i]) {
                add(C[++r]);
            }
            int prv_cnt = cnt, prv_root = root;
            for (int j = b * bucket; j >= ql[i]; j--) {
                add(C[j]);
            }
            answer[i] = cnt;
            for (int j = ql[i]; j <= b * bucket; j++) {
                del(C[j]);
            }
            cnt = prv_cnt; root = prv_root;
        }
        while (r > b * bucket) {
            del(C[r--]);
        }
        cnt = root = 0;
    }
    for (int i = 1; i <= Q; i++) {
        cout << answer[i] << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Correct 2 ms 9304 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 2 ms 9304 KB Output is correct
6 Correct 2 ms 9308 KB Output is correct
7 Correct 3 ms 9308 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 4 ms 9308 KB Output is correct
10 Correct 3 ms 9308 KB Output is correct
11 Correct 3 ms 9356 KB Output is correct
12 Correct 2 ms 9304 KB Output is correct
13 Correct 2 ms 9308 KB Output is correct
14 Correct 2 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 3 ms 9308 KB Output is correct
17 Correct 3 ms 9308 KB Output is correct
18 Correct 3 ms 9412 KB Output is correct
19 Correct 3 ms 9308 KB Output is correct
20 Correct 3 ms 9308 KB Output is correct
21 Correct 3 ms 9308 KB Output is correct
22 Correct 3 ms 9308 KB Output is correct
23 Correct 3 ms 9308 KB Output is correct
24 Correct 3 ms 9308 KB Output is correct
25 Correct 3 ms 9308 KB Output is correct
26 Correct 3 ms 9308 KB Output is correct
27 Correct 2 ms 9304 KB Output is correct
28 Correct 2 ms 9308 KB Output is correct
29 Correct 2 ms 9308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Correct 2 ms 9304 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 2 ms 9304 KB Output is correct
6 Correct 2 ms 9308 KB Output is correct
7 Correct 3 ms 9308 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 4 ms 9308 KB Output is correct
10 Correct 3 ms 9308 KB Output is correct
11 Correct 3 ms 9356 KB Output is correct
12 Correct 2 ms 9304 KB Output is correct
13 Correct 2 ms 9308 KB Output is correct
14 Correct 2 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 3 ms 9308 KB Output is correct
17 Correct 3 ms 9308 KB Output is correct
18 Correct 3 ms 9412 KB Output is correct
19 Correct 3 ms 9308 KB Output is correct
20 Correct 3 ms 9308 KB Output is correct
21 Correct 3 ms 9308 KB Output is correct
22 Correct 3 ms 9308 KB Output is correct
23 Correct 3 ms 9308 KB Output is correct
24 Correct 3 ms 9308 KB Output is correct
25 Correct 3 ms 9308 KB Output is correct
26 Correct 3 ms 9308 KB Output is correct
27 Correct 2 ms 9304 KB Output is correct
28 Correct 2 ms 9308 KB Output is correct
29 Correct 2 ms 9308 KB Output is correct
30 Correct 13 ms 9560 KB Output is correct
31 Correct 15 ms 9564 KB Output is correct
32 Correct 20 ms 9564 KB Output is correct
33 Correct 20 ms 9612 KB Output is correct
34 Correct 20 ms 9436 KB Output is correct
35 Correct 6 ms 9564 KB Output is correct
36 Correct 6 ms 9496 KB Output is correct
37 Correct 6 ms 9612 KB Output is correct
38 Correct 16 ms 9564 KB Output is correct
39 Correct 16 ms 9564 KB Output is correct
40 Correct 16 ms 9560 KB Output is correct
41 Correct 6 ms 9564 KB Output is correct
42 Correct 8 ms 9820 KB Output is correct
43 Correct 6 ms 9584 KB Output is correct
44 Correct 20 ms 9560 KB Output is correct
45 Correct 18 ms 9644 KB Output is correct
46 Correct 17 ms 9652 KB Output is correct
47 Correct 6 ms 9564 KB Output is correct
48 Correct 5 ms 9628 KB Output is correct
49 Correct 6 ms 9564 KB Output is correct
50 Correct 16 ms 9564 KB Output is correct
51 Correct 16 ms 9564 KB Output is correct
52 Correct 17 ms 9544 KB Output is correct
53 Correct 17 ms 9564 KB Output is correct
54 Correct 17 ms 9620 KB Output is correct
55 Correct 17 ms 9564 KB Output is correct
56 Correct 3 ms 9308 KB Output is correct
57 Correct 3 ms 9304 KB Output is correct
58 Correct 3 ms 9304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 3 ms 9308 KB Output is correct
3 Correct 3 ms 9424 KB Output is correct
4 Execution timed out 5045 ms 23604 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9308 KB Output is correct
2 Correct 38 ms 16212 KB Output is correct
3 Correct 53 ms 16724 KB Output is correct
4 Correct 46 ms 17224 KB Output is correct
5 Correct 80 ms 23380 KB Output is correct
6 Correct 81 ms 21844 KB Output is correct
7 Correct 85 ms 20820 KB Output is correct
8 Correct 81 ms 20552 KB Output is correct
9 Correct 93 ms 20540 KB Output is correct
10 Correct 77 ms 20304 KB Output is correct
11 Correct 75 ms 20308 KB Output is correct
12 Correct 83 ms 20560 KB Output is correct
13 Correct 67 ms 20636 KB Output is correct
14 Correct 64 ms 20820 KB Output is correct
15 Correct 57 ms 21848 KB Output is correct
16 Correct 73 ms 22356 KB Output is correct
17 Correct 88 ms 22388 KB Output is correct
18 Correct 73 ms 22356 KB Output is correct
19 Correct 70 ms 23296 KB Output is correct
20 Correct 73 ms 22352 KB Output is correct
21 Correct 79 ms 21328 KB Output is correct
22 Correct 79 ms 20820 KB Output is correct
23 Correct 78 ms 20572 KB Output is correct
24 Correct 90 ms 20560 KB Output is correct
25 Correct 84 ms 20396 KB Output is correct
26 Correct 98 ms 20560 KB Output is correct
27 Correct 90 ms 20460 KB Output is correct
28 Correct 92 ms 20416 KB Output is correct
29 Correct 90 ms 20308 KB Output is correct
30 Correct 89 ms 20564 KB Output is correct
31 Correct 104 ms 21136 KB Output is correct
32 Correct 83 ms 20820 KB Output is correct
33 Correct 69 ms 21328 KB Output is correct
34 Correct 56 ms 21836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Correct 2 ms 9308 KB Output is correct
3 Correct 3 ms 9304 KB Output is correct
4 Execution timed out 5039 ms 20564 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Correct 2 ms 9304 KB Output is correct
3 Correct 2 ms 9308 KB Output is correct
4 Correct 2 ms 9308 KB Output is correct
5 Correct 2 ms 9304 KB Output is correct
6 Correct 2 ms 9308 KB Output is correct
7 Correct 3 ms 9308 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 4 ms 9308 KB Output is correct
10 Correct 3 ms 9308 KB Output is correct
11 Correct 3 ms 9356 KB Output is correct
12 Correct 2 ms 9304 KB Output is correct
13 Correct 2 ms 9308 KB Output is correct
14 Correct 2 ms 9308 KB Output is correct
15 Correct 3 ms 9308 KB Output is correct
16 Correct 3 ms 9308 KB Output is correct
17 Correct 3 ms 9308 KB Output is correct
18 Correct 3 ms 9412 KB Output is correct
19 Correct 3 ms 9308 KB Output is correct
20 Correct 3 ms 9308 KB Output is correct
21 Correct 3 ms 9308 KB Output is correct
22 Correct 3 ms 9308 KB Output is correct
23 Correct 3 ms 9308 KB Output is correct
24 Correct 3 ms 9308 KB Output is correct
25 Correct 3 ms 9308 KB Output is correct
26 Correct 3 ms 9308 KB Output is correct
27 Correct 2 ms 9304 KB Output is correct
28 Correct 2 ms 9308 KB Output is correct
29 Correct 2 ms 9308 KB Output is correct
30 Correct 13 ms 9560 KB Output is correct
31 Correct 15 ms 9564 KB Output is correct
32 Correct 20 ms 9564 KB Output is correct
33 Correct 20 ms 9612 KB Output is correct
34 Correct 20 ms 9436 KB Output is correct
35 Correct 6 ms 9564 KB Output is correct
36 Correct 6 ms 9496 KB Output is correct
37 Correct 6 ms 9612 KB Output is correct
38 Correct 16 ms 9564 KB Output is correct
39 Correct 16 ms 9564 KB Output is correct
40 Correct 16 ms 9560 KB Output is correct
41 Correct 6 ms 9564 KB Output is correct
42 Correct 8 ms 9820 KB Output is correct
43 Correct 6 ms 9584 KB Output is correct
44 Correct 20 ms 9560 KB Output is correct
45 Correct 18 ms 9644 KB Output is correct
46 Correct 17 ms 9652 KB Output is correct
47 Correct 6 ms 9564 KB Output is correct
48 Correct 5 ms 9628 KB Output is correct
49 Correct 6 ms 9564 KB Output is correct
50 Correct 16 ms 9564 KB Output is correct
51 Correct 16 ms 9564 KB Output is correct
52 Correct 17 ms 9544 KB Output is correct
53 Correct 17 ms 9564 KB Output is correct
54 Correct 17 ms 9620 KB Output is correct
55 Correct 17 ms 9564 KB Output is correct
56 Correct 3 ms 9308 KB Output is correct
57 Correct 3 ms 9304 KB Output is correct
58 Correct 3 ms 9304 KB Output is correct
59 Correct 2 ms 9308 KB Output is correct
60 Correct 3 ms 9308 KB Output is correct
61 Correct 3 ms 9424 KB Output is correct
62 Execution timed out 5045 ms 23604 KB Time limit exceeded
63 Halted 0 ms 0 KB -