Submission #670585

# Submission time Handle Problem Language Result Execution time Memory
670585 2022-12-09T15:34:46 Z rainboy Constellation 3 (JOI20_constellation3) C
100 / 100
282 ms 58764 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define M	200000

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

int *ej[N], eo[N], *eh1[N], eo1[N], *eh2[N], eo2[N], aa[N], n;
int jj[M], yy[M], cc[M], m;

void append(int **ej, int *eo, 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 ta[N], tb[N], qu[N], cnt;

void dfs1(int i) {
	static int time;
	int o;

	qu[cnt++] = i, ta[i] = time++;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs1(j);
	}
	for (o = eo1[i]; o--; ) {
		int h = eh1[i][o], lower, upper;

		lower = -1, upper = cnt - 1;
		while (upper - lower > 1) {
			int g = (lower + upper) / 2;

			if (aa[qu[g]] < yy[h])
				upper = g;
			else
				lower = g;
		}
		append(eh2, eo2, qu[upper], h);
	}
	tb[i] = time, cnt--;
}

long long ft[N];

void update(int i, int n, long long x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}

long long query(int i) {
	long long x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

long long dp[N];

void dfs2(int p, int i) {
	int o;
	long long x, y;

	x = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs2(i, j);
		x += dp[j];
	}
	y = 0;
	for (o = eo2[i]; o--; ) {
		int h = eh2[i][o];

		y = max(y, cc[h] - query(ta[jj[h]]) + (p == -1 ? 0 : query(ta[p])));
	}
	dp[i] = x + y;
	update(ta[i], n, y), update(tb[i], n, -y);
}

int main() {
	static int pp[N], qq[N];
	int h, i, j, r;
	long long sum;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	cnt = 0;
	for (i = 0; i < n; i++) {
		while (cnt && aa[qu[cnt - 1]] <= aa[i])
			cnt--;
		pp[i] = cnt ? qu[cnt - 1] : -1;
		qu[cnt++] = i;
	}
	cnt = 0;
	for (i = n - 1; i >= 0; i--) {
		while (cnt && aa[qu[cnt - 1]] < aa[i])
			cnt--;
		qq[i] = cnt ? qu[cnt - 1] : -1;
		qu[cnt++] = i;
	}
	for (i = 0; i < n; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		eh1[i] = (int *) malloc(2 * sizeof *eh1[i]);
		eh2[i] = (int *) malloc(2 * sizeof *eh2[i]);
	}
	r = -1;
	for (i = 0; i < n; i++)
		if (pp[i] == -1 && qq[i] == -1)
			r = i;
		else if (qq[i] == -1 || pp[i] != -1 && aa[pp[i]] <= aa[qq[i]])
			append(ej, eo, pp[i], i);
		else
			append(ej, eo, qq[i], i);
	scanf("%d", &m);
	sum = 0;
	for (h = 0; h < m; h++) {
		scanf("%d%d%d", &j, &yy[h], &cc[h]), j--;
		jj[h] = j;
		append(eh1, eo1, j, h);
		sum += cc[h];
	}
	cnt = 0;
	dfs1(r);
	dfs2(-1, r);
	printf("%lld\n", sum - dp[r]);
	return 0;
}

Compilation message

constellation3.c: In function 'append':
constellation3.c:15:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   15 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
constellation3.c: In function 'main':
constellation3.c:122:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  122 |   else if (qq[i] == -1 || pp[i] != -1 && aa[pp[i]] <= aa[qq[i]])
      |                           ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
constellation3.c:96:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
constellation3.c:98:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
constellation3.c:126:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |  scanf("%d", &m);
      |  ^~~~~~~~~~~~~~~
constellation3.c:129:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |   scanf("%d%d%d", &j, &yy[h], &cc[h]), j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Correct 0 ms 428 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Correct 0 ms 428 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 424 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 2 ms 688 KB Output is correct
26 Correct 2 ms 692 KB Output is correct
27 Correct 2 ms 692 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
29 Correct 2 ms 672 KB Output is correct
30 Correct 2 ms 724 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 2 ms 816 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 852 KB Output is correct
36 Correct 2 ms 804 KB Output is correct
37 Correct 2 ms 852 KB Output is correct
38 Correct 2 ms 852 KB Output is correct
39 Correct 2 ms 692 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 2 ms 684 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 424 KB Output is correct
18 Correct 0 ms 428 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 424 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 2 ms 688 KB Output is correct
26 Correct 2 ms 692 KB Output is correct
27 Correct 2 ms 692 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
29 Correct 2 ms 672 KB Output is correct
30 Correct 2 ms 724 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 852 KB Output is correct
33 Correct 2 ms 816 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 852 KB Output is correct
36 Correct 2 ms 804 KB Output is correct
37 Correct 2 ms 852 KB Output is correct
38 Correct 2 ms 852 KB Output is correct
39 Correct 2 ms 692 KB Output is correct
40 Correct 2 ms 852 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 2 ms 684 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 2 ms 724 KB Output is correct
45 Correct 195 ms 41256 KB Output is correct
46 Correct 189 ms 40904 KB Output is correct
47 Correct 188 ms 40492 KB Output is correct
48 Correct 238 ms 41152 KB Output is correct
49 Correct 185 ms 40260 KB Output is correct
50 Correct 180 ms 40096 KB Output is correct
51 Correct 194 ms 40168 KB Output is correct
52 Correct 191 ms 41124 KB Output is correct
53 Correct 187 ms 40872 KB Output is correct
54 Correct 282 ms 54496 KB Output is correct
55 Correct 262 ms 50408 KB Output is correct
56 Correct 282 ms 48088 KB Output is correct
57 Correct 247 ms 46660 KB Output is correct
58 Correct 222 ms 50032 KB Output is correct
59 Correct 208 ms 49868 KB Output is correct
60 Correct 155 ms 58764 KB Output is correct
61 Correct 194 ms 41428 KB Output is correct
62 Correct 255 ms 54892 KB Output is correct
63 Correct 184 ms 40928 KB Output is correct
64 Correct 189 ms 40100 KB Output is correct
65 Correct 251 ms 55304 KB Output is correct
66 Correct 182 ms 40572 KB Output is correct