Submission #888623

#TimeUsernameProblemLanguageResultExecution timeMemory
888623vjudge1Two Currencies (JOI23_currencies)C++17
28 / 100
214 ms53608 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define ar array #define pb push_back #define ln '\n' #define int long long using i64 = long long; template <class F, class _S> bool chmin(F &u, const _S &v){ bool flag = false; if ( u > v ){ u = v; flag |= true; } return flag; } template <class F, class _S> bool chmax(F &u, const _S &v){ bool flag = false; if ( u < v ){ u = v; flag |= true; } return flag; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, q; cin >> n >> m >> q; vector <vector<ar<int,2>>> G(n); for ( int i = 0; i + 1 < n; i++ ){ int u, v; cin >> u >> v; --u, --v; G[u].pb({v, i}); G[v].pb({u, i}); } vector <int> pt(n); int cost = -1; for ( int i = 0; i < m; i++ ){ int p, c; cin >> p >> c; pt[--p]++; cost = c; } vector <vector<int>> up(20, vector <int> (n)), sum(20, vector <int> (n)); vector <int> d(n), dp(n); auto dfs = [&](auto dfs, int u, int p) -> void{ up[0][u] = p; for ( int i = 1; i < 20; i++ ){ up[i][u] = up[i - 1][up[i - 1][u]]; } for ( auto &[v, i]: G[u] ){ if ( v != p ){ d[v] = d[u] + 1; dp[v] = dp[u] + pt[i]; dfs(dfs, v, u); } } }; dfs(dfs, 0, 0); auto lca = [&](int u, int v){ if ( d[u] < d[v] ) swap(u, v); int df = d[u] - d[v]; for ( int i = 0; i < 20; i++ ){ if ( df >> i & 1 ){ u = up[i][u]; } } if ( u == v ){ return u; } for ( int i = 19; i >= 0; i-- ){ if ( up[i][u] != up[i][v] ){ u = up[i][u]; v = up[i][v]; } } return up[0][u]; }; auto get = [&](int u, int v){ return dp[u] + dp[v] - dp[lca(u, v)] * 2; }; while ( q-- ){ int s, t, x, y; cin >> s >> t >> x >> y; --s, --t; cout << max(-1LL, x - max(0LL, get(s, t) - y / cost)) << ln; } cout << '\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...