#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+5;
const int N2 = 1e5+1;
vector<array<int, 3>> g[N2];
int ans[N2];
int n, M;
int nxt = 1;
unordered_map<int, int> L, R, sg, lz;
void push(int u, int l, int r, int U, unordered_map<int, int> &usedsg, unordered_map<int, int> &usedlz){
if(lz[u]) {
if(usedsg.find(u) == usedsg.end()) usedsg[u] = sg[u];
sg[u] = r-l+1;
}
if(l != r){
if(!L[u]) L[u] = ++nxt;
if(!R[u]) R[u] = ++nxt;
if(usedlz.find(L[u]) == usedlz.end()) usedlz[L[u]] = lz[L[u]];
lz[L[u]] += lz[u];
if(usedlz.find(R[u]) == usedlz.end()) usedlz[R[u]] = lz[R[u]];
lz[R[u]] += lz[u];
}
lz[u] = 0;
}
void update(int u, int l, int r, int x, int y, int U, unordered_map<int, int> &usedsg, unordered_map<int, int> &usedlz){
push(u, l, r, U, usedsg, usedlz);
if(y < l or x > r) return;
if(x <= l and y >= r){
if(usedlz.find(u) == usedlz.end()) usedlz[u] = lz[u];
lz[u]++;
push(u, l, r, U, usedsg, usedlz);
return;
}
///
int m = (l+r)/2;
bool sol = true;
if(y < l or x > m) sol = false;
if(sol){
if(L.find(u) == L.end()) L[u] = ++nxt;
update(L[u], l, m, x, y, U, usedsg, usedlz);
} else {
if(L.find(u) != L.end()){
push(L[u], l, m, U, usedsg, usedlz);
}
}
bool sag = true;
if(y < m+1 or x > r) sag = false;
if(sag){
if(R.find(u) == R.end()) R[u] = ++nxt;
update(R[u], m+1, r, x, y, U, usedsg, usedlz);
} else {
if(R.find(u) != R.end()){
push(R[u], m+1, r, U, usedsg, usedlz);
}
}
///
if(usedsg.find(u) == usedsg.end()) usedsg[u] = sg[u];
sg[u] = sg[L[u]] + sg[R[u]];
}
void dfs(int u, int p, int li, int ri){
unordered_map<int, int> usedsg, usedlz;
update(1, 1, M, li, ri, u, usedsg, usedlz);
ans[u] = sg[1];
for(auto [to, lj, rj]: g[u]){
if(to == p) continue;
dfs(to, u, lj, rj);
}
for(auto [node, ans]: usedsg) sg[node] = ans;
for(auto [node, ans]: usedlz) lz[node] = ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> M;
for(int i=1; i<n; i++){
int x, y, li, ri; cin >> x >> y >> li >> ri;
g[x].push_back({y, li, ri});
g[y].push_back({x, li, ri});
}
dfs(1, -1, 0, 0);
for(int i=2; i<=n; i++) cout << ans[i] << '\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... |