답안 #711394

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711394 2023-03-16T19:50:27 Z rainboy Golf (JOI17_golf) C
30 / 100
228 ms 14244 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100002
#define N2	((N + 1) * 2)
#define L	18	/* L = ceil(log2(N2)) */
#define N_	(1 << L)
#define N1	(N * 4 + N * 4 * (L + 1))

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

unsigned int Z = 12345;

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

int xx[N * 2], yy[N * 2], n, n2;
int xx1[N * 2], xx2[N * 2], yy1[N * 2], yy2[N * 2];

int compare_x(int i, int j) {
	return xx[i] - xx[j];
}

int compare_y(int i, int j) {
	return yy[i] - yy[j];
}

int compare_x_(int i, int j) {
	int ti, tj, xi, xj;

	ti = i / (n * 2), i %= n * 2;
	tj = j / (n * 2), j %= n * 2;
	if (ti == 0)
		xi = xx1[i];
	else if (ti == 1)
		xi = xx[i];
	else
		xi = xx2[i];
	if (tj == 0)
		xj = xx1[j];
	else if (tj == 1)
		xj = xx[j];
	else
		xj = xx2[j];
	return xi != xj ? xi - xj : ti - tj;
}

int compare_y_(int i, int j) {
	int ti, tj, yi, yj;

	ti = i / (n * 2), i %= n * 2;
	tj = j / (n * 2), j %= n * 2;
	if (ti == 0)
		yi = yy1[i];
	else if (ti == 1)
		yi = yy[i];
	else
		yi = yy2[i];
	if (tj == 0)
		yj = yy1[j];
	else if (tj == 1)
		yj = yy[j];
	else
		yj = yy2[j];
	return yi != yj ? yi - yj : ti - tj;
}

int (*compare)(int, int);

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) {
			int c = compare(ii[j], i_);

			if (c == 0)
				j++;
			else if (c < 0) {
				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 *kk, zz[N + 1], ll[N + 1], rr[N + 1], ii[N + 1], u_, l_, r_;

int node(int i) {
	static int _ = 1;

	zz[_] = rand_();
	ii[_] = i;
	return _++;
}

void split(int u, int i) {
	int c;

	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	c = kk[ii[u] << 1 | 0] - kk[i << 1 | 0];
	if (c < 0) {
		split(rr[u], i);
		rr[u] = l_, l_ = u;
	} else if (c > 0) {
		split(ll[u], i);
		ll[u] = r_, r_ = u;
	} else {
		u_ = u, l_ = ll[u], r_ = rr[u];
		ll[u] = rr[u] = 0;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

void tr_add(int i) {
	split(u_, i);
	u_ = merge(merge(l_, node(i)), r_);
}

void tr_remove(int i) {
	split(u_, i);
	u_ = merge(l_, r_);
}

int tr_lower(int k) {
	int u = u_, i_ = -1;

	while (u)
		if (kk[ii[u] << 1 | 0] < k)
			i_ = ii[u], u = rr[u];
		else
			u = ll[u];
	return i_ == -1 ? 0 : kk[i_ << 1 | 1];
}

int tr_higher(int k) {
	int u = u_, i_ = -1;

	while (u)
		if (kk[ii[u] << 1 | 0] > k)
			i_ = ii[u], u = ll[u];
		else
			u = rr[u];
	return i_ == -1 ? n2 : kk[i_ << 1 | 0];
}

int intersect1(int a, int b, int c, int d) {
	return max(a, c) <= min(b, d);
}

int intersect2(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
	return intersect1(x1, x2, x3, x4) && intersect1(y1, y2, y3, y4);
}

int *ej[N1], *ew[N1], eo[N1], n1;

int newnode() {
	ej[n1] = (int *) malloc(2 * sizeof *ej[n1]);
	ew[n1] = (int *) malloc(2 * sizeof *ew[n1]);
	return n1++;
}

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

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

int st[N_ * 2], n_;

void pul(int i) {
	int l = i << 1, r = l | 1;

	if (st[l] == -1 && st[r] == -1)
		st[i] = -1;
	else {
		st[i] = newnode();
		if (st[l] != -1)
			append(st[i], st[l], 0);
		if (st[r] != -1)
			append(st[i], st[r], 0);
	}
}

void update(int i, int u) {
	i += n_;
	if (u != -1 && st[i] != -1) {
		append(st[i], u, 0), append(u, st[i], 0);
		return;
	}
	if (u == -1 && st[i] == -1)
		return;
	st[i] = u;
	while (i > 1)
		pul(i >>= 1);
}

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

int main() {
	static int ii[N * 6], dd[N_], qu[N_ * 2];
	int cnt, h, h_, h1, i, i_, t, x1, y1, x2, y2, x, y, o;

	scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n), n += 2, n2 = (n + 1) * 2;
	xx[0 << 1 | 0] = xx[0 << 1 | 1] = x1, yy[0 << 1 | 0] = yy[0 << 1 | 1] = y1;
	xx[1 << 1 | 0] = xx[1 << 1 | 1] = x2, yy[1 << 1 | 0] = yy[1 << 1 | 1] = y2;
	for (i = 2; i < n; i++)
		scanf("%d%d%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1], &yy[i << 1 | 0], &yy[i << 1 | 1]);
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	compare = compare_x, sort(ii, 0, n * 2);
	for (i = 0, x = 1; i < n * 2; i++)
		xx[ii[i]] = i + 1 == n * 2 || xx[ii[i + 1]] != xx[ii[i]] ? x++ : x;
	compare = compare_y, sort(ii, 0, n * 2);
	for (i = 0, y = 1; i < n * 2; i++)
		yy[ii[i]] = i + 1 == n * 2 || yy[ii[i + 1]] != yy[ii[i]] ? y++ : y;
	compare = compare_x, sort(ii, 0, n * 2);
	kk = yy;
	for (h = 0; h < n * 2; h = h_) {
		h_ = h + 1, x = xx[ii[h]];
		while (h_ < n * 2 && xx[ii[h_]] == x)
			h_++;
		for (h1 = h; h1 < h_; h1++) {
			i_ = ii[h1], i = i_ >> 1;
			if ((i_ & 1) == 1)
				tr_remove(i);
		}
		for (h1 = h; h1 < h_; h1++) {
			i_ = ii[h1], i = i_ >> 1;
			yy1[i_] = tr_lower(yy[i << 1 | 0]), yy2[i_] = tr_higher(yy[i << 1 | 1]);
		}
		for (h1 = h; h1 < h_; h1++) {
			i_ = ii[h1], i = i_ >> 1;
			if ((i_ & 1) == 0 && xx[i_] != xx[i_ ^ 1])
				tr_add(i);
		}
	}
	compare = compare_y, sort(ii, 0, n * 2);
	kk = xx;
	for (h = 0; h < n * 2; h = h_) {
		h_ = h + 1, y = yy[ii[h]];
		while (h_ < n * 2 && yy[ii[h_]] == y)
			h_++;
		for (h1 = h; h1 < h_; h1++) {
			i_ = ii[h1], i = i_ >> 1;
			if ((i_ & 1) == 1)
				tr_remove(i);
		}
		for (h1 = h; h1 < h_; h1++) {
			i_ = ii[h1], i = i_ >> 1;
			xx1[i_] = tr_lower(xx[i << 1 | 0]), xx2[i_] = tr_higher(xx[i << 1 | 1]);
		}
		for (h1 = h; h1 < h_; h1++) {
			i_ = ii[h1], i = i_ >> 1;
			if ((i_ & 1) == 0 && yy[i_] != yy[i_ ^ 1])
				tr_add(i);
		}
	}
	if (intersect2(xx1[0], yy[0], xx2[0], yy[0], x2, y2, x2, y2) || intersect2(xx[0], yy1[0], xx[0], yy2[0], x2, y2, x2, y2)) {
		printf("1\n");
		return 0;
	}
	for (i = 0; i < n * 6; i++)
		ii[i] = i;
	for (i = 0; i < n * 4; i++)
		newnode();
	n_ = 1;
	while (n_ < n2)
		n_ <<= 1;
	memset(st, -1, n_ * 2 * sizeof *st);
	compare = compare_x_, sort(ii, 0, n * 6);
	for (h = 0; h < n * 6; h++) {
		t = ii[h] / (n * 2), i = ii[h] % (n * 2);
		if (t == 0)
			update(yy[i], i << 1 | 1);
		else if (t == 2)
			update(yy[i], -1);
		else
			query(yy1[i], yy2[i], i << 1 | 0);
	}
	memset(st, -1, n_ * 2 * sizeof *st);
	compare = compare_y_, sort(ii, 0, n * 6);
	for (h = 0; h < n * 6; h++) {
		t = ii[h] / (n * 2), i = ii[h] % (n * 2);
		if (t == 0)
			update(xx[i], i << 1 | 0);
		else if (t == 2)
			update(xx[i], -1);
		else
			query(xx1[i], xx2[i], i << 1 | 1);
	}
	for (i = 0; i < n1; i++)
		dd[i] = n1 + 2;
	cnt = n1;
	dd[0 << 1 | 0] = 1, qu[cnt++] = 0 << 1 | 0;
	dd[0 << 1 | 1] = 1, qu[cnt++] = 0 << 1 | 1;
	for (h = n1; h < cnt; h++) {
		i = qu[h];
		if ((i >> 1) == 2) {
			printf("%d\n", dd[i]);
			return 0;
		}
		for (o = eo[i]; o--; ) {
			int j = ej[i][o], w = ew[i][o], d = dd[i] + w;

			if (dd[j] > d) {
				dd[j] = d;
				if (w == 0)
					qu[h--] = j;
				else
					qu[cnt++] = j;
			}
		}
	}
	return 0;
}

Compilation message

golf.c: In function 'append':
golf.c:191:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
  191 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
golf.c: In function 'main':
golf.c:246:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  246 |  scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n), n += 2, n2 = (n + 1) * 2;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
golf.c:250:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  250 |   scanf("%d%d%d%d", &xx[i << 1 | 0], &xx[i << 1 | 1], &yy[i << 1 | 0], &yy[i << 1 | 1]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 14 ms 6560 KB Output is correct
6 Correct 13 ms 6568 KB Output is correct
7 Correct 13 ms 6316 KB Output is correct
8 Correct 14 ms 6520 KB Output is correct
9 Correct 12 ms 6612 KB Output is correct
10 Correct 14 ms 6652 KB Output is correct
11 Correct 12 ms 6740 KB Output is correct
12 Correct 11 ms 6612 KB Output is correct
13 Correct 11 ms 6612 KB Output is correct
14 Correct 13 ms 6740 KB Output is correct
15 Correct 4 ms 1876 KB Output is correct
16 Correct 9 ms 3732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 14 ms 6560 KB Output is correct
6 Correct 13 ms 6568 KB Output is correct
7 Correct 13 ms 6316 KB Output is correct
8 Correct 14 ms 6520 KB Output is correct
9 Correct 12 ms 6612 KB Output is correct
10 Correct 14 ms 6652 KB Output is correct
11 Correct 12 ms 6740 KB Output is correct
12 Correct 11 ms 6612 KB Output is correct
13 Correct 11 ms 6612 KB Output is correct
14 Correct 13 ms 6740 KB Output is correct
15 Correct 4 ms 1876 KB Output is correct
16 Correct 9 ms 3732 KB Output is correct
17 Correct 14 ms 7252 KB Output is correct
18 Correct 13 ms 7252 KB Output is correct
19 Correct 18 ms 7356 KB Output is correct
20 Correct 15 ms 7352 KB Output is correct
21 Correct 14 ms 7432 KB Output is correct
22 Correct 13 ms 7380 KB Output is correct
23 Correct 14 ms 7508 KB Output is correct
24 Correct 14 ms 7436 KB Output is correct
25 Correct 12 ms 7508 KB Output is correct
26 Correct 13 ms 7508 KB Output is correct
27 Correct 4 ms 2132 KB Output is correct
28 Correct 8 ms 4052 KB Output is correct
29 Correct 8 ms 4052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 852 KB Output is correct
5 Correct 14 ms 6560 KB Output is correct
6 Correct 13 ms 6568 KB Output is correct
7 Correct 13 ms 6316 KB Output is correct
8 Correct 14 ms 6520 KB Output is correct
9 Correct 12 ms 6612 KB Output is correct
10 Correct 14 ms 6652 KB Output is correct
11 Correct 12 ms 6740 KB Output is correct
12 Correct 11 ms 6612 KB Output is correct
13 Correct 11 ms 6612 KB Output is correct
14 Correct 13 ms 6740 KB Output is correct
15 Correct 4 ms 1876 KB Output is correct
16 Correct 9 ms 3732 KB Output is correct
17 Correct 14 ms 7252 KB Output is correct
18 Correct 13 ms 7252 KB Output is correct
19 Correct 18 ms 7356 KB Output is correct
20 Correct 15 ms 7352 KB Output is correct
21 Correct 14 ms 7432 KB Output is correct
22 Correct 13 ms 7380 KB Output is correct
23 Correct 14 ms 7508 KB Output is correct
24 Correct 14 ms 7436 KB Output is correct
25 Correct 12 ms 7508 KB Output is correct
26 Correct 13 ms 7508 KB Output is correct
27 Correct 4 ms 2132 KB Output is correct
28 Correct 8 ms 4052 KB Output is correct
29 Correct 8 ms 4052 KB Output is correct
30 Runtime error 228 ms 14244 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -