Submission #775004

#TimeUsernameProblemLanguageResultExecution timeMemory
775004Halym2007Two Currencies (JOI23_currencies)C++11
0 / 100
2 ms2656 KiB
#include <bits/stdc++.h> #define ff first #define ss second #define sz size() #define pb push_back using namespace std; typedef long long ll; const int N = 100005; const int LOG = 17; // const int mod = 1e9+7; // ll bigmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} int lvl[N], p[N][LOG], P[N][LOG]; int gold, silver, val, s, t; int n, m, q, l[N], r[N], pol[N]; vector <pair <int, int>> adj[N]; void dfs (int x, int par) { for (auto i : adj[x]) { if (i.ff != par) { lvl[i.ff] = lvl[x] + 1; p[i.ff][0] = x; P[i.ff][0] = i.ss; dfs (i.ff, x); } } } int kth_ancestor (int x, int k) { for (int i = LOG - 1; i >= 0; i--) { if (k>>i&1) { x = p[x][i]; } } return x; } int LCA(int x, int y) { if (lvl[x] > lvl[y]) { x = kth_ancestor(x, lvl[x] - lvl[y]); } else if (lvl[x] < lvl[y]) { y = kth_ancestor(y, lvl[y] - lvl[x]); } if (x == y) return x; for (int i = LOG - 1; i >= 0; i--) { if (p[x][i] != p[y][i]) { x = p[x][i]; y = p[y][i]; } } return p[x][0]; } int jogap (int x, int k) { int ret = 0; for (int i = LOG - 1; i >= 0; i--) { if (k>>i&1) { ret += P[x][i]; x = p[x][i]; } } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // clock_t tStart = clock(); // printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC); cin >> n >> m >> q; for (int i = 1; i < n; ++i) { cin >> l[i] >> r[i]; } int x, y; for (int i = 1; i <= m; ++i) { cin >> x >> y; pol[x]++; val = y; } for (int i = 1; i < n; ++i) { adj[l[i]].pb ({r[i], pol[i]}); adj[r[i]].pb ({l[i], pol[i]}); } dfs (1, 0); for (int i = 1; i < LOG; ++i) { for (int j = 1; j <= n; ++j) { if (p[j][i - 1]) { p[j][i] = p[p[j][i - 1]][i - 1]; P[j][i] = P[j][i - 1] + P[p[j][i - 1]][i - 1]; } } } while ( q-- ) { cin >> s >> t >> gold >> silver; int a1 = LCA(s, t); int a2 = jogap (s, lvl[s] - lvl[a1]); int a3 = jogap (t, lvl[t] - lvl[a1]); int a4 = a2 + a3; a4 -= (silver / val); gold -= a4; if (gold < 0) { cout << "-1\n"; } else cout << gold << "\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...