Submission #888869

#TimeUsernameProblemLanguageResultExecution timeMemory
888869Alihan_8Two Currencies (JOI23_currencies)C++17
100 / 100
662 ms54164 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; } struct Fenw{ vector <int> fenw; int n; Fenw(int n) : fenw(n + 1, 0), n(n) {} void upd(int i, int val){ for (; i <= n; i += i & -i ){ fenw[i] += val; } } int get(int i){ int cnt = 0; for (; i > 0; i -= i & -i ){ cnt += fenw[i]; } return cnt; } int get(int l, int r){ return get(r) - get(l - 1); } }; 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> P(m), C(m); for ( int i = 0; i < m; i++ ){ cin >> P[i] >> C[i]; --P[i]; } vector <int> S(q), T(q), X(q), Y(q); for ( int i = 0; i < q; i++ ){ cin >> S[i] >> T[i] >> X[i] >> Y[i]; --S[i], --T[i]; } vector <vector<int>> up(20, vector <int> (n)); vector <int> d(n), tin(n), tout(n), F(n); int timer = 0; auto dfs = [&](auto dfs, int u, int p) -> void{ up[0][u] = p; for ( int j = 1; j < 20; j++ ){ up[j][u] = up[j - 1][up[j - 1][u]]; } tin[u] = ++timer; for ( auto &[v, i]: G[u] ){ if ( v != p ){ F[i] = v; d[v] = d[u] + 1; dfs(dfs, v, u); } } tout[u] = timer; }; dfs(dfs, 0, 0); for ( auto &x: P ){ x = F[x]; } vector <int> L(q); for ( int i = 0; i < q; i++ ){ int u = S[i], v = T[i]; 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 ){ for ( int i = 19; i >= 0; i-- ){ if ( up[i][u] != up[i][v] ){ u = up[i][u]; v = up[i][v]; } } u = up[0][u]; } L[i] = u; } vector <ar<int,2>> rn(q, {0, m + 1}), a; for ( int i = 0; i < m; i++ ){ a.pb({C[i], P[i]}); } sort(all(a)); vector <int> us(q); bool add = true; while ( true ){ bool finish = true; vector <vector<int>> qu(m + 1); for ( int i = 0; i < q; i++ ){ if ( rn[i][0] + 1 < rn[i][1] ){ finish = false; int md = (rn[i][0] + rn[i][1]) / 2; qu[md].pb(i); } } Fenw fn1(n), fn2(n); for ( auto &[C, P]: a ){ fn1.upd(tin[P], 1); fn1.upd(tout[P] + 1, -1); } if ( add ){ for ( int j = 0; j < q; j++ ){ us[j] = fn1.get(tin[S[j]]) + fn1.get(tin[T[j]]) - 2 * fn1.get(tin[L[j]]); } add = false; } for ( int i = 0; i <= m; i++ ){ for ( auto &j: qu[i] ){ int qt = fn2.get(tin[S[j]]) + fn2.get(tin[T[j]]) - 2 * fn2.get(tin[L[j]]); if ( qt <= Y[j] ){ rn[j][0] = i; us[j] = fn1.get(tin[S[j]]) + fn1.get(tin[T[j]]) - 2 * fn1.get(tin[L[j]]); } else rn[j][1] = i; } if ( i < m ){ auto [C, P] = a[i]; fn1.upd(tin[P], -1); fn1.upd(tout[P] + 1, 1); fn2.upd(tin[P], C); fn2.upd(tout[P] + 1, -C); } } if ( finish ) break; } for ( int i = 0; i < q; i++ ){ cout << max(-1LL, X[i] - us[i]) << 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...