답안 #927445

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927445 2024-02-14T21:26:40 Z rainboy Cell Automaton (JOI23_cell) C
100 / 100
966 ms 59780 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	100000
#define Q	500000
#define INF	0x7fffffff

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], xx_[N], yy[N], yy_[N], uu[N], vv[N]; long long tt_[N]; int n;
int tt[Q]; long long ans[Q]; int q;

int *ww;

int compare_w(int i, int j) {
	return ww[i] != ww[j] ? (ww[i] < ww[j] ? -1 : 1) : 0;
}

int compare_ux(int i, int j) {
	return uu[i] != uu[j] ? (uu[i] < uu[j] ? -1 : 1) : (xx[i] != xx[j] ? (xx[i] < xx[j] ? -1 : 1) : 0);
}

int compare_ti(int i, int j) {
	return tt_[i] != tt_[j] ? (tt_[i] < tt_[j] ? -1 : 1) : i - j;
}

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 compress(int *xx, int *xx_, int n) {
	static int ii[N];
	int i, x;

	for (i = 0; i < n; i++)
		ii[i] = i;
	ww = xx, compare = compare_w, sort(ii, 0, n);
	for (i = 0, x = 0; i < n; i++)
		xx_[ii[i]] = i + 1 == n || xx[ii[i + 1]] != xx[ii[i]] ? x++ : x;
}

int ft[N];

int min_(int i, int j) {
	return j == -1 || i != -1 && (vv[i] < vv[j] || vv[i] == vv[j] && i < j) ? i : j;
}

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

int query(int i) {
	int x = -1;

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

int *ej[N], eo[N];

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 aa[Q + 1]; long long bb[Q + 1];

void add(int t, int a, long long b) {
	int lower = -1, upper = q;

	while (upper - lower > 1) {
		int h = (lower + upper) / 2;

		if (tt[h] >= t)
			upper = h;
		else
			lower = h;
	}
	aa[upper] += a, bb[upper] += b;
}

int main() {
	static int ii[N], qu[N * 8], ii_[N * 16], jj_[N * 16];
	int m, cnt, h, i, i_, j, j_, jl, jr, r, o, t, t_, tmp;
	long long u, u_, ul, ur;

	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		scanf("%d%d", &xx[i], &yy[i]);
	for (h = 0; h < q; h++)
		scanf("%d", &tt[h]);
	for (i = 0; i < n; i++)
		uu[i] = xx[i] - yy[i], vv[i] = xx[i] + yy[i];
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
	compress(xx, xx_, n), compress(yy, yy_, n);
	for (r = 0; r < 4; r++) {
		for (i = 0; i < n; i++)
			ii[i] = i;
		compare = compare_ux, sort(ii, 0, n);
		memset(ft, -1, n * sizeof *ft);
		for (i = 0; i < n; i = j) {
			j = i + 1;
			while (j < n && uu[ii[j]] == uu[ii[i]])
				j++;
			for (h = i; h < j; h++) {
				i_ = ii[h], j_ = query(n - 1 - xx_[i_]);
				if (j_ != -1)
					append(i_, j_), append(j_, i_);
				update(n - 1 - xx_[i_], n, i_);
			}
			cnt = 0;
			for (h = i; h < j; h++) {
				i_ = ii[h];
				while (cnt && qu[cnt - 1] > i_)
					cnt--;
				if (cnt) {
					j_ = qu[cnt - 1];
					append(i_, j_), append(j_, i_);
				}
				qu[cnt++] = i_;
			}
		}
		memset(ft, -1, n * sizeof *ft);
		for (j = n; j > 0; j = i) {
			i = j - 1;
			while (i > 0 && uu[ii[i - 1]] == uu[ii[j - 1]])
				i--;
			for (h = i; h < j; h++) {
				i_ = ii[h], j_ = query(n - 1 - yy_[i_]);
				if (j_ != -1)
					append(i_, j_), append(j_, i_);
				update(n - 1 - yy_[i_], n, i_);
			}
		}
		for (i = 0; i < n; i++) {
			tmp = xx[i], xx[i] = -yy[i], yy[i] = tmp;
			tmp = xx_[i], xx_[i] = n - 1 - yy_[i], yy_[i] = tmp;
			tmp = uu[i], uu[i] = -vv[i], vv[i] = tmp;
		}
	}
	for (i = 0; i < n; i++) {
		for (o = 0; o < eo[i]; o++) {
			j = ej[i][o];
			tt_[j] = (long long) abs_(xx[j] - xx[i]) + abs_(yy[j] - yy[i]);
		}
		compare = compare_ti, sort(ej[i], 0, eo[i]);
	}
	m = 0;
	for (r = 0; r < 4; r++) {
		for (i = 0; i < n; i++) {
			u = uu[i], ul = -INF, jl = -1, ur = INF, jr = -1, t_ = -1, cnt = 0;
			for (o = 0; o < eo[i]; o++) {
				j = ej[i][o], t = ((long long) abs_(xx[j] - xx[i]) + abs_(yy[j] - yy[i]) + (i < j ? 2 : 1)) / 2;
				if (vv[j] < vv[i] || vv[j] == vv[i] && j > i) {
					if (vv[j] == vv[i])
						qu[cnt++] = j;
					continue;
				}
				if (ul != -INF && ur != INF && ul + t > ur - t + 1 && t != t_)
					break;
				t_ = t;
				if (uu[j] < u) {
					u_ = uu[j] + (i < j ? 0 : 1);
					if (ul == -INF) {
						ul = u_, jl = j;
						for (h = 0; h < cnt; h++)
							ii_[m] = qu[h], jj_[m] = j, m++;
					} else {
						ul = max(ul, u_);
						if (uu[jl] < uu[j] || uu[jl] == uu[j] && jl > j)
							jl = j;
					}
				} else {
					u_ = uu[j] - (i < j ? 0 : 1);
					if (ur == INF) {
						ur = u_, jr = j;
						for (h = 0; h < cnt; h++)
							ii_[m] = qu[h], jj_[m] = j, m++;
					} else {
						ur = min(ur, u_);
						if (uu[jr] > uu[j] || uu[jr] == uu[j] && jr > j)
							jr = j;
					}
				}
			}
			if (jl != -1 && jr != -1)
				ii_[m] = jl, jj_[m] = jr, m++;
		}
		for (i = 0; i < n; i++) {
			tmp = xx[i], xx[i] = -yy[i], yy[i] = tmp;
			tmp = uu[i], uu[i] = -vv[i], vv[i] = tmp;
		}
	}
	for (h = 0; h < m; h++) {
		i = ii_[h], j = jj_[h];
		append(i, j), append(j, i);
	}
	for (i = 0; i < n; i++) {
		for (o = 0; o < eo[i]; o++) {
			j = ej[i][o];
			tt_[j] = (long long) abs_(xx[j] - xx[i]) + abs_(yy[j] - yy[i]);
		}
		compare = compare_ti, sort(ej[i], 0, eo[i]);
	}
	for (r = 0; r < 4; r++) {
		for (i = 0; i < n; i++) {
			add(0, 1, 0);
			u = uu[i], ul = -INF, ur = INF, t = INF;
			for (o = 0; o <= eo[i]; o++) {
				if (o == eo[i]) {
					t = ul != -INF && ur != INF ? (ur - ul) / 2 + 1 : INF;
					break;
				}
				j = ej[i][o], t = ((long long) abs_(xx[j] - xx[i]) + abs_(yy[j] - yy[i]) + (i < j ? 2 : 1)) / 2;
				if (vv[j] < vv[i] || vv[j] == vv[i] && j > i)
					continue;
				if (ul != -INF && ur != INF && ul + t > ur - t) {
					t = (ur - ul) / 2 + 1;
					break;
				}
				if (uu[j] < u) {
					u_ = uu[j] + (i < j ? 0 : 1);
					if (ul == -INF)
						add(t, -1, (u - u_) / 2), ul = u_;
					else if (ul < u_)
						add(t, 0, (u - u_) / 2 - (u - ul) / 2), ul = u_;
				} else {
					u_ = uu[j] - (i < j ? 0 : 1);
					if (ur == INF)
						add(t, -1, (u_ - u + 2) / 2), ur = u_;
					else if (ur > u_)
						add(t, 0, (u_ - u + 2) / 2 - (ur - u + 2) / 2), ur = u_;
				}
				if (ul != -INF && ur != INF && ul + t > ur - t)
					break;
			}
			if (t != INF)
				add(t, 1, -((u - ul) / 2 + (ur - u + 2) / 2));
		}
		for (i = 0; i < n; i++) {
			tmp = xx[i], xx[i] = -yy[i], yy[i] = tmp;
			tmp = uu[i], uu[i] = -vv[i], vv[i] = tmp;
		}
	}
	for (h = 1; h < q; h++)
		aa[h] += aa[h - 1], bb[h] += bb[h - 1];
	for (h = 0; h < q; h++)
		ans[h] = tt[h] == 0 ? n : (long long) aa[h] * tt[h] + bb[h];
	for (h = 0; h < q; h++)
		printf("%lld\n", ans[h]);
	return 0;
}

Compilation message

cell.c: In function 'min_':
cell.c:74:64: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   74 |  return j == -1 || i != -1 && (vv[i] < vv[j] || vv[i] == vv[j] && i < j) ? i : j;
      |                                                 ~~~~~~~~~~~~~~~^~~~~~~~
cell.c:74:28: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   74 |  return j == -1 || i != -1 && (vv[i] < vv[j] || vv[i] == vv[j] && i < j) ? i : j;
      |                    ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cell.c: In function 'append':
cell.c:99:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   99 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
cell.c: In function 'main':
cell.c:193:41: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  193 |     if (vv[j] < vv[i] || vv[j] == vv[i] && j > i) {
      |                          ~~~~~~~~~~~~~~~^~~~~~~~
cell.c:209:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  209 |       if (uu[jl] < uu[j] || uu[jl] == uu[j] && jl > j)
      |                             ~~~~~~~~~~~~~~~~^~~~~~~~~
cell.c:220:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  220 |       if (uu[jr] > uu[j] || uu[jr] == uu[j] && jr > j)
      |                             ~~~~~~~~~~~~~~~~^~~~~~~~~
cell.c:254:41: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  254 |     if (vv[j] < vv[i] || vv[j] == vv[i] && j > i)
      |                          ~~~~~~~~~~~~~~~^~~~~~~~
cell.c:125:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |  scanf("%d%d", &n, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~
cell.c:127:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |   scanf("%d%d", &xx[i], &yy[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cell.c:129:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |   scanf("%d", &tt[h]);
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 18780 KB Output is correct
2 Correct 2 ms 18780 KB Output is correct
3 Correct 2 ms 18780 KB Output is correct
4 Correct 2 ms 18780 KB Output is correct
5 Correct 2 ms 18780 KB Output is correct
6 Correct 2 ms 18776 KB Output is correct
7 Correct 2 ms 18776 KB Output is correct
8 Correct 49 ms 21068 KB Output is correct
9 Correct 2 ms 18780 KB Output is correct
10 Correct 2 ms 18780 KB Output is correct
11 Correct 3 ms 18788 KB Output is correct
12 Correct 9 ms 19036 KB Output is correct
13 Correct 8 ms 19068 KB Output is correct
14 Correct 7 ms 19032 KB Output is correct
15 Correct 8 ms 19036 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16840 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
19 Correct 2 ms 18780 KB Output is correct
20 Correct 2 ms 18780 KB Output is correct
21 Correct 2 ms 18776 KB Output is correct
22 Correct 2 ms 18780 KB Output is correct
23 Correct 2 ms 18780 KB Output is correct
24 Correct 2 ms 18780 KB Output is correct
25 Correct 2 ms 18780 KB Output is correct
26 Correct 2 ms 18780 KB Output is correct
27 Correct 2 ms 18780 KB Output is correct
28 Correct 2 ms 18776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 18780 KB Output is correct
2 Correct 2 ms 18780 KB Output is correct
3 Correct 2 ms 18780 KB Output is correct
4 Correct 2 ms 18780 KB Output is correct
5 Correct 2 ms 18780 KB Output is correct
6 Correct 2 ms 18776 KB Output is correct
7 Correct 2 ms 18776 KB Output is correct
8 Correct 49 ms 21068 KB Output is correct
9 Correct 2 ms 18780 KB Output is correct
10 Correct 2 ms 18780 KB Output is correct
11 Correct 3 ms 18788 KB Output is correct
12 Correct 9 ms 19036 KB Output is correct
13 Correct 8 ms 19068 KB Output is correct
14 Correct 7 ms 19032 KB Output is correct
15 Correct 8 ms 19036 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16840 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
19 Correct 2 ms 18780 KB Output is correct
20 Correct 2 ms 18780 KB Output is correct
21 Correct 2 ms 18776 KB Output is correct
22 Correct 2 ms 18780 KB Output is correct
23 Correct 2 ms 18780 KB Output is correct
24 Correct 2 ms 18780 KB Output is correct
25 Correct 2 ms 18780 KB Output is correct
26 Correct 2 ms 18780 KB Output is correct
27 Correct 2 ms 18780 KB Output is correct
28 Correct 2 ms 18776 KB Output is correct
29 Correct 2 ms 18780 KB Output is correct
30 Correct 3 ms 18780 KB Output is correct
31 Correct 4 ms 18780 KB Output is correct
32 Correct 929 ms 48212 KB Output is correct
33 Correct 2 ms 18780 KB Output is correct
34 Correct 2 ms 18780 KB Output is correct
35 Correct 5 ms 18928 KB Output is correct
36 Correct 3 ms 16732 KB Output is correct
37 Correct 2 ms 16732 KB Output is correct
38 Correct 8 ms 19036 KB Output is correct
39 Correct 62 ms 21024 KB Output is correct
40 Correct 901 ms 48112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 470 ms 42576 KB Output is correct
2 Correct 498 ms 42684 KB Output is correct
3 Correct 462 ms 42656 KB Output is correct
4 Correct 515 ms 42824 KB Output is correct
5 Correct 407 ms 42864 KB Output is correct
6 Correct 393 ms 42600 KB Output is correct
7 Correct 417 ms 42600 KB Output is correct
8 Correct 2 ms 16728 KB Output is correct
9 Correct 2 ms 16732 KB Output is correct
10 Correct 5 ms 18944 KB Output is correct
11 Correct 403 ms 42584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 470 ms 42576 KB Output is correct
2 Correct 498 ms 42684 KB Output is correct
3 Correct 462 ms 42656 KB Output is correct
4 Correct 515 ms 42824 KB Output is correct
5 Correct 407 ms 42864 KB Output is correct
6 Correct 393 ms 42600 KB Output is correct
7 Correct 417 ms 42600 KB Output is correct
8 Correct 2 ms 16728 KB Output is correct
9 Correct 2 ms 16732 KB Output is correct
10 Correct 5 ms 18944 KB Output is correct
11 Correct 403 ms 42584 KB Output is correct
12 Correct 623 ms 51088 KB Output is correct
13 Correct 635 ms 51132 KB Output is correct
14 Correct 624 ms 51284 KB Output is correct
15 Correct 609 ms 51416 KB Output is correct
16 Correct 71 ms 23636 KB Output is correct
17 Correct 70 ms 23568 KB Output is correct
18 Correct 80 ms 27472 KB Output is correct
19 Correct 583 ms 51636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 19036 KB Output is correct
2 Correct 7 ms 19036 KB Output is correct
3 Correct 11 ms 19132 KB Output is correct
4 Correct 11 ms 19292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 19036 KB Output is correct
2 Correct 7 ms 19036 KB Output is correct
3 Correct 11 ms 19132 KB Output is correct
4 Correct 11 ms 19292 KB Output is correct
5 Correct 83 ms 27224 KB Output is correct
6 Correct 83 ms 27376 KB Output is correct
7 Correct 90 ms 27792 KB Output is correct
8 Correct 95 ms 27732 KB Output is correct
9 Correct 87 ms 27776 KB Output is correct
10 Correct 87 ms 27816 KB Output is correct
11 Correct 90 ms 28080 KB Output is correct
12 Correct 91 ms 28064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 18780 KB Output is correct
2 Correct 2 ms 18780 KB Output is correct
3 Correct 2 ms 18780 KB Output is correct
4 Correct 2 ms 18780 KB Output is correct
5 Correct 2 ms 18780 KB Output is correct
6 Correct 2 ms 18776 KB Output is correct
7 Correct 2 ms 18776 KB Output is correct
8 Correct 49 ms 21068 KB Output is correct
9 Correct 2 ms 18780 KB Output is correct
10 Correct 2 ms 18780 KB Output is correct
11 Correct 3 ms 18788 KB Output is correct
12 Correct 9 ms 19036 KB Output is correct
13 Correct 8 ms 19068 KB Output is correct
14 Correct 7 ms 19032 KB Output is correct
15 Correct 8 ms 19036 KB Output is correct
16 Correct 2 ms 16732 KB Output is correct
17 Correct 2 ms 16840 KB Output is correct
18 Correct 2 ms 16732 KB Output is correct
19 Correct 2 ms 18780 KB Output is correct
20 Correct 2 ms 18780 KB Output is correct
21 Correct 2 ms 18776 KB Output is correct
22 Correct 2 ms 18780 KB Output is correct
23 Correct 2 ms 18780 KB Output is correct
24 Correct 2 ms 18780 KB Output is correct
25 Correct 2 ms 18780 KB Output is correct
26 Correct 2 ms 18780 KB Output is correct
27 Correct 2 ms 18780 KB Output is correct
28 Correct 2 ms 18776 KB Output is correct
29 Correct 2 ms 18780 KB Output is correct
30 Correct 3 ms 18780 KB Output is correct
31 Correct 4 ms 18780 KB Output is correct
32 Correct 929 ms 48212 KB Output is correct
33 Correct 2 ms 18780 KB Output is correct
34 Correct 2 ms 18780 KB Output is correct
35 Correct 5 ms 18928 KB Output is correct
36 Correct 3 ms 16732 KB Output is correct
37 Correct 2 ms 16732 KB Output is correct
38 Correct 8 ms 19036 KB Output is correct
39 Correct 62 ms 21024 KB Output is correct
40 Correct 901 ms 48112 KB Output is correct
41 Correct 470 ms 42576 KB Output is correct
42 Correct 498 ms 42684 KB Output is correct
43 Correct 462 ms 42656 KB Output is correct
44 Correct 515 ms 42824 KB Output is correct
45 Correct 407 ms 42864 KB Output is correct
46 Correct 393 ms 42600 KB Output is correct
47 Correct 417 ms 42600 KB Output is correct
48 Correct 2 ms 16728 KB Output is correct
49 Correct 2 ms 16732 KB Output is correct
50 Correct 5 ms 18944 KB Output is correct
51 Correct 403 ms 42584 KB Output is correct
52 Correct 623 ms 51088 KB Output is correct
53 Correct 635 ms 51132 KB Output is correct
54 Correct 624 ms 51284 KB Output is correct
55 Correct 609 ms 51416 KB Output is correct
56 Correct 71 ms 23636 KB Output is correct
57 Correct 70 ms 23568 KB Output is correct
58 Correct 80 ms 27472 KB Output is correct
59 Correct 583 ms 51636 KB Output is correct
60 Correct 7 ms 19036 KB Output is correct
61 Correct 7 ms 19036 KB Output is correct
62 Correct 11 ms 19132 KB Output is correct
63 Correct 11 ms 19292 KB Output is correct
64 Correct 83 ms 27224 KB Output is correct
65 Correct 83 ms 27376 KB Output is correct
66 Correct 90 ms 27792 KB Output is correct
67 Correct 95 ms 27732 KB Output is correct
68 Correct 87 ms 27776 KB Output is correct
69 Correct 87 ms 27816 KB Output is correct
70 Correct 90 ms 28080 KB Output is correct
71 Correct 91 ms 28064 KB Output is correct
72 Correct 2 ms 16984 KB Output is correct
73 Correct 2 ms 16844 KB Output is correct
74 Correct 2 ms 18780 KB Output is correct
75 Correct 618 ms 39608 KB Output is correct
76 Correct 558 ms 38480 KB Output is correct
77 Correct 629 ms 38192 KB Output is correct
78 Correct 581 ms 38232 KB Output is correct
79 Correct 936 ms 55128 KB Output is correct
80 Correct 966 ms 59780 KB Output is correct
81 Correct 928 ms 59196 KB Output is correct
82 Correct 909 ms 57748 KB Output is correct
83 Correct 945 ms 56268 KB Output is correct
84 Correct 880 ms 56232 KB Output is correct