답안 #711404

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711404 2023-03-16T20:17:35 Z rainboy Golf (JOI17_golf) C
100 / 100
1634 ms 224624 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 * (L + 1) * 2 + 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 typei, typej, xi, xj;

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

int compare_y_(int i, int j) {
	int typei, typej, yi, yj;

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

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 * 2 + 1], ll[N * 2 + 1], rr[N * 2 + 1], ii[N * 2 + 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[N * 4], eo[N * 4];

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

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

int ll_[N1], rr_[N1], uu[N1]; char visited[N1];

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

	ll_[t_] = ll_[t], rr_[t_] = rr_[t], uu[t_] = uu[t];
	if (r - l > 1) {
		int m = (l + r) / 2;

		if (i < m)
			ll_[t_] = update(ll_[t_], l, m, i, u);
		else
			rr_[t_] = update(rr_[t_], m, r, i, u);
	} else {
		if (uu[t_] == -1 || u == -1)
			uu[t_] = u;
		else
			append(uu[t_], u), append(u, uu[t_]);
	}
	return t_;
}

int dd[N * 4], qu[N * 8], cnt;

void dfs(int t, int l, int r, int ql, int qr, int d) {
	int m;

	if (qr <= l || r <= ql || t == 0 || visited[t])
		return;
	if (ql <= l && r <= qr) {
		visited[t] = 1;
		if (r - l > 1) {
			m = (l + r) / 2;
			dfs(ll_[t], l, m, ql, qr, d), dfs(rr_[t], m, r, ql, qr, d);
		} else if (uu[t] != -1 && dd[uu[t]] > d)
			dd[uu[t]] = d, qu[cnt++] = uu[t];
		return;
	}
	m = (l + r) / 2;
	dfs(ll_[t], l, m, ql, qr, d), dfs(rr_[t], m, r, ql, qr, d);
}

int main() {
	static int ii[N * 6], tt[N * 4];
	int h, h_, h1, i, i_, j, type, x1, y1, x2, y2, x, y, d, o, t;

	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++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	compare = compare_x_, sort(ii, 0, n * 6);
	uu[0] = -1;
	t = 0;
	for (h = 0; h < n * 6; h++) {
		type = ii[h] / (n * 2), i = ii[h] % (n * 2);
		if (type == 0)
			t = update(t, 0, n2, yy[i], i << 1 | 1);
		else if (type == 2)
			t = update(t, 0, n2, yy[i], -1);
		else
			tt[i << 1 | 0] = t;
	}
	compare = compare_y_, sort(ii, 0, n * 6);
	for (h = 0; h < n * 6; h++) {
		type = ii[h] / (n * 2), i = ii[h] % (n * 2);
		if (type == 0)
			t = update(t, 0, n2, xx[i], i << 1 | 0);
		else if (type == 2)
			t = update(t, 0, n2, xx[i], -1);
		else
			tt[i << 1 | 1] = t;
	}
	for (i = 0; i < n * 4; i++)
		dd[i] = n * 4 + 2;
	cnt = n * 4;
	dd[0 << 1 | 0] = 1, qu[cnt++] = 0 << 1 | 0;
	dd[0 << 1 | 1] = 1, qu[cnt++] = 0 << 1 | 1;
	for (h = n * 4; h < cnt; h++) {
		i_ = qu[h], d = dd[i_], i = i_ >> 1;
		if (i == 2) {
			printf("%d\n", d);
			return 0;
		}
		for (o = eo[i_]; o--; ) {
			j = ej[i_][o];
			if (dd[j] > d)
				dd[j] = d, qu[h--] = j;
		}
		if ((i_ & 1) == 0)
			dfs(tt[i_], 0, n2, yy1[i], yy2[i], d + 1);
		else
			dfs(tt[i_], 0, n2, xx1[i], xx2[i], d + 1);
	}
	return 0;
}

Compilation message

golf.c: In function 'append':
golf.c:185:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
  185 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
golf.c: In function 'main':
golf.c:237:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  237 |  scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n), n += 2, n2 = (n + 1) * 2;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
golf.c:241:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  241 |   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 0 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 1 ms 468 KB Output is correct
5 Correct 7 ms 1876 KB Output is correct
6 Correct 8 ms 1876 KB Output is correct
7 Correct 7 ms 1876 KB Output is correct
8 Correct 7 ms 1844 KB Output is correct
9 Correct 9 ms 1852 KB Output is correct
10 Correct 9 ms 1876 KB Output is correct
11 Correct 7 ms 1876 KB Output is correct
12 Correct 7 ms 1904 KB Output is correct
13 Correct 7 ms 1876 KB Output is correct
14 Correct 7 ms 1876 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 5 ms 1620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 1 ms 468 KB Output is correct
5 Correct 7 ms 1876 KB Output is correct
6 Correct 8 ms 1876 KB Output is correct
7 Correct 7 ms 1876 KB Output is correct
8 Correct 7 ms 1844 KB Output is correct
9 Correct 9 ms 1852 KB Output is correct
10 Correct 9 ms 1876 KB Output is correct
11 Correct 7 ms 1876 KB Output is correct
12 Correct 7 ms 1904 KB Output is correct
13 Correct 7 ms 1876 KB Output is correct
14 Correct 7 ms 1876 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 5 ms 1620 KB Output is correct
17 Correct 8 ms 1876 KB Output is correct
18 Correct 8 ms 1876 KB Output is correct
19 Correct 8 ms 1904 KB Output is correct
20 Correct 7 ms 1876 KB Output is correct
21 Correct 10 ms 1880 KB Output is correct
22 Correct 11 ms 1876 KB Output is correct
23 Correct 10 ms 1904 KB Output is correct
24 Correct 8 ms 1856 KB Output is correct
25 Correct 8 ms 1876 KB Output is correct
26 Correct 8 ms 1904 KB Output is correct
27 Correct 3 ms 1108 KB Output is correct
28 Correct 5 ms 1708 KB Output is correct
29 Correct 6 ms 1652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 1 ms 468 KB Output is correct
5 Correct 7 ms 1876 KB Output is correct
6 Correct 8 ms 1876 KB Output is correct
7 Correct 7 ms 1876 KB Output is correct
8 Correct 7 ms 1844 KB Output is correct
9 Correct 9 ms 1852 KB Output is correct
10 Correct 9 ms 1876 KB Output is correct
11 Correct 7 ms 1876 KB Output is correct
12 Correct 7 ms 1904 KB Output is correct
13 Correct 7 ms 1876 KB Output is correct
14 Correct 7 ms 1876 KB Output is correct
15 Correct 2 ms 980 KB Output is correct
16 Correct 5 ms 1620 KB Output is correct
17 Correct 8 ms 1876 KB Output is correct
18 Correct 8 ms 1876 KB Output is correct
19 Correct 8 ms 1904 KB Output is correct
20 Correct 7 ms 1876 KB Output is correct
21 Correct 10 ms 1880 KB Output is correct
22 Correct 11 ms 1876 KB Output is correct
23 Correct 10 ms 1904 KB Output is correct
24 Correct 8 ms 1856 KB Output is correct
25 Correct 8 ms 1876 KB Output is correct
26 Correct 8 ms 1904 KB Output is correct
27 Correct 3 ms 1108 KB Output is correct
28 Correct 5 ms 1708 KB Output is correct
29 Correct 6 ms 1652 KB Output is correct
30 Correct 1256 ms 214928 KB Output is correct
31 Correct 1301 ms 219040 KB Output is correct
32 Correct 1516 ms 221952 KB Output is correct
33 Correct 1619 ms 222264 KB Output is correct
34 Correct 1355 ms 223652 KB Output is correct
35 Correct 1634 ms 224624 KB Output is correct
36 Correct 1241 ms 223308 KB Output is correct
37 Correct 1482 ms 224000 KB Output is correct
38 Correct 1357 ms 223652 KB Output is correct
39 Correct 1537 ms 224184 KB Output is correct
40 Correct 757 ms 213224 KB Output is correct
41 Correct 758 ms 213136 KB Output is correct
42 Correct 791 ms 215640 KB Output is correct
43 Correct 755 ms 215632 KB Output is correct
44 Correct 791 ms 217820 KB Output is correct
45 Correct 768 ms 217640 KB Output is correct
46 Correct 761 ms 217652 KB Output is correct
47 Correct 786 ms 217860 KB Output is correct
48 Correct 789 ms 217856 KB Output is correct
49 Correct 812 ms 217904 KB Output is correct
50 Correct 5 ms 1720 KB Output is correct
51 Correct 6 ms 1732 KB Output is correct
52 Correct 5 ms 1760 KB Output is correct