답안 #544767

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544767 2022-04-02T16:35:21 Z rainboy Walk (CEOI06_walk) C
100 / 100
93 ms 8704 KB
/* upsolve after reading analysis */
#include <stdio.h>
#include <string.h>

#define N	100000
#define N_	(1 << 18)

int abs_(int x) { return x > 0 ? x : -x; }

unsigned int X = 12345;

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

int *zz;

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 (zz[ii[j]] == zz[i_])
				j++;
			else if (zz[ii[j]] < zz[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 st[N_ * 2], xx[N], n_;

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

int query(int i) {
	int i_ = -1;

	for (i += n_; i > 0; i >>= 1)
		if (i_ == -1 || st[i] != -1 && xx[i_] < xx[st[i]])
			i_ = st[i];
	return i_;
}

int main() {
	static int ii[N * 2], yy[N * 2], yy_[N * 2], dp[N * 2], pp[N * 2], qu[N * 2];
	int n, x, y, y_, h, i, j, j0, j1, ans, cnt, cnt_, x1, y1;

	scanf("%d%d%d", &x, &y, &n);
	for (i = 0; i < n; i++) {
		int x1, y1, x2, y2, tmp;

		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		if (x1 > x2)
			tmp = x1, x1 = x2, x2 = tmp;
		if (y1 > y2)
			tmp = y1, y1 = y2, y2 = tmp;
		y2++;
		xx[i] = x2, yy[i << 1 | 0] = y1, yy[i << 1 | 1] = y2;
	}
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	zz = yy, sort(ii, 0, n * 2);
	for (h = 0, y_ = 0; h < n * 2; h++)
		yy_[ii[h]] = h + 1 == n * 2 || yy[ii[h + 1]] != yy[ii[h]] ? y_++ : y_;
	for (h = -1; h < n * 2; h++)
		if (h + 1 == n * 2 || yy[ii[h + 1]] > y) {
			y_ = h == -1 ? -1 : yy_[ii[h]];
			break;
		}
	n_ = 1;
	while (n_ < n * 2)
		n_ <<= 1;
	memset(st, -1, n_ * 2 * sizeof *st);
	for (i = 0; i < n; i++)
		ii[i] = i;
	zz = xx, sort(ii, 0, n);
	for (h = 0; h < n && xx[ii[h]] < x; h++) {
		int i0, i1;

		i = ii[h], i0 = i << 1 | 0, i1 = i << 1 | 1;
		j = yy_[i0] == 0 ? -1 : query(yy_[i0] - 1), j0 = j << 1 | 0, j1 = j << 1 | 1;
		if (j == -1)
			dp[i0] = abs_(yy[i0] - 1), pp[i0] = -1;
		else if (dp[j0] + yy[i0] - yy[j0] < dp[j1] + yy[j1] - yy[i0] + 1)
			dp[i0] = dp[j0] + yy[i0] - yy[j0], pp[i0] = j0;
		else
			dp[i0] = dp[j1] + yy[j1] - yy[i0] + 1, pp[i0] = j1;
		j = query(yy_[i1]), j0 = j << 1 | 0, j1 = j << 1 | 1;
		if (j == -1)
			dp[i1] = abs_(yy[i1]), pp[i1] = -1;
		else if (dp[j0] + yy[i1] - yy[j0] + 1 < dp[j1] + yy[j1] - yy[i1])
			dp[i1] = dp[j0] + yy[i1] - yy[j0] + 1, pp[i1] = j0;
		else
			dp[i1] = dp[j1] + yy[j1] - yy[i1], pp[i1] = j1;
		update(yy_[i0], yy_[i1] - 1, i);
	}
	j = y_ == -1 ? -1 : query(y_), j0 = j << 1 | 0, j1 = j << 1 | 1;
	if (j == -1)
		ans = abs_(y), i = -1;
	else if (dp[j0] + y - yy[j0] + 1 < dp[j1] + yy[j1] - y)
		ans = dp[j0] + y - yy[j0] + 1, i = j0;
	else
		ans = dp[j1] + yy[j1] - y, i = j1;
	ans += x;
	printf("%d\n", ans);
	cnt = 0;
	while (i != -1) {
		qu[cnt++] = i;
		i = pp[i];
	}
	i = cnt == 0 ? -1 : qu[cnt - 1];
	cnt_ = (cnt + 1) * 2;
	if ((cnt ? ((i & 1) == 0 ? yy[i] - 1 : yy[i]) : y) == 0)
		cnt_--;
	if (cnt && xx[qu[0] >> 1] + 1 == x)
		cnt_--;
	printf("%d\n", cnt_);
	x1 = 0, y1 = 0;
	while (cnt--) {
		int x_, y_;

		i = qu[cnt], x_ = xx[i >> 1] + 1, y_ = (i & 1) == 0 ? yy[i] - 1 : yy[i];
		if (y1 != y_)
			printf("0 %d\n", y_ - y1), y1 = y_;
		if (x1 != x_)
			printf("%d 0\n", x_ - x1), x1 = x_;
	}
	if (y1 != y)
		printf("0 %d\n", y - y1), y1 = y;
	if (x1 != x)
		printf("%d 0\n", x - x1), x1 = x;
	return 0;
}

Compilation message

walk.c: In function 'query':
walk.c:52:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   52 |   if (i_ == -1 || st[i] != -1 && xx[i_] < xx[st[i]])
      |                   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
walk.c: In function 'main':
walk.c:61:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |  scanf("%d%d%d", &x, &y, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
walk.c:65:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |   scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 89 ms 8260 KB Output is correct
6 Correct 33 ms 2728 KB Output is correct
7 Correct 45 ms 4624 KB Output is correct
8 Correct 80 ms 8524 KB Output is correct
9 Correct 93 ms 8620 KB Output is correct
10 Correct 84 ms 8704 KB Output is correct