이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct node {
node *l, *r;
ll s;
node() : s(0) {
l = r = nullptr;
}
node(ll v) : s(v) {
l = r = nullptr;
}
node(node *u, node *v) : l(u), r(v) {
s = l->s + r->s;
}
};
node *bld(int l, int r) {
if (l + 1 == r) {
return new node();
}
int m = (l + r) / 2;
return new node(bld(l, m), bld(m, r));
}
node *add(node *p, int l, int r, int i, int v) {
if (l + 1 == r) {
return new node(p->s + v);
}
int m = (l + r) / 2;
if (i < m) {
return new node(add(p->l, l, m, i, v), p->r);
} else {
return new node(p->l, add(p->r, m, r, i, v));
}
}
ll qry(node *p, int l, int r, int L, int R) {
if (R <= l || r <= L) {
return 0;
}
if (L <= l && r <= R) {
return p->s;
}
int m = (l + r) / 2;
return qry(p->l, l, m, L, R) + qry(p->r, m, r, L, R);
}
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);
}
for (int i = 0; i < N; i++) {
for (int &j : adj[i]) {
auto [x, y] = edges[j];
j = i ^ x ^ y;
}
}
vector<int> dep(N), par(N, -1), sz(N);
auto init = [&](auto init, int x) -> void {
if (par[x] != -1) {
adj[x].erase(find(adj[x].begin(), adj[x].end(), par[x]));
}
sz[x] = 1;
for (int &y : adj[x]) {
par[y] = x;
dep[y] = dep[x] + 1;
init(init, y);
sz[x] += sz[y];
if (sz[y] > sz[adj[x][0]]) {
swap(y, adj[x][0]);
}
}
};
init(init, 0);
vector<int> top(N), in(N);
int timer = 0;
auto dfs = [&](auto dfs, int x) -> void {
in[x] = timer++;
for (int y : adj[x]) {
top[y] = adj[x][0] == y ? top[x] : y;
dfs(dfs, y);
}
};
dfs(dfs, 0);
vector<node*> q{bld(0, N)};
for (int i = 0; i < M; i++) {
auto [c, p] = ch[i];
q.push_back(add(q.back(), 0, N, in[t[p]], c));
}
auto get = [&](int x, int y, int z) {
ll s = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) {
swap(x, y);
}
s += qry(q[z], 0, N, in[top[x]], in[x] + 1);
x = par[top[x]];
}
if (dep[x] < dep[y]) {
swap(x, y);
}
s += qry(q[z], 0, N, in[y] + 1, in[x] + 1);
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 = vector{bld(0, N)};
for (int i = M; i >= 0; i--) {
if (i < M) {
auto [c, p] = ch[i];
q.push_back(add(q.back(), 0, N, in[t[p]], 1));
}
for (int j : f[i]) {
ans[j] = max(-1LL, X[j] - get(S[j], T[j], q.size() - 1));
}
}
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... |