#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 1e5 + 5;
struct Edge {
int to, l, r;
Edge(int _to, int _l, int _r) : to(_to), l(_l), r(_r) { }
};
struct Node {
int len;
int mn, mn_cnt;
Node() : len(0), mn(1e9), mn_cnt(0) { }
};
vector<vector<Edge>> graph;
vector<int> cc;
Node sg[12*MAXN];
int lazy[12*MAXN], ans[MAXN];
int n, M;
Node merge(Node a, Node b) {
Node temp;
temp.len = a.len + b.len;
temp.mn = min(a.mn, b.mn);
temp.mn_cnt = (temp.mn == a.mn ? a.mn_cnt : 0) + (temp.mn == b.mn ? b.mn_cnt : 0);
return temp;
}
void push(int k, int a, int b) {
if (lazy[k]) {
sg[k].mn += lazy[k];
if (a != b) {
lazy[2*k] += lazy[k];
lazy[2*k+1] += lazy[k];
}
lazy[k] = 0;
}
}
void init(int k, int a, int b) {
if (a == b) {
sg[k].mn_cnt = sg[k].len = (a == n ? 0 : cc[a] - cc[a-1]);
sg[k].mn = 0;
return ;
}
init(2*k, a, (a+b)/2);
init(2*k+1, (a+b)/2+1, b);
sg[k] = merge(sg[2*k], sg[2*k+1]);
}
void update(int k, int a, int b, int q_l, int q_r, int val) {
push(k, a, b);
if (b < q_l || a > q_r)
return ;
if (q_l <= a && b <= q_r) {
lazy[k] += val;
push(k, a, b);
return ;
}
update(2*k, a, (a+b)/2, q_l, q_r, val);
update(2*k+1, (a+b)/2+1, b, q_l, q_r, val);
sg[k] = merge(sg[2*k], sg[2*k+1]);
}
Node emp;
Node query(int k, int a, int b, int q_l, int q_r) {
push(k, a, b);
if (b < q_l || a > q_r)
return emp;
if (q_l <= a && b <= q_r)
return sg[k];
return merge(query(2*k, a, (a+b)/2, q_l, q_r), query(2*k+1, (a+b)/2+1, b, q_l, q_r));
}
int index(int k) {
return upper_bound(cc.begin(), cc.end(), k) - cc.begin();
}
void dfs(int node, int parent) {
push(1, 1, n);
if (sg[1].mn == 0)
ans[node] = cc[n-2] - sg[1].mn_cnt;
for (auto u : graph[node]) {
if (u.to == parent)
continue;
int left = index(u.l);
int right = index(u.r);
update(1, 1, n, left, right, 1);
dfs(u.to, node);
update(1, 1, n, left, right, -1);
}
}
int main() {
fast
int N, a, b, l, r;
cin >> N >> M;
graph = vector<vector<Edge>>(N+1, vector<Edge>());
for (int i = 1; i < N; i++) {
cin >> a >> b >> l >> r;
graph[a].push_back(Edge(b, l, r));
graph[b].push_back(Edge(a, l, r));
cc.push_back(l);
cc.push_back(r);
cc.push_back(r+1);
}
sort(cc.begin(), cc.end());
cc.erase(unique(cc.begin(), cc.end()), cc.end());
n = cc.size();
init(1, 1, n);
dfs(1, 1);
for (int i = 2; i <= N; i++)
cout << ans[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15448 KB |
Output is correct |
2 |
Correct |
4 ms |
15452 KB |
Output is correct |
3 |
Correct |
4 ms |
15380 KB |
Output is correct |
4 |
Incorrect |
4 ms |
15452 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15448 KB |
Output is correct |
2 |
Correct |
4 ms |
15452 KB |
Output is correct |
3 |
Correct |
4 ms |
15380 KB |
Output is correct |
4 |
Incorrect |
4 ms |
15452 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15448 KB |
Output is correct |
2 |
Correct |
4 ms |
15452 KB |
Output is correct |
3 |
Correct |
4 ms |
15380 KB |
Output is correct |
4 |
Incorrect |
4 ms |
15452 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15448 KB |
Output is correct |
2 |
Correct |
4 ms |
15452 KB |
Output is correct |
3 |
Correct |
4 ms |
15380 KB |
Output is correct |
4 |
Incorrect |
4 ms |
15452 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15448 KB |
Output is correct |
2 |
Correct |
4 ms |
15452 KB |
Output is correct |
3 |
Correct |
4 ms |
15380 KB |
Output is correct |
4 |
Incorrect |
4 ms |
15452 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |