This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class S, S (*f)(S, S), S e>
struct PST {
struct T {
T *l, *r; S s;
T() : s() {l = r = nullptr;}
T(const S &v) : s(v) {l = r = nullptr;}
T(T *t) : l(t->l), r(t->r), s(t->s) {}
T(T *a, T *b) : l(a), r(b), s() {
if (l) s = f(s, l->s);
if (r) s = f(s, r->s);
}
};
int n;
vector<T*> rts;
PST(int m) : PST(vector<S>(m, e)) {
}
PST(const vector<S> &a) : n(a.size()) {
function<T*(int, int)> bld = [&](int l, int r) {
if (l + 1 == r) {
return new T(a[l]);
}
int m = (l + r) / 2;
return new T(bld(l, m), bld(m, r));
};
rts.push_back(bld(0, n));
}
T* set(T *i, int l, int r, int p, const S &v) {
if (l + 1 == r) {
return new T(v);
}
int m = (l + r) / 2;
if (p < m) {
return new T(set(i->l, l, m, p, v), i->r);
} else {
return new T(i->l, set(i->r, m, r, p, v));
}
}
T* upd(T *i, int l, int r, int p, const S &v) {
if (l + 1 == r) {
return new T(f(i->s, v));
}
int m = (l + r) / 2;
if (p < m) {
return new T(upd(i->l, l, m, p, v), i->r);
} else {
return new T(i->l, upd(i->r, m, r, p, v));
}
}
S qry(T *i, int l, int r, int L, int R) {
if (R <= l || r <= L) {
return e;
}
if (L <= l && r <= R) {
return i->s;
}
int m = (l + r) / 2;
return f(qry(i->l, l, m, L, R), qry(i->r, m, r, L, R));
}
int lst() {
return rts.size() - 1;
}
int cpy(int k) {
rts.push_back(new T(rts[k]));
return rts.size() - 1;
}
void set(int k, int i, const S &v) {
rts[k] = set(rts[k], 0, n, i, v);
}
void upd(int k, int i, const S &v) {
rts[k] = upd(rts[k], 0, n, i, v);
}
S qry(int k, int l, int r) {
return qry(rts[k], 0, n, l, r);
}
};
ll F(ll x, ll y) {
return x + y;
}
template<int SZ, bool VALS_IN_EDGES> struct HLD {
int N; vector<int> adj[SZ];
int par[SZ], root[SZ], depth[SZ], sz[SZ], ti;
int pos[SZ]; vector<int> rpos; // rpos not used but could be useful
void ae(int x, int y) { adj[x].push_back(y), adj[y].push_back(x); }
void dfsSz(int x) {
sz[x] = 1;
for(auto &y:adj[x]) {
par[y] = x; depth[y] = depth[x]+1;
adj[y].erase(find(adj[y].begin(), adj[y].end(),x)); /// remove parent from adj list
dfsSz(y); sz[x] += sz[y];
if (sz[y] > sz[adj[x][0]]) swap(y,adj[x][0]);
}
}
void dfsHld(int x) {
pos[x] = ti++; rpos.push_back(x);
for(auto &y:adj[x]) {
root[y] = (y == adj[x][0] ? root[x] : y);
dfsHld(y); }
}
void init(int _N, int R = 0) { N = _N;
par[R] = depth[R] = ti = 0; dfsSz(R);
root[R] = R; dfsHld(R);
}
int lca(int x, int y) {
for (; root[x] != root[y]; y = par[root[y]])
if (depth[root[x]] > depth[root[y]]) swap(x,y);
return depth[x] < depth[y] ? x : y;
}
/// int dist(int x, int y) { // # edges on path
/// return depth[x]+depth[y]-2*depth[lca(x,y)]; }
// LazySeg<ll,SZ> tree; // segtree for sum
template <class BinaryOp>
void processPath(int x, int y, BinaryOp op) {
for (; root[x] != root[y]; y = par[root[y]]) {
if (depth[root[x]] > depth[root[y]]) swap(x,y);
op(pos[root[y]],pos[y]); }
if (depth[x] > depth[y]) swap(x,y);
op(pos[x]+VALS_IN_EDGES,pos[y]);
}
// void modifyPath(int x, int y, int v) {
// processPath(x,y,[this,&v](int l, int r) {
// tree.upd(l,r,v); }); }
// ll queryPath(int x, int y) {
// ll res = 0; processPath(x,y,[this,&res](int l, int r) {
// res += tree.query(l,r); });
// return res; }
// void modifySubtree(int x, int v) {
// tree.upd(pos[x]+VALS_IN_EDGES,pos[x]+sz[x]-1,v); }
};
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<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].push_back(i);
adj[y].push_back(i);
}
vector<pair<int, int>> ch(M);
for (int i = 0; i < M; i++) {
int p, c;
cin >> p >> c;
ch[i] = {c, p - 1};
}
sort(ch.begin(), ch.end());
vector<int> t(N - 1);
{
auto dfs = [&](auto dfs, int x, int p) -> void {
for (int z : adj[x]) {
auto [u, v] = edges[z];
int y = x ^ u ^ v;
if (y != p) {
t[z] = y;
dfs(dfs, y, x);
}
}
};
dfs(dfs, 0, -1);
}
HLD<1000000, 1> hld;
for (int i = 0; i < N; i++) {
for (int &j : adj[i]) {
auto [x, y] = edges[j];
j = i ^ x ^ y;
if (j < i) {
hld.ae(i, j);
}
}
}
hld.init(N);
PST<ll, F, 0> q(N);
for (int i = 0; i < M; i++) {
auto [c, p] = ch[i];
q.upd(q.cpy(q.lst()), hld.pos[t[p]], c);
}
auto get = [&](int x, int y, int z) -> ll {
ll s = 0;
hld.processPath(x, y, [&](int l, int r) {
s += q.qry(z, l, r);
});
return s;
};
vector<int> S(Q), T(Q), X(Q), a(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;
if (get(S[i], T[i], mi) <= Y[i]) {
lo = mi;
} else {
hi = mi - 1;
}
}
a[i] = lo;
}
vector<ll> ans(Q);
vector<vector<int>> f(M + 1);
for (int i = 0; i < Q; i++) {
f[a[i]].push_back(i);
}
q = PST<ll, F, 0>(N);
for (int i = M; i >= 0; i--) {
if (i < M) {
auto [c, p] = ch[i];
q.upd(q.cpy(q.lst()), hld.pos[t[p]], 1);
}
for (int j : f[i]) {
ans[j] = max(-1LL, X[j] - get(S[j], T[j], q.lst()));
}
}
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... |