답안 #544755

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544755 2022-04-02T16:28:25 Z rainboy Mixture (BOI20_mixture) C
100 / 100
197 ms 29448 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define M	200000
#define MD	0x7fffffff

unsigned int Z = 12345, X = 12345;

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

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

long long gcd(long long a, long long b) {
	return b == 0 ? a : gcd(b, a % b);
}

long long *ox[M], *oy[M]; int *oc[M], oo[M];

int hash(long long x, long long y) {
	return ((x % MD * X % MD + y) % MD + MD) % M;
}

void ht_add(long long x, long long y, int c) {
	int h = hash(x, y), o;

	for (o = 0; o < oo[h]; o++)
		if (ox[h][o] == x && oy[h][o] == y) {
			oc[h][o] += c;
			return;
		}
	o = oo[h]++;
	if (o >= 2 && (o & o - 1) == 0) {
		ox[h] = (long long *) realloc(ox[h], o * 2 * sizeof *ox[h]);
		oy[h] = (long long *) realloc(oy[h], o * 2 * sizeof *oy[h]);
		oc[h] = (int *) realloc(oc[h], o * 2 * sizeof *oc[h]);
	}
	ox[h][o] = x, oy[h][o] = y, oc[h][o] = c;
}

int ht_get(long long x, long long y) {
	int h = hash(x, y), o;

	for (o = 0; o < oo[h]; o++)
		if (ox[h][o] == x && oy[h][o] == y)
			return oc[h][o];
	return 0;
}

long long xx[N], yy[N];

double cross(int i, int j) {
	return (double) xx[i] * yy[j] - (double) xx[j] * yy[i];
}

int cmp(int i, int j) {
	int sign_i = xx[i] < 0 || xx[i] == 0 && yy[i] < 0, sign_j = xx[j] < 0 || xx[j] == 0 && yy[j] < 0;
	double c = cross(i, j);

	return sign_i != sign_j ? sign_i - sign_j : (c != 0 ? (c < 0 ? -1 : 1) : i - j);
}

int bad(int i, int j) {
	double c = cross(i, j);

	return c > 0 || c == 0 && i > j;
}

int zz[1 + N], ll[1 + N], rr[1 + N], kk[1 + N], u_, l_, r_;

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

	zz[_] = rand_();
	kk[_] = k;
	return _++;
}

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

	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	c = cmp(kk[u], k);
	if (c < 0) {
		split(rr[u], k);
		rr[u] = l_, l_ = u;
	} else if (c > 0) {
		split(ll[u], k);
		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;
	}
}

int first(int u) {
	return ll[u] == 0 ? u : first(ll[u]);
}

int last(int u) {
	return rr[u] == 0 ? u : last(rr[u]);
}

int kbad;

void tr_add(int i) {
	if (u_ == 0)
		u_ = node(i);
	else {
		int l, r;

		split(u_, i);
		l = kk[l_ ? last(l_) : last(r_)], r = kk[r_ ? first(r_) : first(l_)];
		kbad += bad(l, i) + bad(i, r) - bad(l, r);
		u_ = merge(merge(l_, node(i)), r_);
	}
}

void tr_remove(int i) {
	if (ll[u_] == 0 && rr[u_] == 0)
		u_ = 0;
	else {
		int l, r;

		split(u_, i);
		l = kk[l_ ? last(l_) : last(r_)], r = kk[r_ ? first(r_) : first(l_)];
		kbad -= bad(l, i) + bad(i, r) - bad(l, r);
		u_ = merge(l_, r_);
	}
}

int main() {
	int n, q, h, x0, y0, z0, w0, k1;
	long long k2;

	scanf("%d%d%d%d", &x0, &y0, &z0, &q), w0 = x0 + y0 + z0;
	for (h = 0; h < M; h++) {
		ox[h] = (long long *) malloc(2 * sizeof *ox[h]);
		oy[h] = (long long *) malloc(2 * sizeof *oy[h]);
		oc[h] = (int *) malloc(2 * sizeof *oc[h]);
	}
	n = 0, k1 = 0, k2 = 0, kbad = 0;
	while (q--) {
		static char s[2];

		scanf("%s", s);
		if (s[0] == 'A') {
			int x, y, z, w;
			long long x_, y_, d;

			scanf("%d%d%d", &x, &y, &z), w = x + y + z;
			x_ = (long long) x * w0 - (long long) x0 * w;
			y_ = (long long) y * w0 - (long long) y0 * w;
			d = gcd(abs_(x_), abs_(y_));
			if (d != 0)
				x_ /= d, y_ /= d;
			xx[n] = x_, yy[n] = y_, n++;
			if (xx[n - 1] == 0 && yy[n - 1] == 0)
				k1++;
			else {
				k2 += ht_get(-x_, -y_);
				ht_add(x_, y_, 1);
				tr_add(n - 1);
			}
		} else {
			int i;

			scanf("%d", &i), i--;
			if (xx[i] == 0 && yy[i] == 0)
				k1--;
			else {
				ht_add(xx[i], yy[i], -1);
				k2 -= ht_get(-xx[i], -yy[i]);
				tr_remove(i);
			}
		}
		printf(k1 > 0 ? "1\n" : (k2 > 0 ? "2\n" : ((ll[u_] || rr[u_]) && kbad == 0 ? "3\n" : "0\n")));
	}
	return 0;
}

Compilation message

Mixture.c: In function 'ht_add':
Mixture.c:35:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   35 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
Mixture.c: In function 'cmp':
Mixture.c:59:39: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   59 |  int sign_i = xx[i] < 0 || xx[i] == 0 && yy[i] < 0, sign_j = xx[j] < 0 || xx[j] == 0 && yy[j] < 0;
      |                            ~~~~~~~~~~~^~~~~~~~~~~~
Mixture.c:59:86: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   59 |  int sign_i = xx[i] < 0 || xx[i] == 0 && yy[i] < 0, sign_j = xx[j] < 0 || xx[j] == 0 && yy[j] < 0;
      |                                                                           ~~~~~~~~~~~^~~~~~~~~~~~
Mixture.c: In function 'bad':
Mixture.c:68:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   68 |  return c > 0 || c == 0 && i > j;
      |                  ~~~~~~~^~~~~~~~
Mixture.c: In function 'main':
Mixture.c:155:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |  scanf("%d%d%d%d", &x0, &y0, &z0, &q), w0 = x0 + y0 + z0;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mixture.c:165:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |   scanf("%s", s);
      |   ^~~~~~~~~~~~~~
Mixture.c:170:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |    scanf("%d%d%d", &x, &y, &z), w = x + y + z;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Mixture.c:187:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |    scanf("%d", &i), i--;
      |    ^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23828 KB Output is correct
2 Correct 23 ms 23716 KB Output is correct
3 Correct 21 ms 23840 KB Output is correct
4 Correct 20 ms 23736 KB Output is correct
5 Correct 20 ms 23752 KB Output is correct
6 Correct 20 ms 23884 KB Output is correct
7 Correct 31 ms 23756 KB Output is correct
8 Correct 24 ms 23740 KB Output is correct
9 Correct 19 ms 23728 KB Output is correct
10 Correct 18 ms 23724 KB Output is correct
11 Correct 19 ms 23764 KB Output is correct
12 Correct 19 ms 23724 KB Output is correct
13 Correct 20 ms 23756 KB Output is correct
14 Correct 22 ms 23684 KB Output is correct
15 Correct 23 ms 23804 KB Output is correct
16 Correct 21 ms 23776 KB Output is correct
17 Correct 20 ms 23872 KB Output is correct
18 Correct 22 ms 23700 KB Output is correct
19 Correct 21 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23828 KB Output is correct
2 Correct 23 ms 23716 KB Output is correct
3 Correct 21 ms 23840 KB Output is correct
4 Correct 20 ms 23736 KB Output is correct
5 Correct 20 ms 23752 KB Output is correct
6 Correct 20 ms 23884 KB Output is correct
7 Correct 31 ms 23756 KB Output is correct
8 Correct 24 ms 23740 KB Output is correct
9 Correct 19 ms 23728 KB Output is correct
10 Correct 18 ms 23724 KB Output is correct
11 Correct 19 ms 23764 KB Output is correct
12 Correct 19 ms 23724 KB Output is correct
13 Correct 20 ms 23756 KB Output is correct
14 Correct 22 ms 23684 KB Output is correct
15 Correct 23 ms 23804 KB Output is correct
16 Correct 21 ms 23776 KB Output is correct
17 Correct 20 ms 23872 KB Output is correct
18 Correct 22 ms 23700 KB Output is correct
19 Correct 21 ms 23808 KB Output is correct
20 Correct 21 ms 23772 KB Output is correct
21 Correct 20 ms 23720 KB Output is correct
22 Correct 22 ms 23788 KB Output is correct
23 Correct 25 ms 24116 KB Output is correct
24 Correct 22 ms 23784 KB Output is correct
25 Correct 32 ms 23856 KB Output is correct
26 Correct 20 ms 23760 KB Output is correct
27 Correct 27 ms 23988 KB Output is correct
28 Correct 20 ms 23980 KB Output is correct
29 Correct 20 ms 24148 KB Output is correct
30 Correct 31 ms 23944 KB Output is correct
31 Correct 19 ms 23816 KB Output is correct
32 Correct 21 ms 24448 KB Output is correct
33 Correct 32 ms 23752 KB Output is correct
34 Correct 21 ms 24384 KB Output is correct
35 Correct 23 ms 24328 KB Output is correct
36 Correct 21 ms 24404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23828 KB Output is correct
2 Correct 23 ms 23716 KB Output is correct
3 Correct 21 ms 23840 KB Output is correct
4 Correct 20 ms 23736 KB Output is correct
5 Correct 20 ms 23752 KB Output is correct
6 Correct 20 ms 23884 KB Output is correct
7 Correct 31 ms 23756 KB Output is correct
8 Correct 24 ms 23740 KB Output is correct
9 Correct 19 ms 23728 KB Output is correct
10 Correct 18 ms 23724 KB Output is correct
11 Correct 19 ms 23764 KB Output is correct
12 Correct 19 ms 23724 KB Output is correct
13 Correct 20 ms 23756 KB Output is correct
14 Correct 22 ms 23684 KB Output is correct
15 Correct 23 ms 23804 KB Output is correct
16 Correct 21 ms 23776 KB Output is correct
17 Correct 20 ms 23872 KB Output is correct
18 Correct 22 ms 23700 KB Output is correct
19 Correct 21 ms 23808 KB Output is correct
20 Correct 21 ms 23772 KB Output is correct
21 Correct 20 ms 23720 KB Output is correct
22 Correct 22 ms 23788 KB Output is correct
23 Correct 25 ms 24116 KB Output is correct
24 Correct 22 ms 23784 KB Output is correct
25 Correct 32 ms 23856 KB Output is correct
26 Correct 20 ms 23760 KB Output is correct
27 Correct 27 ms 23988 KB Output is correct
28 Correct 20 ms 23980 KB Output is correct
29 Correct 20 ms 24148 KB Output is correct
30 Correct 31 ms 23944 KB Output is correct
31 Correct 19 ms 23816 KB Output is correct
32 Correct 21 ms 24448 KB Output is correct
33 Correct 32 ms 23752 KB Output is correct
34 Correct 21 ms 24384 KB Output is correct
35 Correct 23 ms 24328 KB Output is correct
36 Correct 21 ms 24404 KB Output is correct
37 Correct 21 ms 24068 KB Output is correct
38 Correct 22 ms 24420 KB Output is correct
39 Correct 23 ms 24332 KB Output is correct
40 Correct 33 ms 24520 KB Output is correct
41 Correct 22 ms 24512 KB Output is correct
42 Correct 31 ms 24444 KB Output is correct
43 Correct 21 ms 24532 KB Output is correct
44 Correct 27 ms 24528 KB Output is correct
45 Correct 22 ms 24532 KB Output is correct
46 Correct 27 ms 24660 KB Output is correct
47 Correct 26 ms 24672 KB Output is correct
48 Correct 28 ms 24148 KB Output is correct
49 Correct 34 ms 24140 KB Output is correct
50 Correct 23 ms 24116 KB Output is correct
51 Correct 26 ms 24764 KB Output is correct
52 Correct 30 ms 24312 KB Output is correct
53 Correct 31 ms 24652 KB Output is correct
54 Correct 25 ms 24776 KB Output is correct
55 Correct 27 ms 23836 KB Output is correct
56 Correct 28 ms 23892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 23828 KB Output is correct
2 Correct 23 ms 23716 KB Output is correct
3 Correct 21 ms 23840 KB Output is correct
4 Correct 20 ms 23736 KB Output is correct
5 Correct 20 ms 23752 KB Output is correct
6 Correct 20 ms 23884 KB Output is correct
7 Correct 31 ms 23756 KB Output is correct
8 Correct 24 ms 23740 KB Output is correct
9 Correct 19 ms 23728 KB Output is correct
10 Correct 18 ms 23724 KB Output is correct
11 Correct 19 ms 23764 KB Output is correct
12 Correct 19 ms 23724 KB Output is correct
13 Correct 20 ms 23756 KB Output is correct
14 Correct 22 ms 23684 KB Output is correct
15 Correct 23 ms 23804 KB Output is correct
16 Correct 21 ms 23776 KB Output is correct
17 Correct 20 ms 23872 KB Output is correct
18 Correct 22 ms 23700 KB Output is correct
19 Correct 21 ms 23808 KB Output is correct
20 Correct 21 ms 23772 KB Output is correct
21 Correct 20 ms 23720 KB Output is correct
22 Correct 22 ms 23788 KB Output is correct
23 Correct 25 ms 24116 KB Output is correct
24 Correct 22 ms 23784 KB Output is correct
25 Correct 32 ms 23856 KB Output is correct
26 Correct 20 ms 23760 KB Output is correct
27 Correct 27 ms 23988 KB Output is correct
28 Correct 20 ms 23980 KB Output is correct
29 Correct 20 ms 24148 KB Output is correct
30 Correct 31 ms 23944 KB Output is correct
31 Correct 19 ms 23816 KB Output is correct
32 Correct 21 ms 24448 KB Output is correct
33 Correct 32 ms 23752 KB Output is correct
34 Correct 21 ms 24384 KB Output is correct
35 Correct 23 ms 24328 KB Output is correct
36 Correct 21 ms 24404 KB Output is correct
37 Correct 21 ms 24068 KB Output is correct
38 Correct 22 ms 24420 KB Output is correct
39 Correct 23 ms 24332 KB Output is correct
40 Correct 33 ms 24520 KB Output is correct
41 Correct 22 ms 24512 KB Output is correct
42 Correct 31 ms 24444 KB Output is correct
43 Correct 21 ms 24532 KB Output is correct
44 Correct 27 ms 24528 KB Output is correct
45 Correct 22 ms 24532 KB Output is correct
46 Correct 27 ms 24660 KB Output is correct
47 Correct 26 ms 24672 KB Output is correct
48 Correct 28 ms 24148 KB Output is correct
49 Correct 34 ms 24140 KB Output is correct
50 Correct 23 ms 24116 KB Output is correct
51 Correct 26 ms 24764 KB Output is correct
52 Correct 30 ms 24312 KB Output is correct
53 Correct 31 ms 24652 KB Output is correct
54 Correct 25 ms 24776 KB Output is correct
55 Correct 27 ms 23836 KB Output is correct
56 Correct 28 ms 23892 KB Output is correct
57 Correct 26 ms 24032 KB Output is correct
58 Correct 26 ms 24788 KB Output is correct
59 Correct 29 ms 24752 KB Output is correct
60 Correct 28 ms 24732 KB Output is correct
61 Correct 38 ms 24856 KB Output is correct
62 Correct 45 ms 24788 KB Output is correct
63 Correct 196 ms 29272 KB Output is correct
64 Correct 184 ms 29260 KB Output is correct
65 Correct 158 ms 28188 KB Output is correct
66 Correct 154 ms 28252 KB Output is correct
67 Correct 31 ms 23752 KB Output is correct
68 Correct 33 ms 23784 KB Output is correct
69 Correct 197 ms 29448 KB Output is correct
70 Correct 163 ms 28468 KB Output is correct
71 Correct 174 ms 28464 KB Output is correct
72 Correct 176 ms 29196 KB Output is correct
73 Correct 195 ms 29308 KB Output is correct