답안 #666775

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
666775 2022-11-29T16:41:56 Z rainboy Worst Reporter 4 (JOI21_worst_reporter4) C
100 / 100
339 ms 87276 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define LN	18	/* LN = ceil(log2(N)) */
#define N_	(N * (LN + 1) + 1)

long long max(long long a, long long b) { return a > b ? a : b; }

unsigned int X = 12345;

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

int aa[N], cc[N], n;

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)
			if (aa[ii[j]] == aa[i_])
				j++;
			else if (aa[ii[j]] < aa[i_]) {
				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;
	}
}

int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int ll[N_], rr[N_], _; long long xx[N_];

int add(int t, int l, int r, int i, int x) {
	int t_ = t ? t : ++_, m;

	xx[t_] += x;
	if (r - l > 1) {
		m = (l + r) / 2;
		if (i < m)
			ll[t_] = add(ll[t_], l, m, i, x);
		else
			rr[t_] = add(rr[t_], m, r, i, x);
	}
	return t_;
}

int x;

int remove_(int t, int l, int r, int i) {
	int m;

	if (l >= i || x == 0 || !t)
		return t;
	if (r <= i && x >= xx[t]) {
		x -= xx[t];
		return 0;
	}
	if (r - l == 1) {
		xx[t] -= x, x = 0;
		return t;
	}
	m = (l + r) / 2;
	rr[t] = remove_(rr[t], m, r, i), ll[t] = remove_(ll[t], l, m, i);
	xx[t] = xx[ll[t]] + xx[rr[t]];
	return ll[t] || rr[t] ? t : 0;
}

long long query(int t, int l, int r, int i) {
	int m;

	if (r <= i || !t)
		return 0;
	if (l >= i)
		return xx[t];
	m = (l + r) / 2;
	return query(ll[t], l, m, i) + query(rr[t], m, r, i);
}

int merge(int t1, int t2, int l, int r) {
	int m;

	if (!t1)
		return t2;
	else if (!t2)
		return t1;
	xx[t1] += xx[t2];
	if (r - l > 1) {
		m = (l + r) / 2;
		ll[t1] = merge(ll[t1], ll[t2], l, m), rr[t1] = merge(rr[t1], rr[t2], m, r);
	}
	return t1;
}

char visited[N]; int tt[N];

void dfs(int p, int i) {
	int o;

	if (p != -1)
		visited[i] = 1;
	tt[i] = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && visited[j] != -1) {
			dfs(i, j);
			tt[i] = merge(tt[i], tt[j], 0, n);
		}
	}
	if (p != -1)
		x = cc[i], tt[i] = add(remove_(tt[i], 0, n, aa[i]), 0, n, aa[i], cc[i]);
}

int main() {
	static int pp[N], ii[N];
	static long long cc_[N];
	int i, j, k, a, t;
	long long ans, x;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	ans = 0;
	for (i = 0; i < n; i++) {
		scanf("%d%d%d", &pp[i], &aa[i], &cc[i]), pp[i]--;
		ans += cc[i];
		append(pp[i], i);
	}
	for (i = 0; i < n; i++)
		ii[i] = i;
	sort(ii, 0, n);
	for (i = 0, a = 0; i < n; i++)
		aa[ii[i]] = i + 1 == n || aa[ii[i + 1]] != aa[ii[i]] ? a++ : a;
	for (i = 0; i < n; i++)
		if (!visited[i]) {
			j = i;
			while (!visited[j])
				visited[j] = 1, j = pp[j];
			k = j;
			do
				visited[k] = -1, k = pp[k];
			while (k != j);
			t = 0;
			do {
				dfs(-1, k);
				t = merge(t, tt[k], 0, n);
				cc_[aa[k]] += cc[k];
				k = pp[k];
			} while (k != j);
			x = xx[t];
			do {
				a = aa[k];
				if (cc_[a] != 0)
					x = max(x, query(t, 0, n, a) + cc_[a]), cc_[a] = 0;
				k = pp[k];
			} while (k != j);
			ans -= x;
		}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

worst_reporter2.c: In function 'append':
worst_reporter2.c:42:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   42 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
worst_reporter2.c: In function 'main':
worst_reporter2.c:136:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
worst_reporter2.c:141:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |   scanf("%d%d%d", &pp[i], &aa[i], &cc[i]), pp[i]--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 5 ms 1620 KB Output is correct
6 Correct 4 ms 1364 KB Output is correct
7 Correct 4 ms 1236 KB Output is correct
8 Correct 5 ms 1620 KB Output is correct
9 Correct 4 ms 1364 KB Output is correct
10 Correct 4 ms 1236 KB Output is correct
11 Correct 3 ms 1108 KB Output is correct
12 Correct 6 ms 1492 KB Output is correct
13 Correct 4 ms 980 KB Output is correct
14 Correct 4 ms 1364 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 5 ms 1492 KB Output is correct
17 Correct 4 ms 1364 KB Output is correct
18 Correct 4 ms 1108 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 3 ms 1364 KB Output is correct
22 Correct 5 ms 1620 KB Output is correct
23 Correct 5 ms 1620 KB Output is correct
24 Correct 5 ms 1364 KB Output is correct
25 Correct 4 ms 1364 KB Output is correct
26 Correct 5 ms 2004 KB Output is correct
27 Correct 4 ms 1488 KB Output is correct
28 Correct 4 ms 1364 KB Output is correct
29 Correct 5 ms 1236 KB Output is correct
30 Correct 4 ms 932 KB Output is correct
31 Correct 4 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 5 ms 1620 KB Output is correct
6 Correct 4 ms 1364 KB Output is correct
7 Correct 4 ms 1236 KB Output is correct
8 Correct 5 ms 1620 KB Output is correct
9 Correct 4 ms 1364 KB Output is correct
10 Correct 4 ms 1236 KB Output is correct
11 Correct 3 ms 1108 KB Output is correct
12 Correct 6 ms 1492 KB Output is correct
13 Correct 4 ms 980 KB Output is correct
14 Correct 4 ms 1364 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 5 ms 1492 KB Output is correct
17 Correct 4 ms 1364 KB Output is correct
18 Correct 4 ms 1108 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 3 ms 1364 KB Output is correct
22 Correct 5 ms 1620 KB Output is correct
23 Correct 5 ms 1620 KB Output is correct
24 Correct 5 ms 1364 KB Output is correct
25 Correct 4 ms 1364 KB Output is correct
26 Correct 5 ms 2004 KB Output is correct
27 Correct 4 ms 1488 KB Output is correct
28 Correct 4 ms 1364 KB Output is correct
29 Correct 5 ms 1236 KB Output is correct
30 Correct 4 ms 932 KB Output is correct
31 Correct 4 ms 980 KB Output is correct
32 Correct 5 ms 1620 KB Output is correct
33 Correct 298 ms 67328 KB Output is correct
34 Correct 235 ms 56740 KB Output is correct
35 Correct 319 ms 67340 KB Output is correct
36 Correct 244 ms 56780 KB Output is correct
37 Correct 197 ms 45848 KB Output is correct
38 Correct 171 ms 41324 KB Output is correct
39 Correct 233 ms 55740 KB Output is correct
40 Correct 175 ms 33064 KB Output is correct
41 Correct 134 ms 28592 KB Output is correct
42 Correct 220 ms 49536 KB Output is correct
43 Correct 169 ms 26400 KB Output is correct
44 Correct 249 ms 65832 KB Output is correct
45 Correct 191 ms 54444 KB Output is correct
46 Correct 137 ms 40780 KB Output is correct
47 Correct 256 ms 54612 KB Output is correct
48 Correct 173 ms 49748 KB Output is correct
49 Correct 142 ms 49584 KB Output is correct
50 Correct 250 ms 71596 KB Output is correct
51 Correct 173 ms 71500 KB Output is correct
52 Correct 276 ms 53036 KB Output is correct
53 Correct 193 ms 50180 KB Output is correct
54 Correct 186 ms 87276 KB Output is correct
55 Correct 201 ms 53688 KB Output is correct
56 Correct 207 ms 44436 KB Output is correct
57 Correct 185 ms 40644 KB Output is correct
58 Correct 178 ms 27160 KB Output is correct
59 Correct 189 ms 27160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 5 ms 1620 KB Output is correct
6 Correct 4 ms 1364 KB Output is correct
7 Correct 4 ms 1236 KB Output is correct
8 Correct 5 ms 1620 KB Output is correct
9 Correct 4 ms 1364 KB Output is correct
10 Correct 4 ms 1236 KB Output is correct
11 Correct 3 ms 1108 KB Output is correct
12 Correct 6 ms 1492 KB Output is correct
13 Correct 4 ms 980 KB Output is correct
14 Correct 4 ms 1364 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 5 ms 1492 KB Output is correct
17 Correct 4 ms 1364 KB Output is correct
18 Correct 4 ms 1108 KB Output is correct
19 Correct 5 ms 1492 KB Output is correct
20 Correct 4 ms 1364 KB Output is correct
21 Correct 3 ms 1364 KB Output is correct
22 Correct 5 ms 1620 KB Output is correct
23 Correct 5 ms 1620 KB Output is correct
24 Correct 5 ms 1364 KB Output is correct
25 Correct 4 ms 1364 KB Output is correct
26 Correct 5 ms 2004 KB Output is correct
27 Correct 4 ms 1488 KB Output is correct
28 Correct 4 ms 1364 KB Output is correct
29 Correct 5 ms 1236 KB Output is correct
30 Correct 4 ms 932 KB Output is correct
31 Correct 4 ms 980 KB Output is correct
32 Correct 5 ms 1620 KB Output is correct
33 Correct 298 ms 67328 KB Output is correct
34 Correct 235 ms 56740 KB Output is correct
35 Correct 319 ms 67340 KB Output is correct
36 Correct 244 ms 56780 KB Output is correct
37 Correct 197 ms 45848 KB Output is correct
38 Correct 171 ms 41324 KB Output is correct
39 Correct 233 ms 55740 KB Output is correct
40 Correct 175 ms 33064 KB Output is correct
41 Correct 134 ms 28592 KB Output is correct
42 Correct 220 ms 49536 KB Output is correct
43 Correct 169 ms 26400 KB Output is correct
44 Correct 249 ms 65832 KB Output is correct
45 Correct 191 ms 54444 KB Output is correct
46 Correct 137 ms 40780 KB Output is correct
47 Correct 256 ms 54612 KB Output is correct
48 Correct 173 ms 49748 KB Output is correct
49 Correct 142 ms 49584 KB Output is correct
50 Correct 250 ms 71596 KB Output is correct
51 Correct 173 ms 71500 KB Output is correct
52 Correct 276 ms 53036 KB Output is correct
53 Correct 193 ms 50180 KB Output is correct
54 Correct 186 ms 87276 KB Output is correct
55 Correct 201 ms 53688 KB Output is correct
56 Correct 207 ms 44436 KB Output is correct
57 Correct 185 ms 40644 KB Output is correct
58 Correct 178 ms 27160 KB Output is correct
59 Correct 189 ms 27160 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 0 ms 340 KB Output is correct
62 Correct 0 ms 340 KB Output is correct
63 Correct 0 ms 296 KB Output is correct
64 Correct 339 ms 69604 KB Output is correct
65 Correct 246 ms 55312 KB Output is correct
66 Correct 308 ms 69876 KB Output is correct
67 Correct 254 ms 55108 KB Output is correct
68 Correct 194 ms 42788 KB Output is correct
69 Correct 170 ms 39048 KB Output is correct
70 Correct 160 ms 19588 KB Output is correct
71 Correct 135 ms 18196 KB Output is correct
72 Correct 153 ms 19624 KB Output is correct
73 Correct 138 ms 18264 KB Output is correct
74 Correct 298 ms 48972 KB Output is correct
75 Correct 204 ms 47364 KB Output is correct
76 Correct 180 ms 47228 KB Output is correct
77 Correct 252 ms 49484 KB Output is correct
78 Correct 162 ms 47820 KB Output is correct
79 Correct 289 ms 46904 KB Output is correct
80 Correct 200 ms 40084 KB Output is correct
81 Correct 160 ms 34684 KB Output is correct
82 Correct 154 ms 19804 KB Output is correct
83 Correct 119 ms 19584 KB Output is correct
84 Correct 270 ms 40244 KB Output is correct
85 Correct 282 ms 40060 KB Output is correct
86 Correct 261 ms 39244 KB Output is correct
87 Correct 274 ms 40076 KB Output is correct
88 Correct 259 ms 40052 KB Output is correct