#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<bool e = 0> struct HLD {
int n, timer; vector<vector<int>> adj;
vector<int> sz, lvl, top, pos, par;
HLD(int n) : n{n}, timer{}, adj(n), sz(n), lvl(n), top(n), pos(n), par(n, -1) {}
void add_edge(int x, int y) {
adj[x].push_back(y);
adj[y].push_back(x);
}
void init(int x) {
sz[x] = 1;
for (int& y : adj[x]) {
lvl[y] = lvl[x] + 1;
adj[y].erase(find(adj[y].begin(), adj[y].end(), par[y] = x));
init(y);
sz[x] += sz[y];
if (sz[y] > sz[adj[x][0]]) swap(y, adj[x][0]);
}
}
void dfs(int x) {
pos[x] = timer++;
for (int y : adj[x])
top[y] = y == adj[x][0] ? top[x] : y,
dfs(y);
}
void build(int r = 0) {
top[r] = r, init(r), dfs(r);
}
int lca(int x, int y) {
for (; top[x] != top[y]; x = par[top[x]])
if (lvl[top[x]] < lvl[top[y]]) swap(x, y);
return lvl[x] < lvl[y] ? x : y;
}
template<typename F> int trav_path(int x, int y, F f) {
for (; top[x] != top[y]; x = par[top[x]]) {
if (lvl[top[x]] < lvl[top[y]]) swap(x, y);
f(pos[top[x]], pos[x] + 1);
}
if (lvl[x] > lvl[y]) swap(x, y);
f(pos[x] + e, pos[y] + 1);
return x;
}
int operator[](int i) {return pos[i];}
};
template<typename T, T (*f)(T, T), T e> struct psegtree {
int n, N; vector<int> roots; vector<tuple<int, int, T>> a;
psegtree() {} psegtree(int n) : psegtree(vector<T>(n, e)) {}
psegtree(const vector<T>& a) : n(a.size()), N{1}, a{make_tuple(0, 0, e)} {
function<int(int, int)> build = [&](int l, int r) {
if (r - l == 1) return shine(a[l]);
int m = (l + r) / 2;
return shine(build(l, m), build(m, r));
}; roots = {build(0, n)};
}
int shine(int l, int r) {return a.emplace_back(l, r, f(get<2>(a[l]), get<2>(a[r]))), N++;}
int shine(const T& t) {return a.emplace_back(0, 0, t), N++;}
int last() {return roots.size() - 1;}
int copy(int k) {return a.push_back(a[roots[k]]), roots.push_back(N++), roots.size() - 1;}
int upd(int x, int l, int r, int i, const T&t, bool set) {
if (r - l == 1) return shine(set ? t : f(get<2>(a[x]), t));
int m = (l + r) / 2; return i < m
? shine(upd(get<0>(a[x]), l, m, i, t, set), get<1>(a[x]))
: shine(get<0>(a[x]), upd(get<1>(a[x]), m, r, i, t, set));
}
T qry(int x, int l, int r, int L, int R) {
if (R <= l || r <= L) return e;
if (L <= l && r <= R) return get<2>(a[x]);
int m = (l + r) / 2;
return f(qry(get<0>(a[x]), l, m, L, R), qry(get<1>(a[x]), m, r, L, R));
}
int upd(int k, int i, const T& t, bool set = 1) {return roots[k] = upd(roots[k], 0, n, i, t, set);}
T qry(int k, int l, int r) {return qry(roots[k], 0, n, l, r);}
};
ll f(ll a, ll b) {return a + b;}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, Q;
cin >> N >> M >> Q;
HLD<1> hld(N);
vector<pair<int, int>> edges(N - 1);
vector<vector<pair<int, int>>> adj(N);
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
edges[i] = {a, b};
adj[a].emplace_back(b, i);
adj[b].emplace_back(a, i);
hld.add_edge(a, b);
}
vector<int> w(N - 1); {
function<void(int, int)> dfs = [&](int x, int p) {
for (auto [y, z] : adj[x]) {
if (y == p) continue;
w[z] = y;
dfs(y, x);
}
};
dfs(0, -1);
}
hld.build();
vector<pair<int, int>> ch(M);
for (int i = 0; i < M; i++) {
int e, s;
cin >> e >> s; e--;
ch[i] = {s, w[e]};
}
sort(ch.begin(), ch.end());
psegtree<ll, f, 0> pst(N);
for (auto [s, i] : ch) {
pst.upd(pst.copy(pst.last()), hld.pos[i], s, 0);
}
vector<vector<int>> a(M + 1);
vector<int> S(Q), T(Q), X(Q), ans(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]--;
int lo = 0, hi = M;
while (lo < hi) {
int mi = (lo + hi + 1) / 2;
ll s = 0;
hld.trav_path(S[i], T[i], [&](int l, int r) {
s += pst.qry(mi, l, r);
});
s <= Y[i] ? lo = mi : hi = mi - 1;
}
a[lo].push_back(i);
}
pst = psegtree<ll, f, 0>(N);
for (int i = M; i >= 0; i--) {
if (i < M) {
auto [s, j] = ch[i];
pst.upd(pst.copy(pst.last()), hld.pos[j], 1, 0);
}
for (auto j : a[i]) {
int cnt = 0;
hld.trav_path(S[j], T[j], [&](int l, int r) {
cnt += pst.qry(pst.last(), l, r);
});
ans[j] = max<ll>(-1, X[j] - cnt);
}
}
for (int i = 0; i < Q; i++) cout << ans[i] << "\n";
return 6/22;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |