Submission #441967

# Submission time Handle Problem Language Result Execution time Memory
441967 2021-07-06T15:39:12 Z rainboy Pyramid Base (IOI08_pyramid_base) C
70 / 100
5000 ms 32408 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

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

int xmin[P], xmax[P], ymin[P], ymax[P], imin[P], imax[P], cc[P], m, n, p, a, b;

int iay[P * 2][3];

int compare_ymin(const void *a, const void *b) {
	int i = *(int *) a;
	int j = *(int *) b;

	return ymin[i] - ymin[j];
}

int compare_ymax(const void *a, const void *b) {
	int i = *(int *) a;
	int j = *(int *) b;

	return ymax[i] - ymax[j];
}

int st[N_ * 2], lz[N_ * 2]; char ok[N_ * 2];

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)
			st[l] += x, lz[l] += x, l++;
		if ((r & 1) == 0)
			st[r] += x, lz[r] += x, r--;
	}
	while (l_ > 1)
		l_ >>= 1, st[l_] = min(st[l_ << 1 | 0], st[l_ << 1 | 1]) + lz[l_];
	while (r_ > 1)
		r_ >>= 1, st[r_] = min(st[r_ << 1 | 0], st[r_ << 1 | 1]) + lz[r_];
}

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)
			lz[l] += x, ok[l] = !lz[l] && (l >= N_ || ok[l << 1 | 0] || ok[l << 1 | 1]), l++;
		if ((r & 1) == 0)
			lz[r] += x, ok[r] = !lz[r] && (r >= N_ || ok[r << 1 | 0] || ok[r << 1 | 1]), r--;
	}
	while (l_ > 1)
		l_ >>= 1, ok[l_] = !lz[l_] && (l_ >= N_ || ok[l_ << 1 | 0] || ok[l_ << 1 | 1]);
	while (r_ > 1)
		r_ >>= 1, ok[r_] = !lz[r_] && (r_ >= N_ || ok[r_ << 1 | 0] || ok[r_ << 1 | 1]);
}

void merge() {
	int h, i, j;

	for (i = j = 0; (h = i + j) < p + p; ) {
		int *zz = iay[h];

		if (j == p)
			zz[1] = 1, zz[2] = ymin[zz[0] = imin[i++]] - a + 1;
		else if (i == p)
			zz[1] = 0, zz[2] = ymax[zz[0] = imax[j++]] + 1;
		else {
			int y1 = ymin[imin[i]] - a + 1, y0 = ymax[imax[j]] + 1;

			if (y1 < y0)
				zz[0] = imin[i++], zz[1] = 1, zz[2] = y1;
			else
				zz[0] = imax[j++], zz[1] = 0, zz[2] = y0;
		}
	}
}

int check() {
	int h, j, zero;

	zero = 0;
	if (b != 0) {
		memset(st, 0, N_ * 2 * sizeof *st), memset(lz, 0, N_ * 2 * sizeof *lz), update(m - a + 1, N_ - 1, b + 1);
		merge();
		for (h = 0; h < p * 2; h = j) {
			int y = iay[h][2];

			if (y + a - 1 >= n)	/* top boundary */
				break;
			if (!zero && y > 0) {
				zero = 1;
				if (st[1] <= b)
					return 1;
			}
			for (j = h; j < p * 2 && iay[j][2] == y; j++) {
				int i_ = iay[j][0];

				update(max(xmin[i_] - a + 1, 0), min(xmax[i_], m - a), iay[j][1] ? cc[i_] : -cc[i_]);
			}
			if (y < 0)	/* bottom boundary */
				continue;
			if (st[1] <= b)
				return 1;
		}
	} else {
		memset(ok, 1, N_ * 2 * sizeof *ok), memset(lz, 0, N_ * 2 * sizeof *lz), update_(m - a + 1, N_ - 1, 1);
		merge();
		for (h = 0; h < p * 2; h = j) {
			int y = iay[h][2];

			if (y + a - 1 >= n)	/* top boundary */
				break;
			if (!zero && y > 0) {
				zero = 1;
				if (ok[1])
					return 1;
			}
			for (j = h; j < p * 2 && iay[j][2] == y; j++) {
				int i_ = iay[j][0];

				update_(max(xmin[i_] - a + 1, 0), min(xmax[i_], m - a), iay[j][1] ? 1 : -1);
			}
			if (y < 0)	/* bottom boundary */
				continue;
			if (ok[1])
				return 1;
		}
	}
	return 0;
}

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

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

		scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c), x1--, x2--, y1--, y2--;
		xmin[i] = x1, xmax[i] = x2, ymin[i] = y1, ymax[i] = y2, cc[i] = c;
		imin[i] = imax[i] = i;
	}
	qsort(imin, p, sizeof *imin, compare_ymin);
	qsort(imax, p, sizeof *imax, compare_ymax);
	a = 1;
	if (!check()) {
		printf("0\n");
		return 0;
	}
	lower = 1, upper = (m < n ? m : n) + 1, low = 1;
	while (upper - lower > 1) {
		if (upper - lower > 300)
			a = (lower + upper + (low ? lower : upper)) / 3;
		else
			a = (lower + upper) / 2;
		if (check())
			lower = a, low = 1;
		else
			upper = a, low = 0;
	}
	printf("%d\n", lower);
	return 0;
}

Compilation message

pyramid_base.c: In function 'main':
pyramid_base.c:144:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |  scanf("%d%d%d%d", &m, &n, &b, &p);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pyramid_base.c:148:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |   scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c), x1--, x2--, y1--, y2--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 10548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 10572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 10624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 10636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 10572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 10572 KB Output is correct
2 Correct 7 ms 10572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 10572 KB Output is correct
2 Correct 53 ms 10572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 17076 KB Output is correct
2 Correct 77 ms 17008 KB Output is correct
3 Correct 80 ms 16972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 17556 KB Output is correct
2 Correct 283 ms 17552 KB Output is correct
3 Correct 258 ms 17556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 267 ms 17828 KB Output is correct
2 Correct 128 ms 17828 KB Output is correct
3 Correct 91 ms 17828 KB Output is correct
4 Correct 567 ms 17824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 402 ms 18104 KB Output is correct
2 Correct 754 ms 18100 KB Output is correct
3 Correct 269 ms 18116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 486 ms 18248 KB Output is correct
2 Correct 999 ms 18372 KB Output is correct
3 Correct 997 ms 18368 KB Output is correct
4 Correct 1070 ms 18372 KB Output is correct
5 Correct 950 ms 18492 KB Output is correct
6 Correct 239 ms 18372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5001 ms 21532 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5059 ms 26876 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5062 ms 32408 KB Time limit exceeded
2 Halted 0 ms 0 KB -