답안 #486519

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486519 2021-11-11T21:50:48 Z rainboy Relay (COI16_relay) C
0 / 100
1 ms 332 KB
#include <stdio.h>

#define N	100000
#define M	100000

unsigned int X = 12345;

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

long long cross2(int x1, int y1, int x2, int y2) {
	return (long long) x1 * y2 - (long long) x2 * y1;
}

int xx[N + M], yy[N + M], xx_[N * 2], yy_[N * 2], idx[N * 2], n, m;

long long cross(int i, int j, int k) {
	return cross2(xx[j] - xx[i], yy[j] - yy[i], xx[k] - xx[i], yy[k] - yy[i]);
}

int compare1(int i, int j) {
	return xx[i] != xx[j] ? xx[i] - xx[j] : yy[i] - yy[j];
}

int compare2(int x1, int y1, int x2, int y2) {
	int sgn1 = x1 < 0 || x1 == 0 && y1 < 0 ? -1 : 1;
	int sgn2 = x2 < 0 || x2 == 0 && y2 < 0 ? -1 : 1;
	long long c;

	if (sgn1 != sgn2)
		return sgn1 - sgn2;
	c = cross2(x1, y1, x2, y2);
	return c == 0 ? 0 : (c > 0 ? -1 : 1);
}

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 = compare2(xx_[ii[j]], yy_[ii[j]], xx_[i_], yy_[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 intersect(int l1, int r1, int l2, int r2) {
	int l1r1 = l1 <= r1, r1l2 = r1 < l2, l2r2 = l2 <= r2, r2l1 = r2 < l1;

	return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
}

int l, r;

void find_tangents(int o, int *p, int *q) {
	int lower, upper;

	if (compare1(o, n + l) < 0) {
		lower = -1, upper = (r - l + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (cross(o, n + (l + i) % m, n + (l + i + 1) % m) >= 0)
				lower = i;
			else
				upper = i;
		}
		*p = (l + upper) % m;
		lower = -1, upper = (l - r + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (cross(o, n + (l - i + m) % m, n + (l - (i + 1) + m) % m) <= 0)
				lower = i;
			else
				upper = i;
		}
		*q = (l - upper + m) % m;
	} else if (compare1(o, n + r) > 0) {
		lower = -1, upper = (l - r + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (cross(o, n + (r + i) % m, n + (r + i + 1) % m) >= 0)
				lower = i;
			else
				upper = i;
		}
		*p = (r + upper) % m;
		lower = -1, upper = (r - l + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (cross(o, n + (r - i + m) % m, n + (r - (i + 1) + m) % m) <= 0)
				lower = i;
			else
				upper = i;
		}
		*q = (r - upper + m) % m;
	} else if (cross(o, n + l, n + r) > 0) {
		lower = -1, upper = (r - l + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (compare1(o, n + (r - i + m) % m) < 0 && cross(o, n + (r - i + m) % m, n + (r - (i + 1) + m) % m) > 0)
				lower = i;
			else
				upper = i;
		}
		*p = (r - upper + m) % m;
		lower = -1, upper = (r - l + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (compare1(o, n + (l + i) % m) > 0 && cross(o, n + (l + i) % m, n + (l + i + 1) % m) < 0)
				lower = i;
			else
				upper = i;
		}
		*q = (l + upper) % m;
	} else {
		lower = -1, upper = (l - r + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (compare1(o, n + (l - i + m) % m) < 0 && cross(o, n + (l - i + m) % m, n + (l - (i + 1) + m) % m) > 0)
				lower = i;
			else
				upper = i;
		}
		*p = (l - upper + m) % m;
		lower = -1, upper = (l - r + m) % m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;

			if (compare1(o, n + (r + i) % m) > 0 && cross(o, n + (r + i) % m, n + (r + i + 1) % m) < 0)
				lower = i;
			else
				upper = i;
		}
		*q = (r + upper) % m;
	}
}

int main() {
	static int ii[N * 2], dd[N * 2 + 1];
	int n_, i, j, k;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	scanf("%d", &m);
	l = -1, r = -1;
	for (j = m - 1; j >= 0; j--) {
		scanf("%d%d", &xx[n + j], &yy[n + j]);
		if (l == -1 || compare1(n + l, n + j) > 0)
			l = j;
		if (r == -1 || compare1(n + r, n + j) < 0)
			r = j;
	}
	for (i = 0; i < n; i++) {
		int p, q;

		find_tangents(i, &p, &q);
		xx_[i << 1 | 0] = xx[i] - xx[n + p], yy_[i << 1 | 0] = yy[i] - yy[n + p];
		xx_[i << 1 | 1] = xx[n + q] - xx[i], yy_[i << 1 | 1] = yy[n + q] - yy[i];
	}
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	sort(ii, 0, n * 2);
	for (i = 0, n_ = 0; i < n * 2; i++)
		idx[ii[i]] = i + 1 == n * 2 || compare2(xx_[ii[i + 1]], yy_[ii[i + 1]], xx_[ii[i]], yy_[ii[i]]) != 0 ? n_++ : n_;
	for (i = 1; i < n; i++) {
		int l = idx[i << 1 | 0], r = idx[i << 1 | 1];

		if (intersect(idx[0 << 1 | 0], idx[0 << 1 | 1], l, r)) {
			if (l < r)
				dd[l]++, dd[r + 1]--;
			else
				dd[l]++, dd[n_]--, dd[0]++, dd[r + 1]--;
		}
	}
	for (i = 1; i < n_; i++)
		dd[i] += dd[i - 1];
	for (i = 0; i < n_; i++)
		if (dd[i] > 0)
			dd[i] = 1;
	for (i = 1; i < n_; i++)
		dd[i] += dd[i - 1];
	k = 0;
	for (i = 1; i < n; i++) {
		int l = idx[i << 1 | 0], r = idx[i << 1 | 1];

		if ((l < r ? dd[r] - (l == 0 ? 0 : dd[l - 1]) : dd[n_ - 1] - dd[l - 1] + dd[r]) > 0)
			k++;
	}
	printf("%d\n", k);
	return 0;
}

Compilation message

relay.c: In function 'compare2':
relay.c:27:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   27 |  int sgn1 = x1 < 0 || x1 == 0 && y1 < 0 ? -1 : 1;
      |                       ~~~~~~~~^~~~~~~~~
relay.c:28:31: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   28 |  int sgn2 = x2 < 0 || x2 == 0 && y2 < 0 ? -1 : 1;
      |                       ~~~~~~~~^~~~~~~~~
relay.c: In function 'intersect':
relay.c:62:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   62 |  return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
      |           ~~~~~~~~~~~~~^~~~~~~
relay.c:62:72: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   62 |  return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
      |                                                           ~~~~~~~~~~~~~^~~~~~~
relay.c:62:96: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   62 |  return !(l1r1 && r1l2 && l2r2 || r1l2 && l2r2 && r2l1 || l2r2 && r2l1 && l1r1 || r2l1 && l1r1 && r1l2);
      |                                                                                   ~~~~~~~~~~~~~^~~~~~~
relay.c: In function 'main':
relay.c:161:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
relay.c:163:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
relay.c:164:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |  scanf("%d", &m);
      |  ^~~~~~~~~~~~~~~
relay.c:167:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  167 |   scanf("%d%d", &xx[n + j], &yy[n + j]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -