답안 #1060236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1060236 2024-08-15T11:51:27 Z rainboy Petrol stations (CEOI24_stations) C
100 / 100
285 ms 15444 KB
#include <stdio.h>
#include <stdlib.h>

#define N	70000

unsigned int Z = 12345;

int rand_() {
	return (Z *= 3) >> 1;
}

int *ej[N], *ew[N], eo[N], n, d_;

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;
}

void detach(int i, int j) {
	int o;

	for (o = eo[i]; o--; )
		if (ej[i][o] == j) {
			eo[i]--, ej[i][o] = ej[i][eo[i]], ew[i][o] = ew[i][eo[i]];
			return;
		}
}

int sz[N], n_, c;

void dfs1(int p, int i) {
	int o, centroid;

	sz[i] = 1, centroid = 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs1(i, j);
			sz[i] += sz[j];
			if (sz[j] * 2 > n_)
				centroid = 0;
		}
	}
	if ((n_ - sz[i]) * 2 > n_)
		centroid = 0;
	if (centroid)
		c = i;
}

int ii[N], qu[N], cnt;
int pp[N], rr[N]; long long dd[N]; int kk[N];

void dfs2(int p, int i, int r, long long d) {
	int o, h, lower, upper;

	qu[cnt++] = i;
	ii[n_++] = i;
	pp[i] = p, rr[i] = r, dd[i] = d, sz[i] = 1, kk[i] = 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o], w = ew[i][o];

		if (j != p) {
			dfs2(i, j, p == -1 ? j : r, d + w);
			sz[i] += sz[j];
		}
	}
	lower = -1, upper = cnt - 1;
	while (upper - lower > 1) {
		h = (lower + upper) / 2;
		if (dd[i] - dd[qu[h]] > d_)
			lower = h;
		else
			upper = h;
	}
	if (lower != -1)
		kk[qu[upper]] += kk[i];
	cnt--;
}

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = dd[ii[j]] != dd[i_] ? (dd[ii[j]] < dd[i_] ? -1 : 1) : sz[i_] - sz[ii[j]];

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

long long ans[N]; int kk_[N], ss[N];

void dfs3(int p, int i) {
	int o, h, lower, upper;

	qu[cnt++] = i;
	lower = -1, upper = cnt - 1;
	while (upper - lower > 1) {
		h = (lower + upper) / 2;
		if (dd[i] - dd[qu[h]] > d_)
			lower = h;
		else
			upper = h;
	}
	kk_[i] += ss[qu[upper]];
	ss[i] = (p == -1 ? 0 : ss[p]) + kk_[i];
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			kk_[j] -= ss[qu[upper]], dfs3(i, j);
	}
	if (p != -1)
		ans[p] += (long long) kk_[i] * sz[i];
	cnt--;
}

void cd(int n, int i) {
	static int kk1[N];
	int r, h, h1, h2, j, o, k;

	n_ = n, dfs1(-1, i), r = c;
	n_ = 0, dfs2(-1, r, r, 0);
	for (h = 1; h < n; h++) {
		i = ii[h];
		ans[i] += (long long) (kk[i] - 1) * (n - sz[rr[i]]);
	}
	sort(ii, 0, n);
	kk1[r] = 0;
	for (o = eo[r]; o--; ) {
		i = ej[r][o];
		kk1[i] = 0;
	}
	for (h1 = 0, h2 = n - 1, k = 0; h1 < n; h1++) {
		i = ii[h1];
		while (h2 >= 0 && dd[i] + dd[j = ii[h2]] > d_) {
			if (dd[j] <= d_)
				k += kk[j], kk1[rr[j]] += kk[j];
			h2--;
		}
		kk_[i] = k - kk1[rr[i]];
	}
	for (h = n - 1; h > 0; h--) {
		i = ii[h];
		kk_[i] -= kk_[pp[i]];
	}
	dfs3(-1, r);
	for (o = eo[r]; o--; ) {
		i = ej[r][o];
		detach(i, r), cd(sz[i], i);
	}
}

int main() {
	int h, i, j, w;

	scanf("%d%d", &n, &d_);
	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++) {
		scanf("%d%d%d", &i, &j, &w);
		append(i, j, w), append(j, i, w);
	}
	cd(n, 0);
	for (i = 0; i < n; i++)
		printf("%lld\n", ans[i]);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   17 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
Main.c: In function 'main':
Main.c:174:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |  scanf("%d%d", &n, &d_);
      |  ^~~~~~~~~~~~~~~~~~~~~~
Main.c:180:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  180 |   scanf("%d%d%d", &i, &j, &w);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 268 ms 15164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 268 ms 15164 KB Output is correct
5 Correct 284 ms 15444 KB Output is correct
6 Correct 246 ms 14932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 177 ms 10924 KB Output is correct
4 Correct 285 ms 14932 KB Output is correct
5 Correct 237 ms 15404 KB Output is correct
6 Correct 1 ms 2484 KB Output is correct
7 Correct 200 ms 11100 KB Output is correct
8 Correct 195 ms 11092 KB Output is correct
9 Correct 190 ms 11092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 177 ms 10924 KB Output is correct
4 Correct 285 ms 14932 KB Output is correct
5 Correct 237 ms 15404 KB Output is correct
6 Correct 1 ms 2484 KB Output is correct
7 Correct 200 ms 11100 KB Output is correct
8 Correct 195 ms 11092 KB Output is correct
9 Correct 190 ms 11092 KB Output is correct
10 Correct 107 ms 11604 KB Output is correct
11 Correct 130 ms 11020 KB Output is correct
12 Correct 117 ms 11092 KB Output is correct
13 Correct 121 ms 11204 KB Output is correct
14 Correct 121 ms 11164 KB Output is correct
15 Correct 118 ms 11092 KB Output is correct
16 Correct 30 ms 11492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 0 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 268 ms 15164 KB Output is correct
17 Correct 284 ms 15444 KB Output is correct
18 Correct 246 ms 14932 KB Output is correct
19 Correct 177 ms 10924 KB Output is correct
20 Correct 285 ms 14932 KB Output is correct
21 Correct 237 ms 15404 KB Output is correct
22 Correct 1 ms 2484 KB Output is correct
23 Correct 200 ms 11100 KB Output is correct
24 Correct 195 ms 11092 KB Output is correct
25 Correct 190 ms 11092 KB Output is correct
26 Correct 107 ms 11604 KB Output is correct
27 Correct 130 ms 11020 KB Output is correct
28 Correct 117 ms 11092 KB Output is correct
29 Correct 121 ms 11204 KB Output is correct
30 Correct 121 ms 11164 KB Output is correct
31 Correct 118 ms 11092 KB Output is correct
32 Correct 30 ms 11492 KB Output is correct
33 Correct 158 ms 10852 KB Output is correct
34 Correct 119 ms 12356 KB Output is correct
35 Correct 132 ms 11604 KB Output is correct
36 Correct 137 ms 11604 KB Output is correct
37 Correct 116 ms 11252 KB Output is correct
38 Correct 121 ms 11256 KB Output is correct
39 Correct 142 ms 11348 KB Output is correct
40 Correct 43 ms 12004 KB Output is correct