Submission #931109

# Submission time Handle Problem Language Result Execution time Memory
931109 2024-02-21T09:00:28 Z alextodoran Two Currencies (JOI23_currencies) C++17
100 / 100
2234 ms 34552 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 SILVER_MAX = 1000000000;
const int LOG2_N = 17;

int N, M, Q;
int edge_u[N_MAX + 2], edge_v[N_MAX + 2];
vector <int> adj[N_MAX + 2];
int cpt_edge[M_MAX + 2], cpt_silver[M_MAX + 2];
int qry_start[Q_MAX + 2], qry_finish[Q_MAX + 2];
int qry_gold[Q_MAX + 2]; ll qry_silver[Q_MAX + 2];

int parent[N_MAX + 2], depth[N_MAX + 2];;
int label[N_MAX + 2], max_label[N_MAX + 2];
int curr_label;

void dfs (int u) {
    label[u] = ++curr_label;
    for (int v : adj[u]) {
        if (v != parent[u]) {
            parent[v] = u;
            depth[v] = depth[u] + 1;
            dfs(v);
        }
    }
    max_label[u] = curr_label;
}

int ancestor[N_MAX + 2][LOG2_N];

int find_ancestor (int u, int k) {
    for (int t = 0; t < LOG2_N; t++) {
        if ((k >> t) & 1) {
            u = ancestor[u][t];
        }
    }
    return u;
}

int find_lca (int u, int v) {
    if (depth[u] > depth[v]) {
        u = find_ancestor(u, depth[u] - depth[v]);
    }
    if (depth[v] > depth[u]) {
        v = find_ancestor(v, depth[v] - depth[u]);
    }
    if (u == v) {
        return u;
    }
    for (int t = LOG2_N - 1; t >= 0; t--) {
        if (ancestor[u][t] != ancestor[v][t]) {
            u = ancestor[u][t];
            v = ancestor[v][t];
        }
    }
    return parent[u];
}

int lower[Q_MAX + 2], higher[Q_MAX + 2];
int mid[Q_MAX + 2]; ll sum[Q_MAX + 2]; int cnt[Q_MAX + 2];

int cpt_order[M_MAX + 2];
int qry_order[Q_MAX + 2];

ll Fenwick_sum[N_MAX + 2]; int Fenwick_cnt[N_MAX + 2];

void update (int pos, int val) {
    for (int i = pos; i <= N; i += i & -i) {
        Fenwick_sum[i] += val;
        Fenwick_cnt[i] += (val > 0 ? +1 : -1);
    }
}
void update (int l, int r, int val) {
    update(l, +val); update(r + 1, -val);
}
pair <ll, int> query (int pos) {
    ll sum = 0; int cnt = 0;
    for (int i = pos; i >= 1; i -= i & -i) {
        sum += Fenwick_sum[i];
        cnt += Fenwick_cnt[i];
    }
    return make_pair(sum, cnt);
}

void step () {
    iota(cpt_order + 1, cpt_order + M + 1, 1);
    iota(qry_order + 1, qry_order + Q + 1, 1);
    sort(cpt_order + 1, cpt_order + M + 1, [&] (const int &i, const int &j) {
        return cpt_silver[i] < cpt_silver[j];
    });
    sort(qry_order + 1, qry_order + Q + 1, [&] (const int &i, const int &j) {
        return mid[i] < mid[j];
    });
    fill(Fenwick_sum + 1, Fenwick_sum + N + 1, 0);
    fill(Fenwick_cnt + 1, Fenwick_cnt + N + 1, 0);
    for (int i = 1, j = 0; i <= Q && mid[qry_order[i]] != INT_MAX; i++) {
        while (j + 1 <= M && cpt_silver[cpt_order[j + 1]] <= mid[qry_order[i]]) {
            int e = cpt_edge[cpt_order[++j]];
            update(label[edge_v[e]], max_label[edge_v[e]], cpt_silver[cpt_order[j]]);
        }
        int u = qry_start[qry_order[i]], v = qry_finish[qry_order[i]];
        int lca = find_lca(u, v);
        ll sum_u; int cnt_u; tie(sum_u, cnt_u) = query(label[u]);
        ll sum_v; int cnt_v; tie(sum_v, cnt_v) = query(label[v]);
        ll sum_lca; int cnt_lca; tie(sum_lca, cnt_lca) = query(label[lca]);
        sum[qry_order[i]] = sum_u + sum_v - 2 * sum_lca;
        cnt[qry_order[i]] = cnt_u + cnt_v - 2 * cnt_lca;
    }
}

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

    cin >> N >> M >> Q;
    for (int i = 1; i <= N - 1; i++) {
        cin >> edge_u[i] >> edge_v[i];
        adj[edge_u[i]].push_back(edge_v[i]);
        adj[edge_v[i]].push_back(edge_u[i]);
    }
    dfs(1);
    for (int u = 1; u <= N; u++) {
        ancestor[u][0] = parent[u];
    }
    for (int t = 1; t < LOG2_N; t++) {
        for (int u = 1; u <= N; u++) {
            ancestor[u][t] = ancestor[ancestor[u][t - 1]][t - 1];
        }
    }
    for (int i = 1; i <= N - 1; i++) {
        if (edge_u[i] != parent[edge_v[i]]) {
            swap(edge_u[i], edge_v[i]);
        }
    }
    for (int i = 1; i <= M; i++) {
        cin >> cpt_edge[i] >> cpt_silver[i];
    }
    for (int i = 1; i <= Q; i++) {
        cin >> qry_start[i] >> qry_finish[i];
        cin >> qry_gold[i] >> qry_silver[i];
        lower[i] = 0; higher[i] = SILVER_MAX;
    }
    while (true) {
        bool done = true;
        for (int i = 1; i <= Q; i++) {
            if (lower[i] < higher[i]) {
                done = false;
                mid[i] = (lower[i] + higher[i] + 1) / 2;
            } else {
                mid[i] = INT_MAX; // done
            }
        }
        if (done == true) {
            break;
        }
        step();
        for (int i = 1; i <= Q; i++) {
            if (mid[i] != INT_MAX) {
                if (sum[i] <= qry_silver[i]) {
                    lower[i] = mid[i];
                } else {
                    higher[i] = mid[i] - 1;
                }
            }
        }
    }
    for (int i = 1; i <= Q; i++) {
        mid[i] = lower[i];
    }
    step();
    fill(Fenwick_sum + 1, Fenwick_sum + N + 1, 0);
    fill(Fenwick_cnt + 1, Fenwick_cnt + N + 1, 0);
    for (int i = 1; i <= M; i++) {
        int e = cpt_edge[i];
        update(label[edge_v[e]], max_label[edge_v[e]], cpt_silver[i]);
    }
    for (int i = 1; i <= Q; i++) {
        int u = qry_start[i], v = qry_finish[i];
        int lca = find_lca(u, v);
        int total_cpt = query(label[u]).second + query(label[v]).second - 2 * query(label[lca]).second;
        int with_silver = cnt[i];
        if (with_silver < total_cpt) {
            with_silver += min((ll) (total_cpt - with_silver), (qry_silver[i] - sum[i]) / (mid[i] + 1));
        }
        int with_gold = total_cpt - with_silver;
        cout << (with_gold <= qry_gold[i] ? qry_gold[i] - with_gold : -1) << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11608 KB Output is correct
2 Correct 3 ms 11608 KB Output is correct
3 Correct 2 ms 11612 KB Output is correct
4 Correct 2 ms 11612 KB Output is correct
5 Correct 14 ms 11948 KB Output is correct
6 Correct 18 ms 11864 KB Output is correct
7 Correct 16 ms 11864 KB Output is correct
8 Correct 19 ms 11868 KB Output is correct
9 Correct 18 ms 11980 KB Output is correct
10 Correct 18 ms 11980 KB Output is correct
11 Correct 18 ms 11868 KB Output is correct
12 Correct 18 ms 11868 KB Output is correct
13 Correct 19 ms 12068 KB Output is correct
14 Correct 20 ms 11868 KB Output is correct
15 Correct 21 ms 11868 KB Output is correct
16 Correct 24 ms 11868 KB Output is correct
17 Correct 21 ms 11980 KB Output is correct
18 Correct 21 ms 11864 KB Output is correct
19 Correct 15 ms 11896 KB Output is correct
20 Correct 14 ms 11868 KB Output is correct
21 Correct 14 ms 11864 KB Output is correct
22 Correct 14 ms 11864 KB Output is correct
23 Correct 17 ms 11984 KB Output is correct
24 Correct 17 ms 12064 KB Output is correct
25 Correct 17 ms 11868 KB Output is correct
26 Correct 14 ms 11868 KB Output is correct
27 Correct 13 ms 11864 KB Output is correct
28 Correct 14 ms 11868 KB Output is correct
29 Correct 14 ms 11964 KB Output is correct
30 Correct 13 ms 11868 KB Output is correct
31 Correct 13 ms 11864 KB Output is correct
32 Correct 14 ms 12124 KB Output is correct
33 Correct 17 ms 11868 KB Output is correct
34 Correct 17 ms 11868 KB Output is correct
35 Correct 18 ms 12112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11612 KB Output is correct
2 Correct 14 ms 11976 KB Output is correct
3 Correct 13 ms 11868 KB Output is correct
4 Correct 13 ms 11868 KB Output is correct
5 Correct 799 ms 26708 KB Output is correct
6 Correct 768 ms 25200 KB Output is correct
7 Correct 791 ms 25508 KB Output is correct
8 Correct 607 ms 25028 KB Output is correct
9 Correct 588 ms 24852 KB Output is correct
10 Correct 973 ms 27496 KB Output is correct
11 Correct 892 ms 27360 KB Output is correct
12 Correct 929 ms 27728 KB Output is correct
13 Correct 918 ms 27984 KB Output is correct
14 Correct 956 ms 27732 KB Output is correct
15 Correct 1109 ms 33752 KB Output is correct
16 Correct 1059 ms 34552 KB Output is correct
17 Correct 1124 ms 33408 KB Output is correct
18 Correct 1333 ms 27988 KB Output is correct
19 Correct 1247 ms 27880 KB Output is correct
20 Correct 1304 ms 27860 KB Output is correct
21 Correct 836 ms 26928 KB Output is correct
22 Correct 769 ms 27116 KB Output is correct
23 Correct 748 ms 27104 KB Output is correct
24 Correct 729 ms 27100 KB Output is correct
25 Correct 835 ms 27808 KB Output is correct
26 Correct 822 ms 27820 KB Output is correct
27 Correct 776 ms 27804 KB Output is correct
28 Correct 580 ms 27332 KB Output is correct
29 Correct 557 ms 27220 KB Output is correct
30 Correct 635 ms 27520 KB Output is correct
31 Correct 586 ms 27520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11612 KB Output is correct
2 Correct 17 ms 12100 KB Output is correct
3 Correct 18 ms 11868 KB Output is correct
4 Correct 18 ms 11868 KB Output is correct
5 Correct 877 ms 32008 KB Output is correct
6 Correct 810 ms 31316 KB Output is correct
7 Correct 1064 ms 25936 KB Output is correct
8 Correct 1423 ms 34224 KB Output is correct
9 Correct 1455 ms 34228 KB Output is correct
10 Correct 1348 ms 34304 KB Output is correct
11 Correct 1248 ms 34304 KB Output is correct
12 Correct 1183 ms 34304 KB Output is correct
13 Correct 1233 ms 34252 KB Output is correct
14 Correct 801 ms 34132 KB Output is correct
15 Correct 795 ms 34172 KB Output is correct
16 Correct 985 ms 34436 KB Output is correct
17 Correct 926 ms 34388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11608 KB Output is correct
2 Correct 3 ms 11608 KB Output is correct
3 Correct 2 ms 11612 KB Output is correct
4 Correct 2 ms 11612 KB Output is correct
5 Correct 14 ms 11948 KB Output is correct
6 Correct 18 ms 11864 KB Output is correct
7 Correct 16 ms 11864 KB Output is correct
8 Correct 19 ms 11868 KB Output is correct
9 Correct 18 ms 11980 KB Output is correct
10 Correct 18 ms 11980 KB Output is correct
11 Correct 18 ms 11868 KB Output is correct
12 Correct 18 ms 11868 KB Output is correct
13 Correct 19 ms 12068 KB Output is correct
14 Correct 20 ms 11868 KB Output is correct
15 Correct 21 ms 11868 KB Output is correct
16 Correct 24 ms 11868 KB Output is correct
17 Correct 21 ms 11980 KB Output is correct
18 Correct 21 ms 11864 KB Output is correct
19 Correct 15 ms 11896 KB Output is correct
20 Correct 14 ms 11868 KB Output is correct
21 Correct 14 ms 11864 KB Output is correct
22 Correct 14 ms 11864 KB Output is correct
23 Correct 17 ms 11984 KB Output is correct
24 Correct 17 ms 12064 KB Output is correct
25 Correct 17 ms 11868 KB Output is correct
26 Correct 14 ms 11868 KB Output is correct
27 Correct 13 ms 11864 KB Output is correct
28 Correct 14 ms 11868 KB Output is correct
29 Correct 14 ms 11964 KB Output is correct
30 Correct 13 ms 11868 KB Output is correct
31 Correct 13 ms 11864 KB Output is correct
32 Correct 14 ms 12124 KB Output is correct
33 Correct 17 ms 11868 KB Output is correct
34 Correct 17 ms 11868 KB Output is correct
35 Correct 18 ms 12112 KB Output is correct
36 Correct 3 ms 11612 KB Output is correct
37 Correct 14 ms 11976 KB Output is correct
38 Correct 13 ms 11868 KB Output is correct
39 Correct 13 ms 11868 KB Output is correct
40 Correct 799 ms 26708 KB Output is correct
41 Correct 768 ms 25200 KB Output is correct
42 Correct 791 ms 25508 KB Output is correct
43 Correct 607 ms 25028 KB Output is correct
44 Correct 588 ms 24852 KB Output is correct
45 Correct 973 ms 27496 KB Output is correct
46 Correct 892 ms 27360 KB Output is correct
47 Correct 929 ms 27728 KB Output is correct
48 Correct 918 ms 27984 KB Output is correct
49 Correct 956 ms 27732 KB Output is correct
50 Correct 1109 ms 33752 KB Output is correct
51 Correct 1059 ms 34552 KB Output is correct
52 Correct 1124 ms 33408 KB Output is correct
53 Correct 1333 ms 27988 KB Output is correct
54 Correct 1247 ms 27880 KB Output is correct
55 Correct 1304 ms 27860 KB Output is correct
56 Correct 836 ms 26928 KB Output is correct
57 Correct 769 ms 27116 KB Output is correct
58 Correct 748 ms 27104 KB Output is correct
59 Correct 729 ms 27100 KB Output is correct
60 Correct 835 ms 27808 KB Output is correct
61 Correct 822 ms 27820 KB Output is correct
62 Correct 776 ms 27804 KB Output is correct
63 Correct 580 ms 27332 KB Output is correct
64 Correct 557 ms 27220 KB Output is correct
65 Correct 635 ms 27520 KB Output is correct
66 Correct 586 ms 27520 KB Output is correct
67 Correct 3 ms 11612 KB Output is correct
68 Correct 17 ms 12100 KB Output is correct
69 Correct 18 ms 11868 KB Output is correct
70 Correct 18 ms 11868 KB Output is correct
71 Correct 877 ms 32008 KB Output is correct
72 Correct 810 ms 31316 KB Output is correct
73 Correct 1064 ms 25936 KB Output is correct
74 Correct 1423 ms 34224 KB Output is correct
75 Correct 1455 ms 34228 KB Output is correct
76 Correct 1348 ms 34304 KB Output is correct
77 Correct 1248 ms 34304 KB Output is correct
78 Correct 1183 ms 34304 KB Output is correct
79 Correct 1233 ms 34252 KB Output is correct
80 Correct 801 ms 34132 KB Output is correct
81 Correct 795 ms 34172 KB Output is correct
82 Correct 985 ms 34436 KB Output is correct
83 Correct 926 ms 34388 KB Output is correct
84 Correct 1016 ms 25196 KB Output is correct
85 Correct 960 ms 21392 KB Output is correct
86 Correct 797 ms 21076 KB Output is correct
87 Correct 1456 ms 27400 KB Output is correct
88 Correct 1411 ms 27456 KB Output is correct
89 Correct 1619 ms 27448 KB Output is correct
90 Correct 1442 ms 27468 KB Output is correct
91 Correct 1415 ms 27444 KB Output is correct
92 Correct 2001 ms 31852 KB Output is correct
93 Correct 1630 ms 33196 KB Output is correct
94 Correct 2234 ms 27832 KB Output is correct
95 Correct 1969 ms 27856 KB Output is correct
96 Correct 2115 ms 27828 KB Output is correct
97 Correct 1869 ms 27920 KB Output is correct
98 Correct 1321 ms 27080 KB Output is correct
99 Correct 1305 ms 26968 KB Output is correct
100 Correct 1210 ms 27080 KB Output is correct
101 Correct 1221 ms 27076 KB Output is correct
102 Correct 1275 ms 27776 KB Output is correct
103 Correct 1329 ms 27748 KB Output is correct
104 Correct 1309 ms 27744 KB Output is correct
105 Correct 925 ms 27532 KB Output is correct
106 Correct 858 ms 27488 KB Output is correct
107 Correct 936 ms 27408 KB Output is correct
108 Correct 902 ms 27412 KB Output is correct