Submission #946552

# Submission time Handle Problem Language Result Execution time Memory
946552 2024-03-14T18:26:55 Z rainboy Tapestries (POI13_gob) C
10 / 100
3 ms 348 KB
#include <stdio.h>
#include <string.h>

#define N	1000
#define INF	1e12
#define eps	1e-9

typedef long double ld;

int sgn(long long a) { return a == 0 ? 0 : (a > 0 ? 1 : -1); }
ld min(ld a, ld b) { return a < b ? a : b; }
ld max(ld a, ld b) { return a > b ? a : b; }
ld abs_(ld a) { return a > 0 ? a : -a; }

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;
}

ld cross2_(ld x1, ld y1, ld x2, ld y2) {
	return x1 * y2 - x2 * y1;
}

long long cross(int x0, int y0, int x1, int y1, int x2, int y2) {
	return cross2(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}

ld cross_(ld x0, ld y0, ld x1, ld y1, ld x2, ld y2) {
	return cross2_(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}

long long dot2(int x1, int y1, int x2, int y2) {
	return (long long) x1 * x2 + (long long) y1 * y2;
}

ld dot2_(ld x1, ld y1, ld x2, ld y2) {
	return (long long) x1 * x2 + (long long) y1 * y2;
}

ld dot_(ld x0, ld y0, ld x1, ld y1, ld x2, ld y2) {
	return dot2_(x1 - x0, y1 - y0, x2 - x0, y2 - y0);
}

int intersect_(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
	return sgn(cross(x1, y1, x2, y2, x3, y3)) * sgn(cross(x1, y1, x2, y2, x4, y4)) < 0 && sgn(cross(x3, y3, x4, y4, x1, y1)) * sgn(cross(x3, y3, x4, y4, x2, y2)) < 0;
}

int xx[N], yy[N], xx_[N * 2], yy_[N * 2]; char tt[N + 1]; ld x_, y_;
int aa[N], bb[N], cc[N];

int compare_ab(int i, int j) {
	int sgni, sgnj;
	long long c;

	sgni = aa[i] < 0 || aa[i] == 0 && bb[i] < 0 ? -1 : 1;
	sgnj = aa[j] < 0 || aa[j] == 0 && bb[j] < 0 ? -1 : 1;
	if (sgni != sgnj)
		return sgni - sgnj;
	c = cross2(aa[i], bb[i], aa[j], bb[j]);
	return c == 0 ? 0 : (c < 0 ? -1 : 1);
}

int compare_xy_(int i, int j) {
	int sgni, sgnj;
	long long c;

	sgni = xx_[i] < x_ || xx_[i] == x_ && yy_[i] < y_ ? -1 : 1;
	sgnj = xx_[j] < x_ || xx_[j] == x_ && yy_[j] < y_ ? -1 : 1;
	if (sgni != sgnj)
		return sgni - sgnj;
	c = cross_(x_, y_, xx_[i], yy_[i], xx_[j], yy_[j]);
	return c == 0 ? 0 : (c < 0 ? -1 : 1);
}

int compare_xy__(int i, int j) {
	int c = compare_xy_(i, j);

	return c != 0 ? c : (j & 1) - (i & 1);
}

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;
	}
}

void intersect(int i, int j, ld *x, ld *y) {
	long long c;

	c = cross2(aa[i], bb[i], aa[j], bb[j]);
	*x = (ld) -cross2(cc[i], bb[i], cc[j], bb[j]) / c;
	*y = (ld) -cross2(aa[i], cc[i], aa[j], cc[j]) / c;
}

int check(int i, int j, int k) {
	long long cij, cjk;
	ld x1, y1, x2, y2;

	cij = cross2(aa[i], bb[i], aa[j], bb[j]);
	cjk = cross2(aa[j], bb[j], aa[k], bb[k]);
	if (cij == 0) {
		if (dot2(aa[i], bb[i], aa[j], bb[j]) > 0) {
			if ((aa[i] != 0 ? cross2(cc[i], aa[i], cc[j], aa[j]) * sgn(aa[i]) : cross2(cc[i], bb[i], cc[j], bb[j]) * sgn(bb[i])) > 0)
				return 1;
		} else {
			if ((aa[i] != 0 ? cross2(cc[i], aa[i], cc[j], aa[j]) * sgn(aa[i]) : cross2(cc[i], bb[i], cc[j], bb[j]) * sgn(bb[i])) < 0)
				return -1;
		}
	}
	if (cjk == 0) {
		if (dot2(aa[k], bb[k], aa[j], bb[j]) > 0) {
			if ((aa[k] != 0 ? cross2(cc[k], aa[k], cc[j], aa[j]) * sgn(aa[k]) : cross2(cc[k], bb[k], cc[j], bb[j]) * sgn(bb[k])) > 0)
				return 1;
		} else {
			if ((aa[k] != 0 ? cross2(cc[k], aa[k], cc[j], aa[j]) * sgn(aa[k]) : cross2(cc[k], bb[k], cc[j], bb[j]) * sgn(bb[k])) < 0)
				return -1;
		}
	}
	if (cij >= 0 || cjk >= 0)
		return 0;
	intersect(i, j, &x1, &y1), intersect(j, k, &x2, &y2);
	if (cross2_(x2 - x1, y2 - y1, aa[j], bb[j]) <= 0)
		return cross2(aa[k], bb[k], aa[i], bb[i]) > 0 ? 1 : -1;
	return 0;
}

int iq[N + 1], pq[N], cnt;

int lt(int i, int j) {
	int xi0 = xx[i << 1 | 0], yi0 = yy[i << 1 | 0], xi1 = xx[i << 1 | 0], yi1 = yy[i << 1 | 0];
	int xj0 = xx[j << 1 | 0], yj0 = yy[j << 1 | 0], xj1 = xx[j << 1 | 0], yj1 = yy[j << 1 | 0];
	long long c0, c1;

	c0 = cross(xi0, yi0, xi1, yi1, xj0, yj0), c1 = cross(xi0, yi0, xi1, yi1, xj1, yj1);
	if (sgn(c0) * sgn(c1) >= 0)
		return c0 + c1 > 0;
	c0 = cross(xj0, yj0, xj1, yj1, xi0, yi0), c1 = cross(xj0, yj0, xj1, yj1, xi1, yi1);
	return c0 + c1 < 0;
}

int p2(int p) {
	return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_add(int i) {
	pq[i] = ++cnt, pq_up(i);
}

void pq_remove(int i) {
	if (pq[i]) {
		int j = iq[cnt--];

		if (j != i)
			pq[j] = pq[i], pq_up(j), pq_dn(j);
		pq[i] = 0;
	}
}

int pq_first() {
	return iq[1];
}

int main() {
	int t;

	scanf("%d", &t);
	while (t--) {
		static int ii[N * 2];
		static char s[2];
		int n, n_, head, h, i, i1, i2, j, j1, j2, k, c, tmp;
		long long q;
		ld t, t1, t2;

		scanf("%d", &n);
		for (i = 0; i < n; i++)
			scanf("%d%d", &xx[i], &yy[i]);
		for (i = 0; i < n; i++) {
			scanf("%s", s);
			tt[i] = s[0];
		}
		for (i = 0; i < n; i++) {
			aa[i] = yy[i] - yy[(i + 1) % n];
			bb[i] = xx[(i + 1) % n] - xx[i];
			cc[i] = cross2(xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n]);
		}
		k = 0;
		for (i = 0; i < n; i++)
			if (tt[(i - 1 + n) % n] == 'C' && tt[i] == 'S')
				k++;
		if (k == 0) {
			n_ = 0;
			for (i = 0; i < n; i++)
				if (tt[i] == 'S')
					ii[n_++] = i;
			compare = compare_ab, sort(ii, 0, n_);
			head = cnt = 0;
			for (h = 0; h < n_; h++) {
				i = ii[h];
				if (cnt && (c = check(ii[head + cnt - 1], i, ii[head]))) {
					if (c == -1) {
						printf("NIE\n");
						goto end;
					}
					continue;
				}
				while (cnt >= 2 && (c = check(ii[head + cnt - 2], ii[head + cnt - 1], i))) {
					if (c == -1) {
						printf("NIE\n");
						goto end;
					}
					cnt--;
				}
				while (cnt >= 2 && (c = check(i, ii[head], ii[head + 1]))) {
					if (c == -1) {
						printf("NIE\n");
						goto end;
					}
					head++, cnt--;
				}
				ii[head + cnt++] = i;
			}
			printf(tt[i] == 'C' ? "NIE\n" : "TAK\n");
		} else if (k == 1) {
			i1 = -1, i2 = -1;
			for (i = 0; i < n; i++)
				if (tt[(i - 1 + n) % n] == 'S' && tt[i] == 'C')
					i1 = i;
				else if (tt[(i - 1 + n) % n] == 'C' && tt[i] == 'S')
					i2 = i;
			if ((i1 - i2 + n) % n == 1) {
				printf("NIE\n");
				goto end;
			}
			if (sgn(cross(xx[(i1 - 1 + n) % n], yy[(i1 - 1 + n) % n], xx[i1], yy[i1], xx[i2], yy[i2])) * sgn(cross(xx[i1], yy[i1], xx[i2], yy[i2], xx[(i2 + 1) % n], yy[(i2 + 1) % n])) > 0) {
				printf("NIE\n");
				goto end;
			}
			for (i = 0; i < n; i++)
				if (tt[i] == 'C' && intersect_(xx[i1], yy[i1], xx[i2], yy[i2], xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n])) {
					printf("NIE\n");
					goto end;
				}
			if (cross(xx[(i1 - 1 + n) % n], yy[(i1 - 1 + n) % n], xx[i1], yy[i1], xx[i2], yy[i2]) > 0) {
				if (cross(xx[i1], yy[i1], xx[i2], yy[i2], xx[(i1 + 1) % n], yy[(i1 + 1) % n]) < 0) {
					printf("NIE\n");
					goto end;
				}
			} else {
				if (cross(xx[i1], yy[i1], xx[i2], yy[i2], xx[(i1 + 1) % n], yy[(i1 + 1) % n]) > 0) {
					printf("NIE\n");
					goto end;
				}
				tmp = i1, i1 = i2, i2 = tmp;
			}
			t1 = -INF, t2 = INF;
			for (i = 0; i < n; i++)
				if (tt[i] == 'S') {
					q = cross2(xx[i2] - xx[i1], yy[i2] - yy[i1], xx[(i + 1) % n] - xx[i], yy[(i + 1) % n] - yy[i]);
					if (q == 0) {
						if (cross(xx[i1], yy[i1], xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n]) >= 0) {
							printf("NIE\n");
							goto end;
						}
					} else {
						t = (ld) cross2(xx[i] - xx[i1], yy[i] - yy[i1], xx[(i + 1) % n] - xx[i], yy[(i + 1) % n] - yy[i]) / q;
						if (q > 0)
							t1 = max(t1, t);
						else
							t2 = min(t2, t);
					}
				}
			printf(t2 - t1 > eps ? "TAK\n" : "NIE\n");
		} else {
			i1 = -1, j1 = -1, i2 = -1, j2 = -1;
			for (i = 0; i < n; i++)
				if (tt[(i - 1 + n) % n] == 'S' && tt[i] == 'C') {
					j = i;
					while (tt[j] == 'C')
						j = (j + 1) % n;
					if (i1 == -1)
						i1 = i, j1 = j;
					else {
						i2 = i, j2 = j;
						break;
					}
				}
			q = cross2(xx[j1] - xx[i1], yy[j1] - yy[i1], xx[j2] - xx[i2], yy[j2] - yy[i2]);
			if (q == 0) {
				printf("NIE\n");
				goto end;
			}
			t = (ld) cross2(xx[i2] - xx[i1], yy[i2] - yy[i1], xx[j2] - xx[i2], yy[j2] - yy[i2]) / q;
			x_ = xx[i1] + (xx[j1] - xx[i1]) * t, y_ = yy[i1] + (yy[j1] - yy[i1]) * t;
			i = n - 1;
			while (tt[i] == 'C')
				i--;
			for (j = 0; j < n; j++)
				if (tt[j] == 'S') {
					if (abs_(cross_(x_, y_, xx[(i + 1) % n], yy[(i + 1) % n], xx[j], yy[j])) > eps || dot_(x_, y_, xx[(i + 1) % n], yy[(i + 1) % n], xx[j], yy[j]) <= eps)
						goto end;
					if (cross_(x_, y_, xx[j], yy[j], xx[(j + 1) % n], yy[(j + 1) % n]) >= 0)
						goto end;
					i = j;
				}
			n_ = 0;
			for (i = 0; i < n; i++) {
				c = cross_(x_, y_, xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n]);
				if (c < -eps) {
					xx_[i << 1 | 0] = xx[i], yy_[i << 1 | 0] = yy[i];
					xx_[i << 1 | 1] = xx[(i + 1) % n], yy_[i << 1 | 1] = yy[(i + 1) % n];
					ii[n_++] = i << 1 | 0, ii[n_++] = i << 1 | 1;
				} else if (c > eps) {
					xx_[i << 1 | 0] = xx[(i + 1) % n], yy_[i << 1 | 0] = yy[(i + 1) % n];
					xx_[i << 1 | 1] = xx[i], yy_[i << 1 | 1] = yy[i];
					ii[n_++] = i << 1 | 0, ii[n_++] = i << 1 | 1;
				} else {
					if (dot_(x_, y_, xx[i], yy[i], xx[(i + 1) % n], yy[(i + 1) % n]) <= eps) {
						printf("NIE\n");
						goto end;
					}
				}
			}
			compare = compare_xy__, sort(ii, 0, n_);
			memset(pq, 0, n * sizeof *pq), cnt = 0;
			for (i = 0; i < n_; i++)
				if ((ii[i] & 1) == 0)
					pq_add(ii[i] >> 1);
				else
					pq_remove(ii[i] >> 1);
			if (cnt % 2 == 0) {
				printf("NIE\n");
				goto end;
			}
			for (i = 0; i < n_; i++) {
				if ((ii[i] & 1) == 0)
					pq_add(ii[i] >> 1);
				else
					pq_remove(ii[i] >> 1);
				if ((i + 1 == n_ || compare_xy_(ii[i + 1], ii[i]) != 0) && tt[pq_first()] != 'S')
					goto end;
			}
			printf("TAK\n");
		}
end:;
	}
	return 0;
}

Compilation message

gob.c: In function 'compare_ab':
gob.c:60:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   60 |  sgni = aa[i] < 0 || aa[i] == 0 && bb[i] < 0 ? -1 : 1;
      |                      ~~~~~~~~~~~^~~~~~~~~~~~
gob.c:61:33: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   61 |  sgnj = aa[j] < 0 || aa[j] == 0 && bb[j] < 0 ? -1 : 1;
      |                      ~~~~~~~~~~~^~~~~~~~~~~~
gob.c: In function 'compare_xy_':
gob.c:72:37: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   72 |  sgni = xx_[i] < x_ || xx_[i] == x_ && yy_[i] < y_ ? -1 : 1;
      |                        ~~~~~~~~~~~~~^~~~~~~~~~~~~~
gob.c:73:37: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   73 |  sgnj = xx_[j] < x_ || xx_[j] == x_ && yy_[j] < y_ ? -1 : 1;
      |                        ~~~~~~~~~~~~~^~~~~~~~~~~~~~
gob.c: In function 'main':
gob.c:205:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  205 |  scanf("%d", &t);
      |  ^~~~~~~~~~~~~~~
gob.c:213:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  213 |   scanf("%d", &n);
      |   ^~~~~~~~~~~~~~~
gob.c:215:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  215 |    scanf("%d%d", &xx[i], &yy[i]);
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gob.c:217:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  217 |    scanf("%s", s);
      |    ^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Incorrect 0 ms 344 KB Output isn't correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Incorrect 0 ms 348 KB Output isn't correct
5 Incorrect 0 ms 348 KB Output isn't correct
6 Incorrect 1 ms 348 KB Output isn't correct
7 Incorrect 1 ms 348 KB Output isn't correct
8 Incorrect 2 ms 348 KB Output isn't correct
9 Incorrect 3 ms 348 KB Output isn't correct
10 Correct 1 ms 348 KB Output is correct