Submission #442048

# Submission time Handle Problem Language Result Execution time Memory
442048 2021-07-06T21:36:40 Z rainboy Pyramid Base (IOI08_pyramid_base) C
100 / 100
1395 ms 63180 KB
#include <stdio.h>
#include <string.h>

#define N_	(1 << 20)	/* N_ = pow2(ceil(log2(M))) */
#define M	1000000
#define N	400000

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

unsigned int X = 12345;

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

int *yy;

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 xx1[N], xx2[N], yy1[N], yy2[N], ii1[N], ii2[N], cc[N], n, x_, y_, c, s;

int st[N_ * 2], lz[N_ * 2], pp[N_ * 2], qq[N_ * 2], ss[N_ * 2], sz[N_ * 2], mode; char ok[N_ * 2];

void pul(int i) {
	if (mode == 1)
		st[i] = min(st[i << 1 | 0], st[i << 1 | 1]) + lz[i];
	else if (lz[i])
		pp[i] = qq[i] = ss[i] = 0;
	else if (i >= N_)
		pp[i] = qq[i] = ss[i] = 1;
	else {
		int l = i << 1, r = l | 1;

		pp[i] = pp[l] != sz[l] ? pp[l] : sz[l] + pp[r];
		qq[i] = qq[r] != sz[r] ? qq[r] : qq[l] + sz[r];
		ss[i] = max(max(ss[l], ss[r]), qq[l] + pp[r]);
	}
}

void pus(int i, int x) {
	lz[i] += x;
	if (mode == 1)
		st[i] += x;
	else
		pul(i);
}

void update(int l, int r, int x) {
	int l_ = l += N_, r_ = r += N_;

	if (l > r)
		return;
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			pus(l++, x);
		if ((r & 1) == 0)
			pus(r--, x);
	}
	while (l_ > 1)
		pul(l_ >>= 1);
	while (r_ > 1)
		pul(r_ >>= 1);
}

int h1, h2;

int try(int y) {
	int i;

	while (h1 < n && yy1[i = ii1[h1]] - s + 1 <= y)
		h1++, update(max(xx1[i] - s + 1, 0), min(xx2[i], x_ - s), cc[i]);
	while (h2 < n && yy2[i = ii2[h2]] + 1 <= y)
		h2++, update(max(xx1[i] - s + 1, 0), min(xx2[i], x_ - s), -cc[i]);
	return st[1] <= c;
}

int check() {
	int y;

	memset(st, 0, N_ * 2 * sizeof *st), memset(lz, 0, N_ * 2 * sizeof *lz);
	update(x_ - s + 1, N_ - 1, c + 1);
	h1 = 0, h2 = 0;
	if (try(0))
		return 1;
	while (h1 < n || h2 < n) {
		if (h1 == n)
			y = yy2[ii2[h2]] + 1;
		else if (h2 == n)
			y = yy1[ii1[h1]] - s + 1;
		else
			y = min(yy1[ii1[h1]] - s + 1, yy2[ii2[h2]] + 1);
		if (y + s - 1 >= y_)
			break;
		if (try(y))
			return 1;
	}
	return 0;
}

int main() {
	int i, lower, upper;

	scanf("%d%d%d%d", &x_, &y_, &c, &n);
	for (i = 0; i < n; i++) {
		int x1, x2, y1, y2, c;

		scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c), x1--, x2--, y1--, y2--;
		xx1[i] = x1, xx2[i] = x2, yy1[i] = y1, yy2[i] = y2, cc[i] = c;
		ii1[i] = ii2[i] = i;
	}
	yy = yy1, sort(ii1, 0, n);
	yy = yy2, sort(ii2, 0, n);
	if (c != 0) {
		mode = 1;
		lower = 0, upper = (x_ < y_ ? x_ : y_) + 1;
		while (upper - lower > 1) {
			s = (lower + upper) / 2;
			if (check())
				lower = s;
			else
				upper = s;
		}
		printf("%d\n", lower);
	} else {
		mode = 2;
		for (i = N_ + N_ - 1; i >= 0; i--)
			sz[i] = i >= N_ ? 1 : sz[i << 1 | 0] + sz[i << 1 | 1], pul(i);
		update(x_, N_ - 1, 1);
		for (lower = 0, upper = 0; upper < y_; upper++) {
			while (h1 < n && yy1[i = ii1[h1]] <= upper)
				h1++, update(xx1[i], xx2[i], 1);
			while (ss[1] < upper - lower + 1) {
				while (h2 < n && yy2[i = ii2[h2]] <= lower)
					h2++, update(xx1[i], xx2[i], -1);
				lower++;
			}
			s = max(s, upper - lower + 1);
		}
		printf("%d\n", s);
	}
	return 0;
}

Compilation message

pyramid_base.c: In function 'main':
pyramid_base.c:121:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |  scanf("%d%d%d%d", &x_, &y_, &c, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pyramid_base.c:125:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |   scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c), x1--, x2--, y1--, y2--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 33092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 33188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 33100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 33228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 33976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 37828 KB Output is correct
2 Correct 37 ms 38572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 35664 KB Output is correct
2 Correct 36 ms 37956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 16844 KB Output is correct
2 Correct 61 ms 16844 KB Output is correct
3 Correct 69 ms 16964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 17140 KB Output is correct
2 Correct 224 ms 17136 KB Output is correct
3 Correct 203 ms 17136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 228 ms 17288 KB Output is correct
2 Correct 121 ms 17228 KB Output is correct
3 Correct 71 ms 17280 KB Output is correct
4 Correct 476 ms 17272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 371 ms 17484 KB Output is correct
2 Correct 580 ms 17420 KB Output is correct
3 Correct 250 ms 17412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 340 ms 17648 KB Output is correct
2 Correct 804 ms 17556 KB Output is correct
3 Correct 702 ms 17548 KB Output is correct
4 Correct 815 ms 17548 KB Output is correct
5 Correct 685 ms 17556 KB Output is correct
6 Correct 253 ms 17608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 720 ms 46372 KB Output is correct
2 Correct 475 ms 46412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 921 ms 48832 KB Output is correct
2 Correct 938 ms 57384 KB Output is correct
3 Correct 806 ms 49860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1132 ms 48060 KB Output is correct
2 Correct 1395 ms 63036 KB Output is correct
3 Correct 1342 ms 62848 KB Output is correct
4 Correct 1252 ms 62788 KB Output is correct
5 Correct 1078 ms 63180 KB Output is correct