# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
544773 | rainboy | Harbingers (CEOI09_harbingers) | C11 | 184 ms | 28988 KiB |
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 LN 16 /* LN = floor(log2(N - 1)) */
int min(int a, int b) { return a < b ? a : b; }
int *ej[N], *ew[N], eo[N];
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
int pp[N][LN + 1], dd[N], ss[N], vv[N]; long long xx[N], yy[N];
double cross(int i, int j, int k) {
return (double) (xx[j] - xx[i]) * (yy[k] - yy[i]) - (double) (xx[k] - xx[i]) * (yy[j] - yy[i]);
}
void dfs(int p, int i, int d, long long x) {
int o, h;
dd[i] = d, xx[i] = x;
if (i > 0) {
int j;
for (h = LN, j = p; h >= 0; h--) {
int j_ = pp[j][h];
if (j_ != -1 && pp[j_][0] != -1 && yy[j_] + (xx[i] - xx[j_]) * vv[i] >= yy[pp[j_][0]] + (xx[i] - xx[pp[j_][0]]) * vv[i])
j = j_;
}
if (pp[j][0] != -1 && yy[j] + (xx[i] - xx[j]) * vv[i] >= yy[pp[j][0]] + (xx[i] - xx[pp[j][0]]) * vv[i])
j = pp[j][0];
yy[i] = yy[j] + (xx[i] - xx[j]) * vv[i] + ss[i];
for (h = LN, j = p; h >= 0; h--) {
int j_ = pp[j][h];
if (j_ != -1 && pp[j_][0] != -1 && cross(pp[j_][0], j_, i) <= 0)
j = j_;
}
if (j != -1 && pp[j][0] != -1 && cross(pp[j][0], j, i) <= 0)
j = pp[j][0];
pp[i][0] = j;
} else
pp[i][0] = -1;
for (h = 0; h < LN; h++)
pp[i][h + 1] = pp[i][h] == -1 ? -1 : pp[pp[i][h]][h];
for (o = eo[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
if (j != p)
dfs(i, j, d + 1, x + w);
}
}
int main() {
int n, h, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
}
for (h = 0; h < n - 1; h++) {
int w;
scanf("%d%d%d", &i, &j, &w), i--, j--;
append(i, j, w), append(j, i, w);
}
for (i = 1; i < n; i++)
scanf("%d%d", &ss[i], &vv[i]);
dfs(-1, 0, 0, 0);
for (i = 1; i < n; i++)
printf("%lld ", yy[i]);
printf("\n");
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |