답안 #939772

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

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int BUFFER_SIZE = 65536;

char buffer[BUFFER_SIZE];
int buf_pos = BUFFER_SIZE - 1;

char read_char () {
    if (++buf_pos == BUFFER_SIZE) {
        fread(buffer, BUFFER_SIZE, sizeof(char), stdin);
        buf_pos = 0;
    }
    return buffer[buf_pos];
}
int read_int () {
    char c;
    while (!isdigit(c = read_char()));
    int val = 0;
    do {
        val = val * 10 + (int) (c - '0');
    } while (isdigit(c = read_char()));
    return val;
}

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

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 euler[N_MAX * 2 + 2];
int ecurr;

void dfs (int u) {
    euler[++ecurr] = 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);
            euler[++ecurr] = u;
        }
    }
    R[u] = curr;
}

int p2[N_MAX * 2 + 2];

int range_lca[N_MAX * 2 + 2][LOG_N];
int arg_min_depth (const int &u, const int &v) {
    return (depth[u] < depth[v] ? u : v);
}

int euler_L[N_MAX + 2], euler_R[N_MAX + 2];
void precalc () {
    for (int i = 1; i <= N * 2; i++) {
        range_lca[i][0] = euler[i];
        if (euler_L[euler[i]] == 0) {
            euler_L[euler[i]] = i;
        }
        euler_R[euler[i]] = i;
    }
    for (int e = 1; e < LOG_N; e++) {
        for (int i = 1; i + (1 << e) - 1 <= N * 2; i++) {
            range_lca[i][e] = arg_min_depth(range_lca[i][e - 1],
                range_lca[i + (1 << (e - 1))][e - 1]);
        }
    }
}
int lca (int u, int v) {
    if (u == 0 || v == 0) {
        return 0;
    }
    if (L[u] > L[v]) {
        swap(u, v);
    }
    int l = euler_L[u], r = euler_R[v];
    int e = p2[r - l + 1];
    return arg_min_depth(range_lca[l][e], range_lca[r - (1 << e) + 1][e]);
}

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

int cnt, root;

int occ[N_MAX + 2];
set <int> S;
void add (int u) {
    if (++occ[u] == 1) {
        if (S.empty() == true) {
            S.insert(L[u]);
            cnt = 1; root = u;
            return;
        }
        set <int> :: iterator it = S.insert(L[u]).first;
        int ur = (next(it) != S.end() ? order[*next(it)] : 0);
        if (ur == 0 || R[u] < L[ur]) {
            int ul = (it != S.begin() ? order[*prev(it)] : 0);
            ul = lca(u, ul);
            ur = lca(u, ur);
            int v = (depth[ul] > depth[ur] ? ul : ur);
            cnt += depth[u] - depth[v];
            if (depth[v] < depth[root]) {
                cnt += depth[root] - depth[v];
                root = v;
            }
        } else if (depth[u] < depth[root]) {
            cnt += depth[root] - depth[u];
            root = u;
        }
    }
}
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);
//    mt19937 rnd(0);

    N = read_int();
    M = read_int();
    Q = read_int();
//    N = M = Q = 100000;
    for (int i = 1; i <= N - 1; i++) {
        int u, v;
        u = read_int();
        v = read_int();
//        u = rnd() % i + 1;
//        v = i + 1;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= M; i++) {
        C[i] = read_int();
//        C[i] = i;
    }
//    shuffle(C + 1, C + M + 1, rnd);
    for (int i = 1; i <= Q; i++) {
        ql[i] = read_int();
        qr[i] = read_int();
//        ql[i] = rnd() % M + 1;
//        qr[i] = rnd() % M + 1;
        if (ql[i] > qr[i]) {
            swap(ql[i], qr[i]);
        }
    }
    for (int i = 1; i <= N * 2; i++) {
        p2[i] = p2[i - 1];
        if ((1 << (p2[i] + 1)) <= i) {
            p2[i]++;
        }
    }
    depth[1] = 1;
    dfs(1);
    precalc();
    bucket = max(1, min(M, (int) sqrt(M)));
    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) {
            occ[C[r--]]--;
        }
        cnt = root = 0;
        S.clear();
    }
    for (int i = 1; i <= Q; i++) {
        cout << answer[i] << "\n";
    }

    return 0;
}

Compilation message

tourism.cpp: In function 'char read_char()':
tourism.cpp:22:14: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         fread(buffer, BUFFER_SIZE, sizeof(char), stdin);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 3 ms 8892 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 3 ms 8796 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8892 KB Output is correct
22 Correct 2 ms 8868 KB Output is correct
23 Correct 3 ms 8792 KB Output is correct
24 Correct 3 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8792 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 3 ms 8892 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 3 ms 8796 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8892 KB Output is correct
22 Correct 2 ms 8868 KB Output is correct
23 Correct 3 ms 8792 KB Output is correct
24 Correct 3 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8792 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
30 Correct 8 ms 9052 KB Output is correct
31 Correct 10 ms 9052 KB Output is correct
32 Correct 11 ms 9052 KB Output is correct
33 Correct 12 ms 9052 KB Output is correct
34 Correct 15 ms 9052 KB Output is correct
35 Correct 4 ms 9052 KB Output is correct
36 Correct 6 ms 9232 KB Output is correct
37 Correct 5 ms 9304 KB Output is correct
38 Correct 10 ms 9308 KB Output is correct
39 Correct 10 ms 9332 KB Output is correct
40 Correct 10 ms 9304 KB Output is correct
41 Correct 4 ms 9308 KB Output is correct
42 Correct 4 ms 9308 KB Output is correct
43 Correct 4 ms 9312 KB Output is correct
44 Correct 11 ms 9052 KB Output is correct
45 Correct 10 ms 9052 KB Output is correct
46 Correct 10 ms 9052 KB Output is correct
47 Correct 4 ms 9304 KB Output is correct
48 Correct 4 ms 9052 KB Output is correct
49 Correct 4 ms 9236 KB Output is correct
50 Correct 14 ms 9252 KB Output is correct
51 Correct 11 ms 9052 KB Output is correct
52 Correct 11 ms 9228 KB Output is correct
53 Correct 11 ms 9304 KB Output is correct
54 Correct 12 ms 9144 KB Output is correct
55 Correct 11 ms 9032 KB Output is correct
56 Correct 2 ms 8792 KB Output is correct
57 Correct 2 ms 9052 KB Output is correct
58 Correct 3 ms 9308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 3 ms 8796 KB Output is correct
4 Correct 3283 ms 28628 KB Output is correct
5 Correct 2388 ms 35604 KB Output is correct
6 Correct 3314 ms 38224 KB Output is correct
7 Execution timed out 5035 ms 41044 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 26 ms 21340 KB Output is correct
3 Correct 34 ms 21844 KB Output is correct
4 Correct 33 ms 24412 KB Output is correct
5 Correct 62 ms 33108 KB Output is correct
6 Correct 64 ms 31316 KB Output is correct
7 Correct 58 ms 30548 KB Output is correct
8 Correct 68 ms 30320 KB Output is correct
9 Correct 56 ms 30036 KB Output is correct
10 Correct 63 ms 30184 KB Output is correct
11 Correct 65 ms 30032 KB Output is correct
12 Correct 67 ms 30324 KB Output is correct
13 Correct 55 ms 30416 KB Output is correct
14 Correct 48 ms 30772 KB Output is correct
15 Correct 41 ms 31316 KB Output is correct
16 Correct 56 ms 32080 KB Output is correct
17 Correct 69 ms 31964 KB Output is correct
18 Correct 56 ms 32076 KB Output is correct
19 Correct 50 ms 33104 KB Output is correct
20 Correct 55 ms 31808 KB Output is correct
21 Correct 52 ms 30804 KB Output is correct
22 Correct 50 ms 30548 KB Output is correct
23 Correct 49 ms 30288 KB Output is correct
24 Correct 48 ms 30032 KB Output is correct
25 Correct 49 ms 30032 KB Output is correct
26 Correct 52 ms 30224 KB Output is correct
27 Correct 56 ms 30032 KB Output is correct
28 Correct 62 ms 30152 KB Output is correct
29 Correct 53 ms 30044 KB Output is correct
30 Correct 51 ms 30032 KB Output is correct
31 Correct 49 ms 30456 KB Output is correct
32 Correct 58 ms 30548 KB Output is correct
33 Correct 48 ms 30800 KB Output is correct
34 Correct 41 ms 31576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 4821 ms 25892 KB Output is correct
5 Correct 4696 ms 26052 KB Output is correct
6 Execution timed out 5025 ms 32168 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 3 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8796 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 2 ms 8796 KB Output is correct
12 Correct 3 ms 8892 KB Output is correct
13 Correct 3 ms 8796 KB Output is correct
14 Correct 2 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 3 ms 8796 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8796 KB Output is correct
21 Correct 2 ms 8892 KB Output is correct
22 Correct 2 ms 8868 KB Output is correct
23 Correct 3 ms 8792 KB Output is correct
24 Correct 3 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8792 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
30 Correct 8 ms 9052 KB Output is correct
31 Correct 10 ms 9052 KB Output is correct
32 Correct 11 ms 9052 KB Output is correct
33 Correct 12 ms 9052 KB Output is correct
34 Correct 15 ms 9052 KB Output is correct
35 Correct 4 ms 9052 KB Output is correct
36 Correct 6 ms 9232 KB Output is correct
37 Correct 5 ms 9304 KB Output is correct
38 Correct 10 ms 9308 KB Output is correct
39 Correct 10 ms 9332 KB Output is correct
40 Correct 10 ms 9304 KB Output is correct
41 Correct 4 ms 9308 KB Output is correct
42 Correct 4 ms 9308 KB Output is correct
43 Correct 4 ms 9312 KB Output is correct
44 Correct 11 ms 9052 KB Output is correct
45 Correct 10 ms 9052 KB Output is correct
46 Correct 10 ms 9052 KB Output is correct
47 Correct 4 ms 9304 KB Output is correct
48 Correct 4 ms 9052 KB Output is correct
49 Correct 4 ms 9236 KB Output is correct
50 Correct 14 ms 9252 KB Output is correct
51 Correct 11 ms 9052 KB Output is correct
52 Correct 11 ms 9228 KB Output is correct
53 Correct 11 ms 9304 KB Output is correct
54 Correct 12 ms 9144 KB Output is correct
55 Correct 11 ms 9032 KB Output is correct
56 Correct 2 ms 8792 KB Output is correct
57 Correct 2 ms 9052 KB Output is correct
58 Correct 3 ms 9308 KB Output is correct
59 Correct 2 ms 8796 KB Output is correct
60 Correct 2 ms 8796 KB Output is correct
61 Correct 3 ms 8796 KB Output is correct
62 Correct 3283 ms 28628 KB Output is correct
63 Correct 2388 ms 35604 KB Output is correct
64 Correct 3314 ms 38224 KB Output is correct
65 Execution timed out 5035 ms 41044 KB Time limit exceeded
66 Halted 0 ms 0 KB -