이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 1e6;
int iDeb[MAXN], iFin[MAXN], lazy[MAXN];
pair<int, int> seg[MAXN];
void pull(int node) { seg[node] = max(seg[2 * node], seg[2 * node + 1]); }
void build(int node, int l, int r) {
iDeb[node] = l, iFin[node] = r;
lazy[node] = 0;
if (l == r) {
seg[node] = pair(0, l);
return;
}
int m = (l + r) / 2;
build(2 * node, l, m);
build(2 * node + 1, m + 1, r);
}
void push(int node) {
if (!lazy[node])
return;
int x = lazy[node];
lazy[node] = 0;
seg[node].first += x;
if (iDeb[node] < iFin[node]) {
lazy[2 * node] += x, lazy[2 * node + 1] += x;
}
}
void rangeAdd(int node, int l, int r, int x) {
push(node);
if (iDeb[node] > r or iFin[node] < l)
return;
if (l <= iDeb[node] and iFin[node] <= r) {
lazy[node] = x;
push(node);
return;
}
rangeAdd(2 * node, l, r, x);
rangeAdd(2 * node + 1, l, r, x);
pull(node);
}
const int INF = 1e12;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbSommet;
cin >> nbSommet;
int tot = 0;
vector<vector<tuple<int, int, int>>> adj(nbSommet);
for (int i = 1; i < nbSommet; ++i) {
int u, v, C, D;
cin >> u >> v >> C >> D;
--u, --v;
adj[u].emplace_back(v, C, D);
adj[v].emplace_back(u, D, C);
tot += C + D;
}
vector<int> par(nbSommet), costDown(nbSommet), costUp(nbSommet);
vector<int> tin(nbSommet), tout(nbSommet);
vector<int> order;
int Time = 0;
auto Dfs = [&](auto dfs, int u, int p) -> void {
par[u] = p;
tin[u] = Time++;
order.push_back(u);
for (auto [v, down, up] : adj[u])
if (v != p) {
costDown[v] = up;
costUp[v] = down;
dfs(dfs, v, u);
}
tout[u] = Time;
};
Dfs(Dfs, 0, 0);
dbg(tin, tout);
dbg(order);
build(1, 0, Time - 1);
for (int u = 1; u < nbSommet; ++u) {
rangeAdd(1, 0, Time - 1, costDown[u]);
rangeAdd(1, tin[u], tout[u] - 1, costUp[u] - costDown[u]);
}
vector<bool> cleared(nbSommet);
auto Clear = [&](auto clear, int u) {
if (!u or cleared[u])
return;
cleared[u] = true;
rangeAdd(1, tin[u], tout[u] - 1, -costUp[u]);
clear(clear, par[u]);
};
set<pair<int, int>> byTin, byTout;
for (int u = 1; u < nbSommet; ++u)
byTin.emplace(tin[u], u), byTout.emplace(tout[u], u);
vector<bool> downRemoved(nbSommet);
vector<int> sol(nbSommet + 1);
sol[1] = seg[1].first;
for (int u = 0; u < nbSommet; ++u)
if (adj[u].size() > 1)
rangeAdd(1, tin[u], tin[u], -INF);
int curSol = 0;
for (int i = 1; i <= nbSommet; ++i) {
auto [add, u] = seg[1];
u = order[u];
curSol += add;
dbg(u + 1, add);
if (i > 1)
sol[i] = curSol;
Clear(Clear, u);
auto it = byTin.begin();
while ((it = byTin.lower_bound({tin[u] + 1, 0LL})) != byTin.end()) {
auto [t, v] = *it;
byTin.erase(it);
if (!downRemoved[v]) {
rangeAdd(1, 0, Time - 1, -costDown[v]);
rangeAdd(1, tin[v], tout[v] - 1, costDown[v]);
downRemoved[v] = true;
}
}
auto it2 = byTout.begin();
while ((it2 = byTout.lower_bound({tin[u] + 1, 0LL})) != byTout.begin()) {
--it2;
auto [t, v] = *it2;
byTout.erase(it2);
if (!downRemoved[v]) {
rangeAdd(1, 0, Time - 1, -costDown[v]);
rangeAdd(1, tin[v], tout[v] - 1, costDown[v]);
downRemoved[v] = true;
}
}
}
int Q;
cin >> Q;
while (Q--) {
int E;
cin >> E;
cout << tot - sol[E] << '\n';
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |