Submission #532367

# Submission time Handle Problem Language Result Execution time Memory
532367 2022-03-02T18:56:48 Z rainboy None (JOI14_ho_t5) C
100 / 100
193 ms 10456 KB
#include <stdio.h>
#include <string.h>

#define N	100004
#define N_	(1 << 18)	/* N_ = pow2(ceil(log2(N * 2))) */

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

unsigned int X = 12345;

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

int xx[N * 2], yy[N * 2], tt[N * 2], ii[N * 2], m;

int (*compare)(int, int);

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

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

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 ft[N * 2];

void ft_update(int i, int n, int x) {
	while (i < n) {
		ft[i] += x;
		i |= i + 1;
	}
}

int query(int i) {
	int x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

long long get_ve(int n) {
	int h;
	long long ve;

	ve = n;
	for (h = 0; h < m; h++) {
		int i = ii[h];

		if (tt[i] == 0)
			ve -= query(yy[i ^ 1]) - query(yy[i] - 1);
		else
			ft_update(yy[i], n * 2, tt[i]);
	}
	return ve;
}

int ds[N];

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

void join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
}

int st[N_ * 2], n_;

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

	if (st[l] == -2 || st[r] == -2)
		st[i] = -2;
	else if (st[l] == -1)
		st[i] = st[r];
	else if (st[r] == -1)
		st[i] = st[l];
	else
		st[i] = find(st[l]) != find(st[r]) ? -2 : st[l];
}

void pull(int i) {
	while (i > 1)
		pul(i >>= 1);
}

void st_update(int i, int u) {
	i += n_, st[i] = u, pull(i);
}

void join1(int i, int u) {
	if (st[i] == -1)
		return;
	if (st[i] != -2) {
		join(u, st[i]);
		return;
	}
	join1(i << 1 | 0, u), join1(i << 1 | 1, u);
	pul(i);
}

void join_(int l, int r, int u) {
	int l_ = l += n_, r_ = r += n_;

	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			join1(l++, u);
		if ((r & 1) == 0)
			join1(r--, u);
	}
	pull(l_), pull(r_);
}

int get_c(int n) {
	int h, i, c;

	n_ = 1;
	while (n_ < n * 2)
		n_ <<= 1;
	memset(st, -1, n_ * 2 * sizeof *st);
	memset(ds, -1, n * sizeof *ds);
	for (h = 0; h < m; h++) {
		i = ii[h];
		if (tt[i] == 0)
			join_(yy[i], yy[i ^ 1], i >> 1);
		else
			st_update(yy[i], tt[i] == 1 ? i >> 1 : -1);
	}
	c = 0;
	for (i = 0; i < n; i++)
		if (ds[i] < 0)
			c++;
	return c;
}

int main() {
	int w, h, n, i, y, tmp;

	scanf("%d%d%d", &w, &h, &n);
	for (i = 0; i < n * 2; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	xx[n << 1 | 0] = 0, yy[n << 1 | 0] = 0, xx[n << 1 | 1] = w, yy[n << 1 | 1] = 0;
	xx[n + 1 << 1 | 0] = w, yy[n + 1 << 1 | 0] = 0, xx[n + 1 << 1 | 1] = w, yy[n + 1 << 1 | 1] = h;
	xx[n + 2 << 1 | 0] = w, yy[n + 2 << 1 | 0] = h, xx[n + 2 << 1 | 1] = 0, yy[n + 2 << 1 | 1] = h;
	xx[n + 3 << 1 | 0] = 0, yy[n + 3 << 1 | 0] = h, xx[n + 3 << 1 | 1] = 0, yy[n + 3 << 1 | 1] = 0;
	n += 4;
	for (i = 0; i < n; i++) {
		if (xx[i << 1 | 0] > xx[i << 1 | 1])
			tmp = xx[i << 1 | 0], xx[i << 1 | 0] = xx[i << 1 | 1], xx[i << 1 | 1] = tmp;
		if (yy[i << 1 | 0] > yy[i << 1 | 1])
			tmp = yy[i << 1 | 0], yy[i << 1 | 0] = yy[i << 1 | 1], yy[i << 1 | 1] = tmp;
	}
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	for (i = 0; i < n * 2; i++)
		tt[i] = xx[i] == xx[i ^ 1] ? 0 : ((i & 1) == 0 ? 1 : -1);
	compare = compare_y, sort(ii, 0, n * 2);
	for (i = 0, y = 0; i < n * 2; i++)
		yy[ii[i]] = i + 1 == n * 2 || yy[ii[i + 1]] != yy[ii[i]] ? y++ : y;
	m = 0;
	for (i = 0; i < n; i++) {
		ii[m++] = i << 1 | 0;
		if (tt[i << 1 | 0] != 0)
			ii[m++] = i << 1 | 1;
	}
	compare = compare_xt, sort(ii, 0, m);
	printf("%lld\n", get_c(n) - get_ve(n));
	return 0;
}

Compilation message

2014_ho_t5.c: In function 'main':
2014_ho_t5.c:181:7: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  181 |  xx[n + 1 << 1 | 0] = w, yy[n + 1 << 1 | 0] = 0, xx[n + 1 << 1 | 1] = w, yy[n + 1 << 1 | 1] = h;
      |     ~~^~~
2014_ho_t5.c:181:31: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  181 |  xx[n + 1 << 1 | 0] = w, yy[n + 1 << 1 | 0] = 0, xx[n + 1 << 1 | 1] = w, yy[n + 1 << 1 | 1] = h;
      |                             ~~^~~
2014_ho_t5.c:181:55: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  181 |  xx[n + 1 << 1 | 0] = w, yy[n + 1 << 1 | 0] = 0, xx[n + 1 << 1 | 1] = w, yy[n + 1 << 1 | 1] = h;
      |                                                     ~~^~~
2014_ho_t5.c:181:79: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  181 |  xx[n + 1 << 1 | 0] = w, yy[n + 1 << 1 | 0] = 0, xx[n + 1 << 1 | 1] = w, yy[n + 1 << 1 | 1] = h;
      |                                                                             ~~^~~
2014_ho_t5.c:182:7: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  182 |  xx[n + 2 << 1 | 0] = w, yy[n + 2 << 1 | 0] = h, xx[n + 2 << 1 | 1] = 0, yy[n + 2 << 1 | 1] = h;
      |     ~~^~~
2014_ho_t5.c:182:31: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  182 |  xx[n + 2 << 1 | 0] = w, yy[n + 2 << 1 | 0] = h, xx[n + 2 << 1 | 1] = 0, yy[n + 2 << 1 | 1] = h;
      |                             ~~^~~
2014_ho_t5.c:182:55: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  182 |  xx[n + 2 << 1 | 0] = w, yy[n + 2 << 1 | 0] = h, xx[n + 2 << 1 | 1] = 0, yy[n + 2 << 1 | 1] = h;
      |                                                     ~~^~~
2014_ho_t5.c:182:79: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  182 |  xx[n + 2 << 1 | 0] = w, yy[n + 2 << 1 | 0] = h, xx[n + 2 << 1 | 1] = 0, yy[n + 2 << 1 | 1] = h;
      |                                                                             ~~^~~
2014_ho_t5.c:183:7: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  183 |  xx[n + 3 << 1 | 0] = 0, yy[n + 3 << 1 | 0] = h, xx[n + 3 << 1 | 1] = 0, yy[n + 3 << 1 | 1] = 0;
      |     ~~^~~
2014_ho_t5.c:183:31: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  183 |  xx[n + 3 << 1 | 0] = 0, yy[n + 3 << 1 | 0] = h, xx[n + 3 << 1 | 1] = 0, yy[n + 3 << 1 | 1] = 0;
      |                             ~~^~~
2014_ho_t5.c:183:55: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  183 |  xx[n + 3 << 1 | 0] = 0, yy[n + 3 << 1 | 0] = h, xx[n + 3 << 1 | 1] = 0, yy[n + 3 << 1 | 1] = 0;
      |                                                     ~~^~~
2014_ho_t5.c:183:79: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  183 |  xx[n + 3 << 1 | 0] = 0, yy[n + 3 << 1 | 0] = h, xx[n + 3 << 1 | 1] = 0, yy[n + 3 << 1 | 1] = 0;
      |                                                                             ~~^~~
2014_ho_t5.c:177:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |  scanf("%d%d%d", &w, &h, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
2014_ho_t5.c:179:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  179 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 15 ms 908 KB Output is correct
6 Correct 70 ms 3400 KB Output is correct
7 Correct 144 ms 6532 KB Output is correct
8 Correct 152 ms 10208 KB Output is correct
9 Correct 137 ms 10272 KB Output is correct
10 Correct 136 ms 10212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 16 ms 912 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 190 ms 6428 KB Output is correct
7 Correct 11 ms 972 KB Output is correct
8 Correct 132 ms 6084 KB Output is correct
9 Correct 119 ms 6180 KB Output is correct
10 Correct 120 ms 6088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 15 ms 908 KB Output is correct
34 Correct 70 ms 3400 KB Output is correct
35 Correct 144 ms 6532 KB Output is correct
36 Correct 152 ms 10208 KB Output is correct
37 Correct 137 ms 10272 KB Output is correct
38 Correct 136 ms 10212 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 16 ms 912 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 2 ms 332 KB Output is correct
44 Correct 190 ms 6428 KB Output is correct
45 Correct 11 ms 972 KB Output is correct
46 Correct 132 ms 6084 KB Output is correct
47 Correct 119 ms 6180 KB Output is correct
48 Correct 120 ms 6088 KB Output is correct
49 Correct 2 ms 336 KB Output is correct
50 Correct 1 ms 304 KB Output is correct
51 Correct 2 ms 336 KB Output is correct
52 Correct 86 ms 5280 KB Output is correct
53 Correct 73 ms 5308 KB Output is correct
54 Correct 86 ms 5312 KB Output is correct
55 Correct 182 ms 10204 KB Output is correct
56 Correct 143 ms 10312 KB Output is correct
57 Correct 178 ms 10184 KB Output is correct
58 Correct 193 ms 10328 KB Output is correct
59 Correct 118 ms 9164 KB Output is correct
60 Correct 119 ms 9160 KB Output is correct
61 Correct 136 ms 9140 KB Output is correct
62 Correct 132 ms 9308 KB Output is correct
63 Correct 180 ms 10260 KB Output is correct
64 Correct 150 ms 10456 KB Output is correct
65 Correct 180 ms 10296 KB Output is correct
66 Correct 177 ms 10312 KB Output is correct
67 Correct 174 ms 10312 KB Output is correct
68 Correct 173 ms 10208 KB Output is correct
69 Correct 167 ms 10144 KB Output is correct
70 Correct 180 ms 10192 KB Output is correct
71 Correct 102 ms 8328 KB Output is correct
72 Correct 134 ms 8648 KB Output is correct