#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 {
struct S {
S* l; S* r; T t;
S() : l{nullptr}, r{nullptr}, t{e()} {}
S(const T& t) : l{nullptr}, r{nullptr}, t{t} {}
S(S* s) : l{s->l}, r{s->r}, t{s->t} {}
S(S* l, S* r) : l{l}, r{r}, t{e()} {
if (l) t = f(t, l->t);
if (r) t = f(t, r->t);
}
};
int n; vector<S*> roots;
psegtree() {} psegtree(int n) : psegtree(vector<T>(n, e())) {}
psegtree(const vector<T>& a) : n(a.size()) {
function<S*(int, int)> build = [&](int l, int r) {
if (r - l == 1) return new S(a[l]);
int m = (l + r) / 2;
return new S(build(l, m), build(m, r));
}; roots.push_back(build(0, n));
}
S* upd(S* x, int l, int r, int i, const T& t, bool set) {
if (r - l == 1) return new S(set ? t : f(x->t, t));
int m = (l + r) / 2; return i < m
? new S(upd(x->l, l, m, i, t, set), x->r)
: new S(x->l, upd(x->r, m, r, i, t, set));
}
T qry(S* x, int l, int r, int L, int R) {
if (R <= l || r <= L) return e();
if (L <= l && r <= R) return x->t;
int m = (l + r) / 2;
return f(qry(x->l, l, m, L, R), qry(x->r, m, r, L, R));
}
int last() {return roots.size() - 1;}
int copy(int k) {return roots.push_back(new S(roots[k])), roots.size() - 1;}
void upd(int k, int i, const T& t, bool set = 1) {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;}
inline ll e() {return 0;}
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, e> 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, e>(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... |