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 <stdio.h>
#include <stdlib.h>
#define N 100000
#define M ((N - 1) * 2)
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(M))) */
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ii[N - 1], jj[N - 1], xx[M], xx_[M], m;
int *eh[N], eo[N], ans[N];
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k)
if (xx[hh[j]] == xx[h])
j++;
else if (xx[hh[j]] < xx[h]) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
sort(hh, l, i);
l = k;
}
}
int st[N_ * 2], sum[N_ * 2], lz[N_ * 2], n_;
void pul(int i) {
st[i] = lz[i] ? sum[i] : st[i << 1 | 0] + st[i << 1 | 1];
}
void put(int i, int x) {
lz[i] += x, pul(i);
}
void pull(int i) {
while (i > 1)
pul(i >>= 1);
}
void build() {
int h, i;
n_ = 1;
while (n_ < m - 1)
n_ <<= 1;
for (h = 0; h + 1 < m; h++)
sum[n_ + h] = xx_[h + 1] - xx_[h];
for (i = n_ - 1; i > 0; i--)
sum[i] = sum[i << 1 | 0] + sum[i << 1 | 1], pul(i);
}
void update(int l, int r, int x) {
int l_ = l += n_, r_ = r += n_;
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
put(l++, x);
if ((r & 1) == 0)
put(r--, x);
}
pull(l_), pull(r_);
}
void dfs(int p, int i) {
int o;
ans[i] = st[1];
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h];
if (j != p) {
update(xx[h << 1 | 0], xx[h << 1 | 1] - 1, 1);
dfs(i, j);
update(xx[h << 1 | 0], xx[h << 1 | 1] - 1, -1);
}
}
}
int main() {
static int hh[(N - 1) * 2];
int n, h, h_, i, j, x;
scanf("%d%*d", &n);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d%d%d", &i, &j, &xx[h << 1 | 0], &xx[h << 1 | 1]), i--, j--, xx[h << 1 | 1]++;
ii[h] = i, jj[h] = j;
append(i, h), append(j, h);
}
for (h = 0; h < (n - 1) * 2; h++)
hh[h] = h;
sort(hh, 0, (n - 1) * 2);
m = 0;
for (h = 0; h < (n - 1) * 2; ) {
xx_[m] = x = xx[hh[h]];
while (h < (n - 1) * 2 && xx[h_ = hh[h]] == x)
xx[h_] = m, h++;
m++;
}
build();
dfs(-1, 0);
for (i = 1; i < n; i++)
printf("%d\n", ans[i]);
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:20:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
20 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:102:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%d%*d", &n);
| ^~~~~~~~~~~~~~~~~~
Main.c:106:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | scanf("%d%d%d%d", &i, &j, &xx[h << 1 | 0], &xx[h << 1 | 1]), i--, j--, xx[h << 1 | 1]++;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |