// #pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
// #pragma GCC target("avx2")
// #define int long long
using namespace std;
#define MAXN 100005
int n, m;
vector<pair<int, pair<int, int>>> neighbors[MAXN];
bool seen[MAXN];
int ans[MAXN];
struct seg {
int lchild;
int rchild;
int part;
int ful;
int lazyadd;
};
seg tree[MAXN * 500];
int num = 1;
void childprop(int node) {
if(!tree[node].lchild) {
tree[node].lchild = num++;
}
if(!tree[node].rchild) {
tree[node].rchild = num++;
}
}
void prop(int node, int l, int r) {
if(l + 1 != r) childprop(node);
tree[node].ful += tree[node].lazyadd;
if(l + 1 != r) {
tree[tree[node].lchild].lazyadd += tree[node].lazyadd;
tree[tree[node].rchild].lazyadd += tree[node].lazyadd;
}
tree[node].lazyadd = 0;
}
void upd(int left, int right, int val, int pos = 0, int l = 0, int r = 2000000010) {
prop(pos, l, r);
if(l >= left && r <= right) {
tree[pos].lazyadd += val;
prop(pos, l, r);
return;
}
int mid = (l + r) / 2;
int lchild = tree[pos].lchild;
int rchild = tree[pos].rchild;
if(left < mid) {
upd(left, right, val, lchild, l, mid);
}
if(right > mid) {
upd(left, right, val, rchild, mid, r);
}
prop(lchild, l, mid);
prop(rchild, mid, r);
tree[pos].ful = min(tree[lchild].ful, tree[rchild].ful);
if(tree[lchild].ful > tree[rchild].ful) {
tree[pos].part = (mid - l) + tree[rchild].part;
}else if(tree[lchild].ful < tree[rchild].ful) {
tree[pos].part = (r - mid) + tree[lchild].part;
}else {
tree[pos].part = tree[lchild].part + tree[rchild].part;
}
}
void dp(int node) {
seen[node] = true;
ans[node] = tree[1].part;
for(int i = 0; i < neighbors[node].size(); i++) {
if(!seen[neighbors[node][i].first]) {
upd(neighbors[node][i].second.first, neighbors[node][i].second.second + 1, 1);
dp(neighbors[node][i].first);
upd(neighbors[node][i].second.first, neighbors[node][i].second.second + 1, -1);
}
}
}
signed main(){
cin >> n >> m;
for(int i = 0; i < n-1; i++) {
int a, b, c, d;
cin >> a>> b >>c >> d;
neighbors[a].push_back({b, {c, d}});
neighbors[b].push_back({a, {c, d}});
}
dp(1);
for(int i = 2; i <= n; i++) {
cout<<ans[i]<<'\n';
}
return 0;
}
| # | 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... |