Submission #1050824

# Submission time Handle Problem Language Result Execution time Memory
1050824 2024-08-09T14:54:48 Z rainboy Tricks of the Trade (CEOI23_trade) C
50 / 100
1794 ms 111604 KB
#include <stdio.h>
#include <string.h>

#define N	250000
#define LN	18	/* LN = ceil(log2(N)) */
#define N_	(N * (LN + 1) + 1)
#define INF	0x3f3f3f3f3f3f3f3fLL

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

unsigned int Z = 12345;

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

long long xx[N + 1]; int yy[N], yy_[N], n, m, k;

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 (yy[ii[j]] == yy[i_])
				j++;
			else if (yy[ii[j]] < yy[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 ll[N_], rr[N_], kk[N_]; long long ss[N_];

int update(int t, int l, int r, int i) {
	static int _ = 1;
	int t_ = _++;

	ll[t_] = ll[t], rr[t_] = rr[t], kk[t_] = kk[t] + 1, ss[t_] = ss[t] + yy_[i];
	if (r - l > 1) {
		int m = (l + r) / 2;

		if (i < m)
			ll[t_] = update(ll[t_], l, m, i);
		else
			rr[t_] = update(rr[t_], m, r, i);
	}
	return t_;
}

long long s_; int y_;

void query(int t1, int t2, int l, int r, int k) {
	int m;

	if (r - l == 1) {
		s_ += (long long) yy_[l] * k, y_ = l;
		return;
	}
	m = (l + r) / 2;
	if (k <= kk[rr[t2]] - kk[rr[t1]])
		query(rr[t1], rr[t2], m, r, k);
	else
		s_ += ss[rr[t2]] - ss[rr[t1]], query(ll[t1], ll[t2], l, m, k - (kk[rr[t2]] - kk[rr[t1]]));
}

int tt[N + 1];

long long f(int i, int j) {
	s_ = 0, y_ = -1, query(tt[i], tt[j], 0, m, k);
	return s_ - (xx[j] - xx[i]);
}

int find_j(int i1, int i2) {
	int lower = i2 + k - 1, upper = n + 1;

	while (upper - lower > 1) {
		int j = (lower + upper) / 2;

		if (f(i1, j) <= f(i2, j))
			upper = j;
		else
			lower = j;
	}
	return upper;
}

int find_i(int j1, int j2) {
	int lower = -1, upper = j1 - k + 1;

	while (upper - lower > 1) {
		int i = (lower + upper) / 2;

		if (f(i, j1) >= f(i, j2))
			lower = i;
		else
			upper = i;
	}
	return lower;
}

int st[N_ * 2], n_;

void build() {
	n_ = 1;
	while (n_ < n)
		n_ <<= 1;
	memset(st, 0x3f, n_ * 2 * sizeof *st);
}

void update_(int l, int r, int y) {
	for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			st[l] = min(st[l], y), l++;
		if ((r & 1) == 0)
			st[r] = min(st[r], y), r--;
	}
}

int main() {
	static int ii[N + 1], jj[N + 1], qu[N + 1], ii_[N + 1], jj_[N + 1];
	static long long xxj[N + 1], xxi[N + 1];
	static char cc[N + 1];
	int head, cnt, i, j, y;
	long long x_;

	scanf("%d%d", &n, &k);
	for (i = 1; i <= n; i++)
		scanf("%lld", &xx[i]), xx[i] += xx[i - 1];
	for (i = 0; i < n; i++)
		scanf("%d", &yy[i]);
	for (i = 0; i < n; i++)
		ii[i] = i;
	sort(ii, 0, n);
	for (i = 0, m = 0; i < n; i = j) {
		j = i, y = yy[ii[i]];
		while (j < n && yy[ii[j]] == y)
			yy[ii[j++]] = m;
		yy_[m++] = y;
	}
	for (i = 0; i < n; i++)
		tt[i + 1] = update(tt[i], 0, m, yy[i]);
	x_ = -INF;
	head = 0, cnt = 0;
	for (j = k; j <= n; j++) {
		while (cnt >= 2 && jj[head] <= j)
			head++, cnt--;
		i = j - k;
		while (cnt >= 2 && f(i, jj[head + cnt - 2]) >= f(qu[head + cnt - 1], jj[head + cnt - 2]))
			cnt--;
		if (cnt)
			jj[head + cnt - 1] = find_j(qu[head + cnt - 1], i);
		qu[head + cnt++] = i;
		while (cnt >= 2 && jj[head] <= j)
			head++, cnt--;
		i = qu[head];
		ii_[j] = i, xxj[j] = f(i, j);
		x_ = max(x_, xxj[j]);
	}
	head = 0, cnt = 0;
	for (i = n - k; i >= 0; i--) {
		while (cnt >= 2 && ii[head] >= i)
			head++, cnt--;
		j = i + k;
		while (cnt >= 2 && f(ii[head + cnt - 2], j) >= f(ii[head + cnt - 2], qu[head + cnt - 1]))
			cnt--;
		if (cnt)
			ii[head + cnt - 1] = find_i(j, qu[head + cnt - 1]);
		qu[head + cnt++] = j;
		while (cnt >= 2 && ii[head] >= i)
			head++, cnt--;
		j = qu[head];
		jj_[i] = j, xxi[i] = f(i, j);
		x_ = max(x_, xxi[i]);
	}
	build();
	for (j = k; j <= n; j++)
		if (xxj[j] == x_) {
			i = ii_[j];
			s_ = 0, y_ = -1, query(tt[i], tt[j], 0, m, k);
			update_(i, j - 1, y_);
		}
	for (i = 0; i <= n - k; i++)
		if (xxi[i] == x_) {
			j = jj_[i];
			s_ = 0, y_ = -1, query(tt[i], tt[j], 0, m, k);
			update_(i, j - 1, y_);
		}
	for (i = 1; i < n_; i++)
		st[i << 1 | 0] = min(st[i << 1 | 0], st[i]), st[i << 1 | 1] = min(st[i << 1 | 1], st[i]);
	for (i = 0; i < n; i++)
		cc[i] = st[n_ + i] <= yy[i] ? '1' : '0';
	printf("%lld\n", x_);
	printf("%s\n", cc);
	return 0;
}

Compilation message

trade.c: In function 'main':
trade.c:133:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
trade.c:135:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |   scanf("%lld", &xx[i]), xx[i] += xx[i - 1];
      |   ^~~~~~~~~~~~~~~~~~~~~
trade.c:137:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |   scanf("%d", &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 12636 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 12632 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 1 ms 12636 KB Output is correct
6 Partially correct 1 ms 12636 KB Partially correct
7 Correct 1 ms 12636 KB Output is correct
8 Correct 1 ms 12636 KB Output is correct
9 Correct 1 ms 12632 KB Output is correct
10 Correct 1 ms 12632 KB Output is correct
11 Partially correct 1 ms 12636 KB Partially correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 12632 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 1 ms 12636 KB Output is correct
6 Partially correct 1 ms 12636 KB Partially correct
7 Correct 1 ms 12636 KB Output is correct
8 Correct 1 ms 12636 KB Output is correct
9 Correct 1 ms 12632 KB Output is correct
10 Correct 1 ms 12632 KB Output is correct
11 Partially correct 1 ms 12636 KB Partially correct
12 Correct 1 ms 12636 KB Output is correct
13 Correct 1 ms 12636 KB Output is correct
14 Correct 1 ms 12636 KB Output is correct
15 Correct 1 ms 12636 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Partially correct 1 ms 12636 KB Partially correct
18 Correct 1 ms 12748 KB Output is correct
19 Correct 1 ms 12784 KB Output is correct
20 Correct 2 ms 12632 KB Output is correct
21 Correct 2 ms 12632 KB Output is correct
22 Partially correct 1 ms 12636 KB Partially correct
23 Correct 3 ms 13660 KB Output is correct
24 Correct 21 ms 14404 KB Output is correct
25 Correct 16 ms 14680 KB Output is correct
26 Correct 17 ms 14428 KB Output is correct
27 Correct 3 ms 13148 KB Output is correct
28 Correct 9 ms 13732 KB Output is correct
29 Correct 6 ms 14176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 12636 KB Output is correct
2 Correct 126 ms 32940 KB Output is correct
3 Correct 364 ms 59020 KB Output is correct
4 Correct 1295 ms 111440 KB Output is correct
5 Partially correct 1329 ms 110540 KB Partially correct
6 Correct 1341 ms 109264 KB Output is correct
7 Correct 905 ms 105296 KB Output is correct
8 Correct 463 ms 68868 KB Output is correct
9 Correct 433 ms 58964 KB Output is correct
10 Correct 300 ms 57684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 12636 KB Output is correct
2 Correct 126 ms 32940 KB Output is correct
3 Correct 364 ms 59020 KB Output is correct
4 Correct 1295 ms 111440 KB Output is correct
5 Partially correct 1329 ms 110540 KB Partially correct
6 Correct 1341 ms 109264 KB Output is correct
7 Correct 905 ms 105296 KB Output is correct
8 Correct 463 ms 68868 KB Output is correct
9 Correct 433 ms 58964 KB Output is correct
10 Correct 300 ms 57684 KB Output is correct
11 Correct 1 ms 12636 KB Output is correct
12 Correct 132 ms 33996 KB Output is correct
13 Correct 378 ms 59108 KB Output is correct
14 Correct 1280 ms 111440 KB Output is correct
15 Partially correct 1293 ms 110788 KB Partially correct
16 Correct 1361 ms 109196 KB Output is correct
17 Correct 919 ms 105344 KB Output is correct
18 Correct 477 ms 68872 KB Output is correct
19 Correct 430 ms 59032 KB Output is correct
20 Correct 317 ms 57672 KB Output is correct
21 Correct 1 ms 12636 KB Output is correct
22 Correct 1 ms 12636 KB Output is correct
23 Correct 1 ms 12636 KB Output is correct
24 Correct 1 ms 12636 KB Output is correct
25 Partially correct 1 ms 12636 KB Partially correct
26 Correct 1 ms 12636 KB Output is correct
27 Correct 1 ms 12636 KB Output is correct
28 Correct 1 ms 12720 KB Output is correct
29 Correct 1 ms 12636 KB Output is correct
30 Partially correct 1 ms 12636 KB Partially correct
31 Correct 334 ms 58908 KB Output is correct
32 Partially correct 1332 ms 110672 KB Partially correct
33 Correct 1368 ms 109164 KB Output is correct
34 Correct 1261 ms 96340 KB Output is correct
35 Correct 1152 ms 98640 KB Output is correct
36 Correct 542 ms 67232 KB Output is correct
37 Correct 171 ms 36044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 12636 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
3 Correct 1 ms 12632 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 1 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 1 ms 12636 KB Output is correct
8 Partially correct 1 ms 12636 KB Partially correct
9 Correct 1 ms 12636 KB Output is correct
10 Correct 1 ms 12636 KB Output is correct
11 Correct 1 ms 12632 KB Output is correct
12 Correct 1 ms 12632 KB Output is correct
13 Partially correct 1 ms 12636 KB Partially correct
14 Correct 1 ms 12636 KB Output is correct
15 Correct 1 ms 12636 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Correct 1 ms 12636 KB Output is correct
18 Correct 1 ms 12636 KB Output is correct
19 Partially correct 1 ms 12636 KB Partially correct
20 Correct 1 ms 12748 KB Output is correct
21 Correct 1 ms 12784 KB Output is correct
22 Correct 2 ms 12632 KB Output is correct
23 Correct 2 ms 12632 KB Output is correct
24 Partially correct 1 ms 12636 KB Partially correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 21 ms 14404 KB Output is correct
27 Correct 16 ms 14680 KB Output is correct
28 Correct 17 ms 14428 KB Output is correct
29 Correct 3 ms 13148 KB Output is correct
30 Correct 9 ms 13732 KB Output is correct
31 Correct 6 ms 14176 KB Output is correct
32 Correct 1 ms 12636 KB Output is correct
33 Correct 126 ms 32940 KB Output is correct
34 Correct 364 ms 59020 KB Output is correct
35 Correct 1295 ms 111440 KB Output is correct
36 Partially correct 1329 ms 110540 KB Partially correct
37 Correct 1341 ms 109264 KB Output is correct
38 Correct 905 ms 105296 KB Output is correct
39 Correct 463 ms 68868 KB Output is correct
40 Correct 433 ms 58964 KB Output is correct
41 Correct 300 ms 57684 KB Output is correct
42 Correct 1 ms 12636 KB Output is correct
43 Correct 132 ms 33996 KB Output is correct
44 Correct 378 ms 59108 KB Output is correct
45 Correct 1280 ms 111440 KB Output is correct
46 Partially correct 1293 ms 110788 KB Partially correct
47 Correct 1361 ms 109196 KB Output is correct
48 Correct 919 ms 105344 KB Output is correct
49 Correct 477 ms 68872 KB Output is correct
50 Correct 430 ms 59032 KB Output is correct
51 Correct 317 ms 57672 KB Output is correct
52 Correct 1 ms 12636 KB Output is correct
53 Correct 1 ms 12636 KB Output is correct
54 Correct 1 ms 12636 KB Output is correct
55 Correct 1 ms 12636 KB Output is correct
56 Partially correct 1 ms 12636 KB Partially correct
57 Correct 1 ms 12636 KB Output is correct
58 Correct 1 ms 12636 KB Output is correct
59 Correct 1 ms 12720 KB Output is correct
60 Correct 1 ms 12636 KB Output is correct
61 Partially correct 1 ms 12636 KB Partially correct
62 Correct 334 ms 58908 KB Output is correct
63 Partially correct 1332 ms 110672 KB Partially correct
64 Correct 1368 ms 109164 KB Output is correct
65 Correct 1261 ms 96340 KB Output is correct
66 Correct 1152 ms 98640 KB Output is correct
67 Correct 542 ms 67232 KB Output is correct
68 Correct 171 ms 36044 KB Output is correct
69 Correct 1 ms 12632 KB Output is correct
70 Correct 123 ms 34080 KB Output is correct
71 Correct 366 ms 58960 KB Output is correct
72 Correct 1336 ms 111604 KB Output is correct
73 Partially correct 1308 ms 110748 KB Partially correct
74 Correct 1379 ms 109052 KB Output is correct
75 Correct 909 ms 105244 KB Output is correct
76 Correct 468 ms 68948 KB Output is correct
77 Correct 428 ms 58964 KB Output is correct
78 Correct 299 ms 57628 KB Output is correct
79 Correct 1 ms 12636 KB Output is correct
80 Correct 1 ms 12636 KB Output is correct
81 Correct 1 ms 12636 KB Output is correct
82 Correct 1 ms 12632 KB Output is correct
83 Partially correct 1 ms 12636 KB Partially correct
84 Correct 1 ms 12636 KB Output is correct
85 Correct 1 ms 12636 KB Output is correct
86 Correct 1 ms 12636 KB Output is correct
87 Correct 1 ms 12636 KB Output is correct
88 Partially correct 1 ms 12636 KB Partially correct
89 Correct 331 ms 58936 KB Output is correct
90 Partially correct 1278 ms 110604 KB Partially correct
91 Correct 1364 ms 109204 KB Output is correct
92 Correct 1286 ms 96380 KB Output is correct
93 Correct 1198 ms 98732 KB Output is correct
94 Correct 539 ms 67232 KB Output is correct
95 Correct 172 ms 35924 KB Output is correct
96 Correct 3 ms 13660 KB Output is correct
97 Correct 17 ms 14492 KB Output is correct
98 Correct 16 ms 14444 KB Output is correct
99 Correct 17 ms 14680 KB Output is correct
100 Correct 3 ms 13400 KB Output is correct
101 Correct 11 ms 13660 KB Output is correct
102 Correct 8 ms 14172 KB Output is correct
103 Correct 1794 ms 109596 KB Output is correct
104 Correct 1254 ms 98924 KB Output is correct
105 Correct 1201 ms 95572 KB Output is correct
106 Partially correct 1103 ms 95600 KB Partially correct
107 Correct 107 ms 94804 KB Output is correct
108 Correct 568 ms 96336 KB Output is correct
109 Correct 97 ms 35972 KB Output is correct
110 Correct 278 ms 67480 KB Output is correct
111 Correct 675 ms 108860 KB Output is correct
112 Partially correct 154 ms 35920 KB Partially correct