Submission #883776

#TimeUsernameProblemLanguageResultExecution timeMemory
883776NK_Two Currencies (JOI23_currencies)C++17
100 / 100
2413 ms37888 KiB
// Success consists of going from failure to failure without loss of enthusiasm #include <bits/stdc++.h> using namespace std; #define nl '\n' #define pb push_back #define sz(x) int(x.size()) #define f first #define s second #define mp make_pair using ll = long long; template<class T> using V = vector<T>; using pi = pair<int, int>; using QRY = array<ll, 4>; using vpi = V<pi>; using vi = V<int>; using vl = V<ll>; const int LG = 18; struct BIT { int N; vl A; void init(int n) { N = n; A = vl(N, 0); }; void upd(int p, int x) { for(++p;p<=N;p+=p&-p) A[p-1] += x; } ll sum(int l, int r) { return sum(r + 1) - sum(l); } ll sum(int r) { ll s = 0; for(;r;r-=r&-r) s += A[r-1]; return s; } }; int main() { cin.tie(0)->sync_with_stdio(0); int N, M, Q; cin >> N >> M >> Q; V<vpi> adj(N); for(int i = 0; i < N-1; i++) { int u, v; cin >> u >> v; --u, --v; adj[u].pb(mp(v, i)); adj[v].pb(mp(u, i)); } vpi C(M); for(auto& x : C) { cin >> x.s >> x.f; --x.s; } sort(begin(C), end(C)); C.pb(mp(0, -1)); V<QRY> qry(Q); for(auto& x : qry) { cin >> x[0] >> x[1] >> x[2] >> x[3]; --x[0], --x[1]; } V<vi> up(N, vi(LG)); vi st(N), en(N), dep(N), EI(N-1); int t = 0; function<void(int, int)> gen = [&](int u, int p) { st[u] = t++; up[u][0] = p; for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1]; for(auto& e : adj[u]) { auto [v, i] = e; if (v != p) { EI[i] = v; // v is the child of edge i dep[v] = dep[u] + 1; gen(v, u); } } en[u] = t - 1; }; dep[0] = 0; gen(0, 0); auto jmp = [&](int u, int d) { for(int i = 0; i < LG; i++) if ((d >> i) & 1) u = up[u][i]; return u; }; auto lca = [&](int a, int b) { if (dep[a] < dep[b]) swap(a, b); a = jmp(a, dep[a] - dep[b]); if (a == b) return a; for(int i = LG - 1; i >= 0; --i) { if (up[a][i] != up[b][i]) { a = up[a][i], b = up[b][i]; } } return up[a][0]; }; BIT B, S, P; B.init(N + 1); for(int i = 0; i < M; i++) { int c, e; tie(c, e) = C[i]; B.upd(st[EI[e]], 1); B.upd(en[EI[e]] + 1, -1); } vi lo(Q, -1), hi(Q, M); vpi ans(Q, mp(-1, -1)); while(1) { vi mid(Q); for(int i = 0; i < Q; i++) { if (lo[i] >= hi[i]) mid[i] = -1; else mid[i] = (lo[i] + hi[i] + 1) / 2; } vpi E; for(int i = 0; i < Q; i++) { if (mid[i] != -1) E.pb(mp(mid[i], i)); } if (sz(E) == 0) break; sort(begin(E), end(E)); S.init(N + 1), P.init(N + 1); for(int i = 0, cur = 0; i <= M; i++) { while(cur < sz(E) && E[cur].f == i) { int idx = E[cur].s; int u = qry[idx][0], v = qry[idx][1], l = lca(u, v); ll s = S.sum(st[l], st[u]) + S.sum(st[l], st[v]) - 2 * S.sum(st[l], st[l]); // silver on path ll p = P.sum(st[l], st[u]) + P.sum(st[l], st[v]) - 2 * P.sum(st[l], st[l]); // checkpoints on path ll d = B.sum(st[l], st[u]) + B.sum(st[l], st[v]) - 2 * B.sum(st[l], st[l]); // checkpoints on path (all checkpoints) ll g = d - p; if (s <= qry[idx][3]) { lo[idx] = mid[idx]; ans[idx] = mp(s, g); } else hi[idx] = mid[idx] - 1; cur++; } if (i != M) { int c, e; tie(c, e) = C[i]; S.upd(st[EI[e]], c); S.upd(en[EI[e]] + 1, -c); P.upd(st[EI[e]], 1); P.upd(en[EI[e]] + 1, -1); } } } for(int i = 0; i < Q; i++) { if (ans[i].f <= qry[i][3] && ans[i].s <= qry[i][2]) cout << qry[i][2] - ans[i].s << nl; else cout << -1 << nl; } exit(0-0); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...