제출 #861881

#제출 시각아이디문제언어결과실행 시간메모리
861881sleepntsheepTwo Currencies (JOI23_currencies)C++17
0 / 100
2 ms12636 KiB
#include <cstdio> #include <cstring> #include <cassert> #include <string> #include <deque> #include <vector> #include <map> #include <queue> #include <algorithm> #include <iostream> #include <utility> using namespace std; using ll = long long; using ld = long double; #define ShinLena cin.tie(nullptr)->sync_with_stdio(false) #define N 100005 #define ALL(x) x.begin(), x.end() struct persist { int a; ll b; persist *l, *r; persist(int a0, ll b0) : a(a0), b(b0), l(nullptr), r(nullptr) {} persist(persist *l0, persist *r0) : a((l0?l0->a:0) + (r0?r0->a:0)), b((l0?l0->b:0) + (r0?r0->b:0)), l(l0), r(r0) {} ~persist() {if (l) delete l; if (r) delete r;} }; persist *build(int l, int r) { if (l == r) return new persist(0, 0); return new persist(build(l, (l+r)/2), build((l+r)/2+1, r)); } persist *upd(persist *v, int l, int r, int w, int W) { if (l == r) return new persist(v->a + 1, v->b + w); if (W <= (l+r)/2) return new persist(upd(v->l, l, (l+r)/2, w, W), v->r); return new persist(v->l, upd(v->r, (l+r)/2+1, r, w, W)); } vector<int> silver_cord; ll qry(persist *u, persist *v, persist *a, int l, int r, ll k) { if (l == r) { if (l >= (int)silver_cord.size()) return 0; return k / silver_cord[l]; } ll left = u->l->b + v->l->b - a->l->b * 2; if (k <= left) return qry(u->l, v->l, a->l, l, (l+r)/2, k); return (u->l->a + v->l->a - a->l->a * 2) + qry(u->r, v->r, a->r, (l+r)/2+1, r, k - left); } ll qry_count(persist *u, persist *v, persist *a) { return u->a + v->a - a->a * 2; } int n, m, q, P[17][N], dep[N]; vector<int> mark[N]; vector<tuple<int, int>> g[N]; persist *root[N]; int lca(int u, int v) { if (dep[u] < dep[v]) swap(v, u); int dt = dep[u] - dep[v]; for (int j = 17; j--;) if (dt & (1 << j)) u = P[j][u]; if (u == v) return u; for (int j = 17; j--;) if (P[j][u] != P[j][v]) u = P[j][u], v = P[j][v]; return P[0][u]; } void dfs(int u, int p, int d) { dep[u] = d; P[0][u] = p; for (int j = 1; j < 17; ++j) P[j][u] = P[j-1][P[j-1][u]]; for (auto [v, i] : g[u]) { if (v == p) continue; root[v] = root[u]; for (auto w : mark[i]) root[v] = upd(root[v], 0, n, w, lower_bound(ALL(silver_cord), w) - silver_cord.begin()); dfs(v, u, d+1); } } int main() { ShinLena; cin >> n >> m >> q; for (int u, v, i = 1; i < n; ++i) cin >> u >> v, g[u].emplace_back(v, i), g[v].emplace_back(u, i); for (int i = 0, p, c; i < m; ++i) cin >> p >> c, mark[p].push_back(c), silver_cord.push_back(c); sort(ALL(silver_cord)); silver_cord.erase(unique(ALL(silver_cord)), silver_cord.end()); root[1] = build(0, n); dfs(1, 1, 1); for (ll s, t, a, gold, silver, i = 0; i < q; ++i) { cin >> s >> t >> gold >> silver; a = lca(s, t); persist *ps = root[s], *pt = root[t], *pa = root[a]; int gone = qry(ps, pt, pa, 0, n, silver), all = qry_count(ps, pt, pa); cout << ((all - gone > gold) ? -1 : all - gone) << '\n'; } return 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...