This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<array<int, 3>>> adj(n);
vector<int> all;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u, --v;
int l, r;
cin >> l >> r;
--l, --r;
all.push_back(l);
all.push_back(r);
all.push_back(r + 1);
adj[u].push_back({v, l, r});
adj[v].push_back({u, l, r});
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
m = (int) all.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < (int) adj[i].size(); j++) {
adj[i][j][1] = lower_bound(all.begin(), all.end(), adj[i][j][1]) - all.begin();
adj[i][j][2] = lower_bound(all.begin(), all.end(), adj[i][j][2]) - all.begin();
}
}
debug(all);
int B = sqrt(m);
int number_of_blocks = (m + B - 1) / B;
vector<int> small(m);
vector<int> large(number_of_blocks);
vector<int> couches(number_of_blocks);
auto Insert = [&](int l, int r) {
int bL = l / B;
int bR = r / B;
if (bL == bR) {
for (int i = l; i <= r; i++) {
int add = all[i + 1] - all[i];
if (small[i] == 0) {
large[bL] += add;
}
++small[i];
}
} else {
for (int i = l; i < (bL + 1) * B; i++) {
int add = all[i + 1] - all[i];
if (small[i] == 0) {
large[bL] += add;
}
++small[i];
}
for (int i = bL + 1; i < bR; i++) {
++couches[i];
}
for (int i = bR * B; i <= r; i++) {
int add = all[i + 1] - all[i];
if (small[i] == 0) {
large[bR] += add;
}
++small[i];
}
}
};
auto Delete = [&](int l, int r) {
int bL = l / B;
int bR = r / B;
if (bL == bR) {
for (int i = l; i <= r; i++) {
int add = all[i + 1] - all[i];
if (small[i] == 1) {
large[bL] -= add;
}
--small[i];
}
} else {
for (int i = l; i < (bL + 1) * B; i++) {
int add = all[i + 1] - all[i];
if (small[i] == 1) {
large[bL] -= add;
}
--small[i];
}
for (int i = bL + 1; i < bR; i++) {
--couches[i];
}
for (int i = bR * B; i <= r; i++) {
int add = all[i + 1] - all[i];
if (small[i] == 1) {
large[bR] -= add;
}
--small[i];
}
}
};
auto Compute = [&]() {
int ans = 0;
for (int i = 0; i < number_of_blocks; i++) {
if (couches[i] > 0) {
assert(i != number_of_blocks - 1);
ans += all[(i + 1) * B] - all[i * B];
} else {
ans += large[i];
}
}
return ans;
};
vector<int> res(n);
function<void(int, int)> Dfs = [&](int u, int prev) {
if (u > 0) {
res[u] = Compute();
}
/*
debug(u);
debug(couches);
debug(large);
debug(small);
*/
for (auto v : adj[u]) {
if (v[0] == prev) {
continue;
}
Insert(v[1], v[2]);
Dfs(v[0], u);
Delete(v[1], v[2]);
}
};
Dfs(0, -1);
for (int i = 1; i < n; i++) {
cout << res[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... |