Submission #1092303

#TimeUsernameProblemLanguageResultExecution timeMemory
1092303Nom_mxmxTwo Currencies (JOI23_currencies)C++14
0 / 100
10 ms10068 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' #define fi first #define se second #define pb push_back struct path { ll x, y, lca; ll g, s; }; struct binS { ll lo, hi, mid; }; const ll N = 1e5+3; const ll B = (1<<17); const ll L = 17; pair<ll, ll> T[2*B+3]; pair<ll, ll> edg[N]; pair<ll, ll> price[N]; vector<ll> g[N]; vector<ll> mids[N]; binS bin[N]; path p[N]; ll jump[N][L]; ll ans[N]; ll d[N]; ll siz[N]; ll pre[N]; ll n, m, q; ll ind; void dfs(ll v, ll p) { pre[v] = ++ind; siz[v] = 1; d[v] = d[p]+1; jump[v][0] = p; for(auto u:g[v]) { if(u == p) continue; dfs(u, v); siz[v] += siz[u]; } } ll lca(ll a, ll b) { if(d[a] < d[b]) swap(a, b); for(ll i=L; i>=0; i--) if(d[jump[a][i]] >= d[b]) a = jump[a][i]; for(ll i=L; i>=0; i--) { if(jump[a][i] != jump[b][i]) { a = jump[a][i]; b = jump[b][i]; } } if(a == b) return a; return jump[a][0]; } void clr() { for(ll i=1; i<2*B+3; i++) T[i] = {0, 0}; } void upd(ll a, ll b, ll x, ll v=1, ll l=1, ll r=B-1) { if(l > b || r < a) return; if(l >= a && r <= b) { T[v].fi += x; T[v].se += 1; return; } upd(a, b, x, 2*v, l, (l+r)/2); upd(a, b, x, 2*v+1, (l+r)/2+1, r); } pair<ll, ll> cnt(ll v) { pair<ll, ll> a = {0, 0}; v += B-1; while(v > 0) { a.fi += T[v].fi; a.se += T[v].se; v /= 2; } return a; } void parallel() { ll lft = q; while(lft) { clr(); for(ll i=1; i<=m; i++) { // numer krawedzi jest w price[i].se; ll curEdg = price[i].se; ll num = edg[curEdg].fi; if(d[edg[curEdg].fi] < d[edg[curEdg].se]) num = edg[curEdg].se; upd(pre[num], pre[num] + siz[num] - 1, price[i].fi); for(auto x:mids[i]) { pair<ll, ll> v1 = cnt(pre[p[x].x]); pair<ll, ll> v2 = cnt(pre[p[x].y]); pair<ll, ll> lc = cnt(pre[p[x].lca]); pair<ll, ll> path = {v1.fi + v2.fi - 2*lc.fi, v1.se + v2.se - 2*lc.se}; if(p[x].s >= path.fi) { bin[x].lo = i; ans[x] = path.se; } else bin[x].hi = i-1; bin[x].mid = (bin[x].lo + bin[x].hi + 1)/2; if(bin[x].lo == bin[x].hi) lft--; else mids[bin[x].mid].pb(x); } mids[i].clear(); mids[i].shrink_to_fit(); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; for(ll i=1; i<n; i++) { cin >> edg[i].fi >> edg[i].se; g[edg[i].fi].pb(edg[i].se); g[edg[i].se].pb(edg[i].fi); } dfs(1, 1); for(ll i=1; i<=L; i++) for(ll j=1; j<=n; j++) jump[j][i] = jump[jump[j][i-1]][i-1]; for(ll i=1; i<=m; i++) cin >> price[i].se >> price[i].fi; sort(price+1, price+m+1); for(ll i=1; i<=q; i++) { cin >> p[i].x >> p[i].y >> p[i].g >> p[i].s; p[i].lca = lca(p[i].x, p[i].y); bin[i].lo = 0; bin[i].hi = m; bin[i].mid = (n+1)/2; mids[bin[i].mid].pb(i); } parallel(); for(ll i=1; i<=q; i++) { pair<ll, ll> v1 = cnt(pre[p[i].x]); pair<ll, ll> v2 = cnt(pre[p[i].y]); pair<ll, ll> lc = cnt(pre[p[i].lca]); pair<ll, ll> path = {v1.fi + v2.fi - 2*lc.fi, v1.se + v2.se - 2*lc.se}; ll Ans = p[i].g - (path.se - ans[i]); if(Ans < 0) cout << -1 << endl; else cout << Ans << endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...