Submission #759049

# Submission time Handle Problem Language Result Execution time Memory
759049 2023-06-15T17:33:35 Z Desh03 Two Currencies (JOI23_currencies) C++17
100 / 100
1372 ms 43588 KB
#include <bits/stdc++.h>
using namespace std;

struct fenwick {
    vector<pair<long long, int>> fenw;
    int n;
    fenwick(int n_) : n(n_) {
        fenw.resize(n, {0, 0});
    }
    void upd(int i, int d, int d2) {
        for (; i < n; i |= i + 1) fenw[i].first += d, fenw[i].second += d2;
    }
    pair<long long, int> qry(int i) {
        pair<long long, int> s = {0, 0};
        for (; i >= 0; i = (i & (i + 1)) - 1) s.first += fenw[i].first, s.second += fenw[i].second;
        return s;
    }
    void reset() {
        for (int i = 0; i < n; i++) fenw[i] = {0, 0};
    }
};

vector<vector<int>> g, up;
vector<int> in, out, dep;
int t;
const int lg = 17;

void dfs(int u) {
    in[u] = t++;
    for (int i = 1; i < lg; i++) up[i][u] = up[i - 1][up[i - 1][u]];
    for (int v : g[u])
        if (v ^ up[0][u])
            up[0][v] = u, dep[v] = dep[u] + 1, dfs(v);
    out[u] = t;
}

int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int k = dep[u] - dep[v];
    for (int i = 0; i < lg; i++)
        if (k >> i & 1)
            u = up[i][u];
    if (u == v) return u;
    for (int i = lg - 1; i >= 0; i--)
        if (up[i][u] ^ up[i][v])
            u = up[i][u], v = up[i][v];
    return up[0][u];
}

int main() {
    int n, m, q, c = 0;
    cin >> n >> m >> q;
    up = vector<vector<int>> (lg, vector<int> (n)), g.resize(n), in.resize(n), out.resize(n), dep.resize(n);
    vector<pair<int, int>> e(n - 1);
    for (auto &[a, b] : e) {
        cin >> a >> b;
        --a, --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0);
    fenwick fen(n);
    for (auto &[a, b] : e)
        if (b == up[0][a])
            swap(a, b);
    vector<pair<int, int>> ck(m);
    for (auto &[c, p] : ck) cin >> p >> c, --p;
    sort(ck.begin(), ck.end());
    vector<vector<int>> v(m);
    vector<tuple<int, int, int, long long>> qr(q);
    vector<int> l(q), r(q, m - 1), ans(q);
    for (int i = 0; i < q; i++) {
        v[(m - 1) >> 1].push_back(i);
        auto &[s, t, x, y] = qr[i];
        cin >> s >> t >> x >> y, --s, --t;
    }
    for (int itr = 0; itr < lg; itr++) {
        for (int i = 0; i < m; i++) {
            auto [c, p] = ck[i];
            int b = e[p].second;
            fen.upd(in[b], c, 1);
            fen.upd(out[b], -c, -1);
            for (int id : v[i]) {
                auto [s, t, x, y] = qr[id];
                auto [s1, s2, s3] = make_tuple(fen.qry(in[s]), fen.qry(in[t]), fen.qry(in[lca(s, t)]));
                if (s1.first + s2.first - (s3.first << 1) > y) r[id] = i - 1;
                else ans[id] = s1.second + s2.second - (s3.second << 1), l[id] = i + 1;
                int m = l[id] + r[id] >> 1;
                if (i ^ m && l[id] <= r[id]) v[m].push_back(id);
            }
            v[i].clear();
        }
        if (itr < lg - 1) fen.reset();
    }
    for (int i = 0; i < q; i++) {
        auto [s, t, x, y] = qr[i];
        ans[i] = fen.qry(in[s]).second + fen.qry(in[t]).second - (fen.qry(in[lca(s, t)]).second << 1) - ans[i];
        cout << max(x - ans[i], -1) << '\n';
    }
}

Compilation message

currencies.cpp: In function 'int main()':
currencies.cpp:88:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   88 |                 int m = l[id] + r[id] >> 1;
currencies.cpp:51:18: warning: unused variable 'c' [-Wunused-variable]
   51 |     int n, m, q, c = 0;
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 6 ms 724 KB Output is correct
6 Correct 8 ms 932 KB Output is correct
7 Correct 7 ms 724 KB Output is correct
8 Correct 9 ms 916 KB Output is correct
9 Correct 9 ms 980 KB Output is correct
10 Correct 8 ms 968 KB Output is correct
11 Correct 9 ms 852 KB Output is correct
12 Correct 9 ms 980 KB Output is correct
13 Correct 9 ms 1068 KB Output is correct
14 Correct 9 ms 980 KB Output is correct
15 Correct 9 ms 948 KB Output is correct
16 Correct 9 ms 1012 KB Output is correct
17 Correct 10 ms 980 KB Output is correct
18 Correct 12 ms 980 KB Output is correct
19 Correct 8 ms 980 KB Output is correct
20 Correct 7 ms 952 KB Output is correct
21 Correct 7 ms 980 KB Output is correct
22 Correct 7 ms 980 KB Output is correct
23 Correct 8 ms 884 KB Output is correct
24 Correct 8 ms 980 KB Output is correct
25 Correct 8 ms 912 KB Output is correct
26 Correct 6 ms 852 KB Output is correct
27 Correct 7 ms 840 KB Output is correct
28 Correct 7 ms 852 KB Output is correct
29 Correct 6 ms 808 KB Output is correct
30 Correct 8 ms 912 KB Output is correct
31 Correct 9 ms 980 KB Output is correct
32 Correct 8 ms 924 KB Output is correct
33 Correct 8 ms 980 KB Output is correct
34 Correct 8 ms 980 KB Output is correct
35 Correct 8 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 10 ms 976 KB Output is correct
3 Correct 9 ms 980 KB Output is correct
4 Correct 8 ms 980 KB Output is correct
5 Correct 693 ms 31408 KB Output is correct
6 Correct 675 ms 31092 KB Output is correct
7 Correct 786 ms 31628 KB Output is correct
8 Correct 652 ms 28568 KB Output is correct
9 Correct 617 ms 27772 KB Output is correct
10 Correct 954 ms 37936 KB Output is correct
11 Correct 1004 ms 38000 KB Output is correct
12 Correct 971 ms 37836 KB Output is correct
13 Correct 1012 ms 37808 KB Output is correct
14 Correct 942 ms 38012 KB Output is correct
15 Correct 1047 ms 43004 KB Output is correct
16 Correct 1006 ms 43356 KB Output is correct
17 Correct 1190 ms 42752 KB Output is correct
18 Correct 1187 ms 38428 KB Output is correct
19 Correct 1259 ms 38600 KB Output is correct
20 Correct 1372 ms 38472 KB Output is correct
21 Correct 812 ms 38068 KB Output is correct
22 Correct 890 ms 38300 KB Output is correct
23 Correct 896 ms 38404 KB Output is correct
24 Correct 791 ms 38340 KB Output is correct
25 Correct 859 ms 37952 KB Output is correct
26 Correct 816 ms 37944 KB Output is correct
27 Correct 704 ms 38080 KB Output is correct
28 Correct 467 ms 36060 KB Output is correct
29 Correct 462 ms 35520 KB Output is correct
30 Correct 546 ms 36140 KB Output is correct
31 Correct 590 ms 36312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 8 ms 1076 KB Output is correct
3 Correct 8 ms 968 KB Output is correct
4 Correct 8 ms 948 KB Output is correct
5 Correct 612 ms 34768 KB Output is correct
6 Correct 586 ms 33816 KB Output is correct
7 Correct 640 ms 31032 KB Output is correct
8 Correct 866 ms 43588 KB Output is correct
9 Correct 962 ms 43452 KB Output is correct
10 Correct 932 ms 43420 KB Output is correct
11 Correct 665 ms 43464 KB Output is correct
12 Correct 664 ms 43480 KB Output is correct
13 Correct 677 ms 43452 KB Output is correct
14 Correct 433 ms 42636 KB Output is correct
15 Correct 449 ms 42404 KB Output is correct
16 Correct 558 ms 43196 KB Output is correct
17 Correct 552 ms 42964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 6 ms 724 KB Output is correct
6 Correct 8 ms 932 KB Output is correct
7 Correct 7 ms 724 KB Output is correct
8 Correct 9 ms 916 KB Output is correct
9 Correct 9 ms 980 KB Output is correct
10 Correct 8 ms 968 KB Output is correct
11 Correct 9 ms 852 KB Output is correct
12 Correct 9 ms 980 KB Output is correct
13 Correct 9 ms 1068 KB Output is correct
14 Correct 9 ms 980 KB Output is correct
15 Correct 9 ms 948 KB Output is correct
16 Correct 9 ms 1012 KB Output is correct
17 Correct 10 ms 980 KB Output is correct
18 Correct 12 ms 980 KB Output is correct
19 Correct 8 ms 980 KB Output is correct
20 Correct 7 ms 952 KB Output is correct
21 Correct 7 ms 980 KB Output is correct
22 Correct 7 ms 980 KB Output is correct
23 Correct 8 ms 884 KB Output is correct
24 Correct 8 ms 980 KB Output is correct
25 Correct 8 ms 912 KB Output is correct
26 Correct 6 ms 852 KB Output is correct
27 Correct 7 ms 840 KB Output is correct
28 Correct 7 ms 852 KB Output is correct
29 Correct 6 ms 808 KB Output is correct
30 Correct 8 ms 912 KB Output is correct
31 Correct 9 ms 980 KB Output is correct
32 Correct 8 ms 924 KB Output is correct
33 Correct 8 ms 980 KB Output is correct
34 Correct 8 ms 980 KB Output is correct
35 Correct 8 ms 980 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 10 ms 976 KB Output is correct
38 Correct 9 ms 980 KB Output is correct
39 Correct 8 ms 980 KB Output is correct
40 Correct 693 ms 31408 KB Output is correct
41 Correct 675 ms 31092 KB Output is correct
42 Correct 786 ms 31628 KB Output is correct
43 Correct 652 ms 28568 KB Output is correct
44 Correct 617 ms 27772 KB Output is correct
45 Correct 954 ms 37936 KB Output is correct
46 Correct 1004 ms 38000 KB Output is correct
47 Correct 971 ms 37836 KB Output is correct
48 Correct 1012 ms 37808 KB Output is correct
49 Correct 942 ms 38012 KB Output is correct
50 Correct 1047 ms 43004 KB Output is correct
51 Correct 1006 ms 43356 KB Output is correct
52 Correct 1190 ms 42752 KB Output is correct
53 Correct 1187 ms 38428 KB Output is correct
54 Correct 1259 ms 38600 KB Output is correct
55 Correct 1372 ms 38472 KB Output is correct
56 Correct 812 ms 38068 KB Output is correct
57 Correct 890 ms 38300 KB Output is correct
58 Correct 896 ms 38404 KB Output is correct
59 Correct 791 ms 38340 KB Output is correct
60 Correct 859 ms 37952 KB Output is correct
61 Correct 816 ms 37944 KB Output is correct
62 Correct 704 ms 38080 KB Output is correct
63 Correct 467 ms 36060 KB Output is correct
64 Correct 462 ms 35520 KB Output is correct
65 Correct 546 ms 36140 KB Output is correct
66 Correct 590 ms 36312 KB Output is correct
67 Correct 0 ms 212 KB Output is correct
68 Correct 8 ms 1076 KB Output is correct
69 Correct 8 ms 968 KB Output is correct
70 Correct 8 ms 948 KB Output is correct
71 Correct 612 ms 34768 KB Output is correct
72 Correct 586 ms 33816 KB Output is correct
73 Correct 640 ms 31032 KB Output is correct
74 Correct 866 ms 43588 KB Output is correct
75 Correct 962 ms 43452 KB Output is correct
76 Correct 932 ms 43420 KB Output is correct
77 Correct 665 ms 43464 KB Output is correct
78 Correct 664 ms 43480 KB Output is correct
79 Correct 677 ms 43452 KB Output is correct
80 Correct 433 ms 42636 KB Output is correct
81 Correct 449 ms 42404 KB Output is correct
82 Correct 558 ms 43196 KB Output is correct
83 Correct 552 ms 42964 KB Output is correct
84 Correct 677 ms 29328 KB Output is correct
85 Correct 583 ms 25812 KB Output is correct
86 Correct 527 ms 24328 KB Output is correct
87 Correct 1002 ms 37948 KB Output is correct
88 Correct 984 ms 37876 KB Output is correct
89 Correct 975 ms 37924 KB Output is correct
90 Correct 984 ms 37928 KB Output is correct
91 Correct 1012 ms 37980 KB Output is correct
92 Correct 1151 ms 41624 KB Output is correct
93 Correct 1156 ms 42736 KB Output is correct
94 Correct 1178 ms 38428 KB Output is correct
95 Correct 1181 ms 38464 KB Output is correct
96 Correct 1255 ms 38388 KB Output is correct
97 Correct 1268 ms 38396 KB Output is correct
98 Correct 834 ms 38084 KB Output is correct
99 Correct 844 ms 37760 KB Output is correct
100 Correct 951 ms 38092 KB Output is correct
101 Correct 949 ms 38192 KB Output is correct
102 Correct 951 ms 38596 KB Output is correct
103 Correct 900 ms 38552 KB Output is correct
104 Correct 797 ms 38620 KB Output is correct
105 Correct 548 ms 35692 KB Output is correct
106 Correct 527 ms 36100 KB Output is correct
107 Correct 621 ms 35708 KB Output is correct
108 Correct 564 ms 35976 KB Output is correct