Submission #888662

#TimeUsernameProblemLanguageResultExecution timeMemory
888662vjudge1Two Currencies (JOI23_currencies)C++17
38 / 100
432 ms51396 KiB
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define all(x) x.begin(), x.end()
//#define size(x) (int)x.size()

template<class S, class T> 
bool chmin(S &a, const T &b) {
    return (a > b ? (a = b) == b : false);
}
template<class S, class T> 
bool chmax(S &a, const T &b) {
    return (a < b ? (a = b) == b : false);
}
const int inf = 1e9;
const ll INF = 1e18;
const int mod = 998244353;
const int N = 1e5 + 1;

int C;

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m, q;
    cin >> n >> m >> q;
    int a[n], b[n];
    vector<int> c[n], g[n + 1];
    map<pair<int, int>, int> ind;
    for (int i = 1; i < n; ++i) {
        cin >> a[i] >> b[i];
        g[a[i]].push_back(b[i]);
        g[b[i]].push_back(a[i]);
        ind[{a[i], b[i]}] = i;
        ind[{b[i], a[i]}] = i;
    }
    for (int i = 0; i < m; ++i) {
        int j, w;
        cin >> j >> w;
        C = w;
        c[j].push_back(w);
    }
    if (n <= 2000 && m <= 2000 && q <= 2000) {
        vector<int> p(n + 1, 0);
        function<void(int, int)> dfs = [&](int v, int parent) {
            for (int to : g[v]) {
                if (to != parent) {
                    p[to] = v;
                    dfs(to, v);
                }
            }
        };
        while (q--) {
            int a, b, x; ll y;
            cin >> a >> b >> x >> y;
            vector<int> path;
            dfs(a, 0);
            int v = b;
            while (v) {
                path.push_back(v);
                v = p[v];
            }
            vector<int> cost;
            for (int i = 0; i + 1 < (int)path.size(); ++i) {
                for (int j : c[ind[{path[i], path[i + 1]}]]) {
                    cost.push_back(j);
                }
            }
            sort(all(cost), greater<int>());
            while (!cost.empty() && y - cost.back() >= 0) {
                y -= cost.back();
                cost.pop_back();
            }
            x -= size(cost);
            cout << (x < 0 ? -1 : x) << '\n';
            for (int i = 1; i <= n; ++i) {
                p[i] = 0;
            }
        }
    } else {
        vector<int> tin(n + 1), tout(n + 1), val(n + 1, 0);
        vector<vector<int>> up(n + 1, vector<int> (17, 1));
        int timer = -1;
        function<void(int, int)> dfs = [&](int v, int p) {
            val[v] += (int)c[ind[{v, p}]].size();
            tin[v] = ++timer;
            up[v][0] = p;
            for (int i = 1; i < 17; ++i) {
                up[v][i] = up[up[v][i - 1]][i - 1];
            }
            for (int to : g[v]) {
                if (to != p) {
                    val[to] += val[v];
                    dfs(to, v);
                }
            }
            tout[v] = ++timer;
        };
        auto upper = [&](int u, int v) {
            return tin[u] <= tin[v] && tout[u] >= tout[v];
        };
        auto get_lca = [&](int u, int v) {
            if (upper(u, v)) return u;
            if (upper(v, u)) return v;
            for (int i = 16; i >= 0; --i) {
                if (!upper(up[u][i], v)) {
                    u = up[u][i];
                }
            }
            return up[u][0];
        };
        dfs(1, 1);
        while (q--) {
            int a, b, x; ll y;
            cin >> a >> b >> x >> y;
            if (upper(a, b)) {
                int need = val[b] - val[a];
                y /= C;
                if (need <= y) {
                    cout << x << '\n';
                } else {
                    need -= y;
                    x -= need;
                    cout << (x < 0 ? -1 : x) << '\n';
                }
            } else if (upper(b, a)) {
                swap(a, b);
                int need = val[b] - val[a];
                y /= C;
                if (need <= y) {
                    cout << x << '\n';
                } else {
                    need -= y;
                    x -= need;
                    cout << (x < 0 ? -1 : x) << '\n';
                }
            } else {
                int lca = get_lca(a, b);
                int need = val[b] + val[a] - val[lca] * 2;
                y /= C;
                if (need <= y) {
                    cout << x << '\n';
                } else {
                    need -= y;
                    x -= need;
                    cout << (x < 0 ? -1 : x) << '\n';
                }
            }
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...