Submission #825833

#TimeUsernameProblemLanguageResultExecution timeMemory
825833t6twotwoTwo Currencies (JOI23_currencies)C++17
68 / 100
281 ms30932 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M, Q; cin >> N >> M >> Q; vector<pair<int, int>> edges(N - 1); vector<vector<pair<int, int>>> adj(N); for (int i = 0; i < N - 1; i++) { int x, y; cin >> x >> y; x--, y--; edges[i] = {x, y}; adj[x].emplace_back(y, i); adj[y].emplace_back(x, i); } vector<int> P(M), C(M); vector<vector<int>> c(N - 1); for (int i = 0; i < M; i++) { cin >> P[i] >> C[i]; P[i]--; c[P[i]].push_back(C[i]); } if (C == vector(M, C[0])) { vector<int> cnt(N - 1); for (int i = 0; i < M; i++) { cnt[P[i]]++; } vector<int> f(N), dep(N); vector par(N, vector<int>(20, -1)); auto dfs = [&](auto dfs, int x) -> void { for (int i = 0; i < 19 && par[x][i] != -1; i++) { par[x][i + 1] = par[par[x][i]][i]; } for (auto [y, z] : adj[x]) { if (y != par[x][0]) { dep[y] = dep[x] + 1; f[y] = f[x] + cnt[z]; par[y][0] = x; dfs(dfs, y); } } }; dfs(dfs, 0); auto lca = [&](int x, int y) { if (dep[x] < dep[y]) { swap(x, y); } for (int i = 0; i < 20; i++) { if ((dep[x] - dep[y]) >> i & 1) { x = par[x][i]; } } if (x == y) { return x; } for (int i = 19; i >= 0; i--) { if (par[x][i] != par[y][i]) { x = par[x][i]; y = par[y][i]; } } return par[x][0]; }; while (Q--) { int S, T, X; ll Y; cin >> S >> T >> X >> Y; S--, T--; int k = f[S] + f[T] - 2 * f[lca(S, T)]; cout << max(-1LL, X - max(0LL, k - Y / C[0])) << "\n"; } return 0; } if (N <= 2000 && M <= 2000 && Q <= 2000) { while (Q--) { int S, T, X; ll Y; cin >> S >> T >> X >> Y; S--, T--; vector<int> p(N, -1); queue<int> q; p[S] = S; q.push(S); while (!q.empty()) { int x = q.front(); q.pop(); for (auto [y, z] : adj[x]) { if (p[y] == -1) { p[y] = z; q.push(y); } } } priority_queue<int, vector<int>, greater<int>> pq; while (T != S) { for (int v : c[p[T]]) { pq.push(v); } auto [x, y] = edges[p[T]]; T ^= x ^ y; } while (!pq.empty() && pq.top() <= Y) { Y -= pq.top(); pq.pop(); } X -= pq.size(); X = max(X, -1); cout << X << "\n"; } return 0; } vector<int> S(Q), T(Q), X(Q); vector<ll> Y(Q); for (int i = 0; i < Q; i++) { cin >> S[i] >> T[i] >> X[i] >> Y[i]; S[i]--, T[i]--; if (S[i] > T[i]) { swap(S[i], T[i]); } } vector<pair<int, int>> ch(M); for (int i = 0; i < M; i++) { ch[i] = {C[i], P[i]}; } sort(ch.begin(), ch.end()); vector<int> lo(Q), hi(Q, M); vector<ll> bit(N); auto add = [&](int i, int v) { for (i++; i < N; i += i & -i) { bit[i] += v; } }; auto sum = [&](int i) { ll s = 0; for (; i; i -= i & -i) { s += bit[i]; } return s; }; for (int z = 0; z < 20; z++) { vector<int> mi(Q); vector<vector<int>> f(M); for (int i = 0; i < Q; i++) { if (lo[i] != hi[i]) { mi[i] = (lo[i] + hi[i] + 1) / 2; f[mi[i] - 1].push_back(i); } } bit.assign(N, 0); for (int i = 0; i < M; i++) { auto [c, p] = ch[i]; add(p, c); for (int j : f[i]) { if (sum(T[j]) - sum(S[j]) <= Y[j]) { lo[j] = i + 1; } else { hi[j] = i; } } } } vector<ll> ans(Q); vector<vector<int>> f(M + 1); for (int i = 0; i < Q; i++) { f[lo[i]].push_back(i); } bit.assign(N, 0); for (int i = M; i >= 0; i--) { if (i < M) { auto [c, p] = ch[i]; add(p, 1); } for (int j : f[i]) { ans[j] = max(-1LL, X[j] - sum(T[j]) + sum(S[j])); } } for (int i = 0; i < Q; i++) { cout << ans[i] << " \n"[i == Q - 1]; } return 6/22; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...