답안 #673689

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
673689 2022-12-21T17:01:53 Z rainboy Two Dishes (JOI19_dishes) C
100 / 100
6075 ms 226264 KB
#include <stdio.h>
#include <stdlib.h>

#define N	1000000
#define M	1000000
#define N_	(1 << 20)	/* N_ = pow2(ceil(log2(M + 1))) */
#define INF	0x3f3f3f3f3f3f3f3fLL

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

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

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

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

long long st[N_ * 2], lz1[N_], lz2[N_]; int h_, n_;

void build(int n) {
	int i;

	h_ = 0;
	while (1 << h_ < n)
		h_++;
	n_ = 1 << h_;
	for (i = 1; i < n_ * 2; i++)
		st[i] = -INF;
	for (i = 1; i < n_; i++)
		lz1[i] = -INF, lz2[i] = 0;
}

void put(int i, long long x, long long y) {
	st[i] = max(st[i], x);
	if (st[i] != -INF)
		st[i] += y;
	if (i < n_)
		lz1[i] = max(lz1[i], x - lz2[i]), lz2[i] += y;
}

void pus(int i) {
	if (lz1[i] != -INF || lz2[i] != 0)
		put(i << 1 | 0, lz1[i], lz2[i]), put(i << 1 | 1, lz1[i], lz2[i]), lz1[i] = -INF, lz2[i] = 0;
}

void pul(int i) {
	if (lz1[i] == -INF && lz2[i] == 0)
		st[i] = max(st[i << 1 | 0], st[i << 1 | 1]);
}

void push(int i) {
	int h;

	for (h = h_; h > 0; h--)
		pus(i >> h);
}

void pull(int i) {
	while (i > 1)
		pul(i >>= 1);
}

void update(int l, int r, long long x, long long y) {
	int l_ = l += n_, r_ = r += n_;

	push(l_), push(r_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			put(l++, x, y);
		if ((r & 1) == 0)
			put(r--, x, y);
	}
	pull(l_), pull(r_);
}

long long query(int i) {
	push(i += n_);
	return st[i];
}

int main() {
	static long long aa[N], ss[N], bb[M], tt[M];
	static int pp[N], qq[M];
	int n, m, i, j, v, o, lower, upper;
	long long x;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%lld%lld%d", &aa[i], &ss[i], &pp[i]);
	for (i = 1; i < n; i++)
		aa[i] += aa[i - 1];
	for (j = 0; j < m; j++)
		scanf("%lld%lld%d", &bb[j], &tt[j], &qq[j]);
	for (j = 1; j < m; j++)
		bb[j] += bb[j - 1];
	for (i = 0; i < n; i++) {
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
		ev[i] = (int *) malloc(2 * sizeof *ev[i]);
	}
	x = 0;
	for (i = 0; i < n; i++) {
		if (aa[i] > ss[i])
			continue;
		if (aa[i] + bb[m - 1] <= ss[i]) {
			x += pp[i];
			continue;
		}
		lower = -1, upper = m - 1;
		while (upper - lower > 1) {
			j = (lower + upper) / 2;
			if (aa[i] + bb[j] <= ss[i])
				lower = j;
			else
				upper = j;
		}
		j = upper;
		append(i, j, pp[i]);
	}
	for (j = 0; j < m; j++) {
		if (bb[j] > tt[j])
			continue;
		x += qq[j];
		if (aa[n - 1] + bb[j] <= tt[j])
			continue;
		lower = -1, upper = n - 1;
		while (upper - lower > 1) {
			i = (lower + upper) / 2;
			if (aa[i] + bb[j] <= tt[j])
				lower = i;
			else
				upper = i;
		}
		i = upper;
		append(i, j, -qq[j]);
	}
	build(m + 1);
	update(0, m, x, 0);
	for (i = 0; i < n; i++) {
		for (o = eo[i]; o--; ) {
			j = ej[i][o], v = ev[i][o];
			update(0, j, -INF, v);
		}
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			update(j + 1, m, query(j), 0);
		}
	}
	printf("%lld\n", query(m));
	return 0;
}

Compilation message

dishes.c: In function 'append':
dishes.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   16 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
dishes.c: In function 'main':
dishes.c:92:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
dishes.c:94:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |   scanf("%lld%lld%d", &aa[i], &ss[i], &pp[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dishes.c:98:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |   scanf("%lld%lld%d", &bb[j], &tt[j], &qq[j]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 596 ms 37584 KB Output is correct
2 Correct 617 ms 36876 KB Output is correct
3 Correct 171 ms 36724 KB Output is correct
4 Correct 504 ms 36684 KB Output is correct
5 Correct 0 ms 292 KB Output is correct
6 Correct 501 ms 36632 KB Output is correct
7 Correct 105 ms 16980 KB Output is correct
8 Correct 92 ms 24564 KB Output is correct
9 Correct 184 ms 36428 KB Output is correct
10 Correct 590 ms 37308 KB Output is correct
11 Correct 164 ms 36440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 364 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 296 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 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 364 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 296 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 0 ms 340 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 724 KB Output is correct
19 Correct 4 ms 688 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 680 KB Output is correct
22 Correct 4 ms 724 KB Output is correct
23 Correct 4 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 364 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 296 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 0 ms 340 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 724 KB Output is correct
19 Correct 4 ms 688 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 680 KB Output is correct
22 Correct 4 ms 724 KB Output is correct
23 Correct 4 ms 724 KB Output is correct
24 Correct 485 ms 37388 KB Output is correct
25 Correct 438 ms 38248 KB Output is correct
26 Correct 431 ms 37196 KB Output is correct
27 Correct 400 ms 37316 KB Output is correct
28 Correct 322 ms 37316 KB Output is correct
29 Correct 164 ms 36512 KB Output is correct
30 Correct 811 ms 37352 KB Output is correct
31 Correct 181 ms 17756 KB Output is correct
32 Correct 84 ms 24992 KB Output is correct
33 Correct 483 ms 36728 KB Output is correct
34 Correct 675 ms 37332 KB Output is correct
35 Correct 785 ms 37192 KB Output is correct
36 Correct 720 ms 37316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 364 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 296 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 0 ms 340 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 724 KB Output is correct
19 Correct 4 ms 688 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 680 KB Output is correct
22 Correct 4 ms 724 KB Output is correct
23 Correct 4 ms 724 KB Output is correct
24 Correct 485 ms 37388 KB Output is correct
25 Correct 438 ms 38248 KB Output is correct
26 Correct 431 ms 37196 KB Output is correct
27 Correct 400 ms 37316 KB Output is correct
28 Correct 322 ms 37316 KB Output is correct
29 Correct 164 ms 36512 KB Output is correct
30 Correct 811 ms 37352 KB Output is correct
31 Correct 181 ms 17756 KB Output is correct
32 Correct 84 ms 24992 KB Output is correct
33 Correct 483 ms 36728 KB Output is correct
34 Correct 675 ms 37332 KB Output is correct
35 Correct 785 ms 37192 KB Output is correct
36 Correct 720 ms 37316 KB Output is correct
37 Correct 444 ms 37260 KB Output is correct
38 Correct 413 ms 37244 KB Output is correct
39 Correct 653 ms 37184 KB Output is correct
40 Correct 611 ms 37184 KB Output is correct
41 Correct 1 ms 300 KB Output is correct
42 Correct 836 ms 37352 KB Output is correct
43 Correct 510 ms 36552 KB Output is correct
44 Correct 703 ms 37156 KB Output is correct
45 Correct 945 ms 37292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 364 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 296 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 0 ms 340 KB Output is correct
17 Correct 3 ms 724 KB Output is correct
18 Correct 3 ms 724 KB Output is correct
19 Correct 4 ms 688 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 4 ms 680 KB Output is correct
22 Correct 4 ms 724 KB Output is correct
23 Correct 4 ms 724 KB Output is correct
24 Correct 485 ms 37388 KB Output is correct
25 Correct 438 ms 38248 KB Output is correct
26 Correct 431 ms 37196 KB Output is correct
27 Correct 400 ms 37316 KB Output is correct
28 Correct 322 ms 37316 KB Output is correct
29 Correct 164 ms 36512 KB Output is correct
30 Correct 811 ms 37352 KB Output is correct
31 Correct 181 ms 17756 KB Output is correct
32 Correct 84 ms 24992 KB Output is correct
33 Correct 483 ms 36728 KB Output is correct
34 Correct 675 ms 37332 KB Output is correct
35 Correct 785 ms 37192 KB Output is correct
36 Correct 720 ms 37316 KB Output is correct
37 Correct 444 ms 37260 KB Output is correct
38 Correct 413 ms 37244 KB Output is correct
39 Correct 653 ms 37184 KB Output is correct
40 Correct 611 ms 37184 KB Output is correct
41 Correct 1 ms 300 KB Output is correct
42 Correct 836 ms 37352 KB Output is correct
43 Correct 510 ms 36552 KB Output is correct
44 Correct 703 ms 37156 KB Output is correct
45 Correct 945 ms 37292 KB Output is correct
46 Correct 2452 ms 154972 KB Output is correct
47 Correct 2474 ms 154936 KB Output is correct
48 Correct 3842 ms 154808 KB Output is correct
49 Correct 3694 ms 154764 KB Output is correct
50 Correct 6075 ms 155688 KB Output is correct
51 Correct 3246 ms 148216 KB Output is correct
52 Correct 4275 ms 153616 KB Output is correct
53 Correct 5715 ms 155300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 596 ms 37584 KB Output is correct
2 Correct 617 ms 36876 KB Output is correct
3 Correct 171 ms 36724 KB Output is correct
4 Correct 504 ms 36684 KB Output is correct
5 Correct 0 ms 292 KB Output is correct
6 Correct 501 ms 36632 KB Output is correct
7 Correct 105 ms 16980 KB Output is correct
8 Correct 92 ms 24564 KB Output is correct
9 Correct 184 ms 36428 KB Output is correct
10 Correct 590 ms 37308 KB Output is correct
11 Correct 164 ms 36440 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 364 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 296 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 3 ms 724 KB Output is correct
30 Correct 4 ms 688 KB Output is correct
31 Correct 3 ms 724 KB Output is correct
32 Correct 4 ms 680 KB Output is correct
33 Correct 4 ms 724 KB Output is correct
34 Correct 4 ms 724 KB Output is correct
35 Correct 485 ms 37388 KB Output is correct
36 Correct 438 ms 38248 KB Output is correct
37 Correct 431 ms 37196 KB Output is correct
38 Correct 400 ms 37316 KB Output is correct
39 Correct 322 ms 37316 KB Output is correct
40 Correct 164 ms 36512 KB Output is correct
41 Correct 811 ms 37352 KB Output is correct
42 Correct 181 ms 17756 KB Output is correct
43 Correct 84 ms 24992 KB Output is correct
44 Correct 483 ms 36728 KB Output is correct
45 Correct 675 ms 37332 KB Output is correct
46 Correct 785 ms 37192 KB Output is correct
47 Correct 720 ms 37316 KB Output is correct
48 Correct 444 ms 37260 KB Output is correct
49 Correct 413 ms 37244 KB Output is correct
50 Correct 653 ms 37184 KB Output is correct
51 Correct 611 ms 37184 KB Output is correct
52 Correct 1 ms 300 KB Output is correct
53 Correct 836 ms 37352 KB Output is correct
54 Correct 510 ms 36552 KB Output is correct
55 Correct 703 ms 37156 KB Output is correct
56 Correct 945 ms 37292 KB Output is correct
57 Correct 443 ms 35140 KB Output is correct
58 Correct 420 ms 35140 KB Output is correct
59 Correct 722 ms 35108 KB Output is correct
60 Correct 672 ms 35136 KB Output is correct
61 Correct 803 ms 35376 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 855 ms 35300 KB Output is correct
64 Correct 545 ms 35000 KB Output is correct
65 Correct 634 ms 34536 KB Output is correct
66 Correct 738 ms 35224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 596 ms 37584 KB Output is correct
2 Correct 617 ms 36876 KB Output is correct
3 Correct 171 ms 36724 KB Output is correct
4 Correct 504 ms 36684 KB Output is correct
5 Correct 0 ms 292 KB Output is correct
6 Correct 501 ms 36632 KB Output is correct
7 Correct 105 ms 16980 KB Output is correct
8 Correct 92 ms 24564 KB Output is correct
9 Correct 184 ms 36428 KB Output is correct
10 Correct 590 ms 37308 KB Output is correct
11 Correct 164 ms 36440 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 292 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 364 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 296 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 3 ms 724 KB Output is correct
30 Correct 4 ms 688 KB Output is correct
31 Correct 3 ms 724 KB Output is correct
32 Correct 4 ms 680 KB Output is correct
33 Correct 4 ms 724 KB Output is correct
34 Correct 4 ms 724 KB Output is correct
35 Correct 485 ms 37388 KB Output is correct
36 Correct 438 ms 38248 KB Output is correct
37 Correct 431 ms 37196 KB Output is correct
38 Correct 400 ms 37316 KB Output is correct
39 Correct 322 ms 37316 KB Output is correct
40 Correct 164 ms 36512 KB Output is correct
41 Correct 811 ms 37352 KB Output is correct
42 Correct 181 ms 17756 KB Output is correct
43 Correct 84 ms 24992 KB Output is correct
44 Correct 483 ms 36728 KB Output is correct
45 Correct 675 ms 37332 KB Output is correct
46 Correct 785 ms 37192 KB Output is correct
47 Correct 720 ms 37316 KB Output is correct
48 Correct 444 ms 37260 KB Output is correct
49 Correct 413 ms 37244 KB Output is correct
50 Correct 653 ms 37184 KB Output is correct
51 Correct 611 ms 37184 KB Output is correct
52 Correct 1 ms 300 KB Output is correct
53 Correct 836 ms 37352 KB Output is correct
54 Correct 510 ms 36552 KB Output is correct
55 Correct 703 ms 37156 KB Output is correct
56 Correct 945 ms 37292 KB Output is correct
57 Correct 2452 ms 154972 KB Output is correct
58 Correct 2474 ms 154936 KB Output is correct
59 Correct 3842 ms 154808 KB Output is correct
60 Correct 3694 ms 154764 KB Output is correct
61 Correct 6075 ms 155688 KB Output is correct
62 Correct 3246 ms 148216 KB Output is correct
63 Correct 4275 ms 153616 KB Output is correct
64 Correct 5715 ms 155300 KB Output is correct
65 Correct 443 ms 35140 KB Output is correct
66 Correct 420 ms 35140 KB Output is correct
67 Correct 722 ms 35108 KB Output is correct
68 Correct 672 ms 35136 KB Output is correct
69 Correct 803 ms 35376 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 855 ms 35300 KB Output is correct
72 Correct 545 ms 35000 KB Output is correct
73 Correct 634 ms 34536 KB Output is correct
74 Correct 738 ms 35224 KB Output is correct
75 Correct 2484 ms 154688 KB Output is correct
76 Correct 2183 ms 226264 KB Output is correct
77 Correct 3749 ms 213400 KB Output is correct
78 Correct 4229 ms 213376 KB Output is correct
79 Correct 5351 ms 225840 KB Output is correct
80 Correct 2969 ms 220804 KB Output is correct
81 Correct 3774 ms 212172 KB Output is correct
82 Correct 5136 ms 193728 KB Output is correct
83 Correct 5015 ms 213932 KB Output is correct