#include <iostream>
#include <vector>
#include <array>
#include <set>
#define pb push_back
using namespace std;
const int N = 1e5+10;
vector<array<int, 3>> adj[N];
int ans[N];
set<int> state;
vector<array<int, 2>> hist;
void print_state() {
for (auto u : state) {
if (u&1) {
cout << "[" << (u/2) << ", ";
} else
cout << (u/2 - 1) << "] ";
}
cout << endl;
}
int adds(int l, int r) {
r++; /* [l, r) */
int val = r - l;
int last = l;
auto it = state.lower_bound(2*l+1);
vector<int> trem;
vector<int> tadd;
if (it == state.end() || (*it) % 2 == 1)
tadd.pb(2*l+1);
while (it != state.end() && (*it) <= 2 * r) {
int v = (*it);
trem.pb(v);
if (v & 1)
last = v / 2;
else
val -= (v/2)-last;
it++;
}
if (it == state.end() || (*it) % 2 == 1) {
tadd.pb(2*r);
} else {
val -= r - last;
}
for (auto u : trem) {
/* cout << "ERASE " << u << endl;*/
state.erase(u);
hist.pb({1, u});
}
for (auto u : tadd) {
/* cout << "ADD " << u << endl;*/
state.insert(u);
hist.pb({2, u});
}
hist.pb({0, 0});
return val;
}
void rems() {
hist.pop_back();
while (hist.size() > 0 && hist.back()[0] != 0) {
auto u = hist.back();
hist.pop_back();
if (u[0] == 1)
state.insert(u[1]);
else
state.erase(u[1]);
}
}
void dfs(int x, int p, int c) {
ans[x] = c;
int d = c;
/* cout << x << " ";*/
/* print_state();*/
for (auto u : adj[x]) {
if (u[0] == p) continue;
d = c + adds(u[1], u[2]);
dfs(u[0], x, d);
rems();
}
}
int main() {
int n, m;
int a, b, l, r;
cin >> n >> m;
for (int i = 1; i < n; i++) {
cin >> a >> b >> l >> r;
--a, --b;
adj[a].pb({b, l, r});
adj[b].pb({a, l, r});
}
dfs(0, 0, 0);
for (int i = 1; i < n; i++)
cout << ans[i] << endl;
}
# | 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... |