답안 #466752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
466752 2021-08-20T15:48:16 Z rainboy 육각형 영역 (APIO21_hexagon) C++17
84 / 100
191 ms 32580 KB
#include "hexagon.h"
#include <stdlib.h>
#include <vector>
 
using namespace std;
 
typedef vector<int> vi;
 
const int N = 200000, MD = 1000000007, V2 = 500000004, V6 = 166666668;
 
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 dx[] = { 1, 1, 0, -1, -1, 0 };
int dy[] = { 0, 1, 1, 0, -1, -1 };
 
int sum1(int n) {
	return (long long) n * (n + 1) % MD * V2 % MD;
}

int sum2(int n) {
	return (long long) n * (n + 1) % MD * (n * 2 + 1) % MD * V6 % MD;
}

long long cross(int x1, int y1, int x2, int y2) {
	return (long long) x1 * y2 - (long long) x2 * y1;
}
 
int hh[N], ll[N], ii[N], xx[N][2], yy[N], n, n_;
 
void add(long long x1, long long y1, long long x2, long long y2) {
	if (y1 < y2)
		xx[n_][0] = x1, xx[n_][1] = x2, yy[n_] = y1, n_++;
	else if (y1 > y2)
		xx[n_][0] = x2, xx[n_][1] = x1, yy[n_] = y2, n_++;
}
 
int compare(int i, int j) {
	int c = yy[i] != yy[j] ? yy[i] - yy[j] : (xx[i][0] + xx[i][1]) - (xx[j][0] + xx[j][1]);
 
	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 = 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 *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 area(int xi1, int xi2, int xj1, int xj2, int z, int l) {
	int d = ((xj2 - xj1) - (xi2 - xi1)) / l, x = xi2 - xi1 + 1;
 
	/* sum_{1}^{l-1} (x + d h) (z + h)
	 * = sum_{1}^{l-1} d h^2 + (x + z d) h + x z
	 * = d sum2(l - 1) + (x + z d) sum1(l - 1) + x z (l - 1)
	 */
	return ((long long) sum2(l - 1) * d % MD + (long long) (x + z * d) * sum1(l - 1) % MD + (long long) x * z % MD * (l - 1) % MD) % MD;
}
 
int dfs(int p, int i, int d, int l) {
	int i_, s, xi1, xi2, o, x;
 
	i_ = i >> 1, s = i & 1, xi1 = xx[ii[i_ << 1 | 0]][s], xi2 = xx[ii[i_ << 1 | 1]][s];
	x = (long long) d * (xi2 - xi1 + 1) % MD;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];
 
		if (j != p) {
			int j_ = j >> 1, t = j & 1, xj1 = xx[ii[j_ << 1 | 0]][t], xj2 = xx[ii[j_ << 1 | 1]][t];
 
			if ((i ^ j) == 1)
				x = ((long long) x + dfs(i, j, d + l, l) + area(xi1, xi2, xj1, xj2, d, l)) % MD; 
			else {
				x = (x + dfs(i, j, d, l)) % MD;
				x = (x - (long long) d * (min(xi2, xj2) - max(xi1, xj1) + 1)) % MD;
			}
		}
	}
	return x;
}
 
int solve() {
	int l_, h, i, j, k, l, x, y, ans;
 
	l_ = ll[0];
	for (i = 0; i < n; i++)
		if (ll[i] != ll[0]) {
			l_ = 1;
			break;
		}
	if (l_ == 1) {
		n_ = 0, x = 0, y = 0;
		for (i = 0; i < n; i++)
			for (l = 0; l < ll[i]; l++)
				add(x, y, x + dx[hh[i]], y + dy[hh[i]]), x += dx[hh[i]], y += dy[hh[i]];
	} else {
		n_ = 0, x = 0, y = 0;
		for (i = 0; i < n; i++)
			add(x, y, x + dx[hh[i]] * l_, y + dy[hh[i]] * l_), x += dx[hh[i]] * l_, y += dy[hh[i]] * l_;
	}
	for (i = 0; i < n_; i++)
		ii[i] = i;
	sort(ii, 0, n_);
	n_ /= 2;
	for (i = 0; i < n_ * 2; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
	for (i = 0; i < n_; i++)
		append(i << 1 | 0, i << 1 | 1), append(i << 1 | 1, i << 1 | 0);
	for (h = -1, i = 0; i < n_; h = i, i = j) {
		int y = yy[ii[i << 1 | 0]];
 
		j = i + 1;
		while (j < n_ && yy[ii[j << 1 | 0]] == y)
			j++;
		if (h != -1) {
			k = h, l = i;
			while (k < i && l < j)
				if (xx[ii[k << 1 | 1]][1] < xx[ii[l << 1 | 1]][0]) {
					if (xx[ii[l << 1 | 0]][0] <= xx[ii[k << 1 | 1]][1])
						append(k << 1 | 1, l << 1 | 0), append(l << 1 | 0, k << 1 | 1);
					k++;
				} else {
					if (xx[ii[k << 1 | 0]][1] <= xx[ii[l << 1 | 1]][0])
						append(k << 1 | 1, l << 1 | 0), append(l << 1 | 0, k << 1 | 1);
					l++;
				}
		}
	}
	ans = 0;
	for (i = 0; i < n_; i++) {
		int i0 = ii[i << 1 | 0], i1 = ii[i << 1 | 1];
 
		if (yy[i0] == 0 && xx[i0][0] <= 0 && 0 <= xx[i1][0]) {
			ans = dfs(-1, i << 1 | 0, 0, l_);
			for (i = 0; i < n_ * 2; i++)
				free(ej[i]);
			break;
		}
		if (yy[i1] + l_ == 0 && xx[i0][1] <= 0 && 0 <= xx[i1][1]) {
			ans = dfs(-1, i << 1 | 1, 0, l_);
			for (i = 0; i < n_ * 2; i++)
				free(ej[i]);
			break;
		}
	}
	return ans;
}
 
int draw_territory(int n_, int a, int b, vi hh_, vi ll_) {
	int h, i, x, y, ans1, ans2;
	long long area2, boundary, internal;
 
	n = n_;
	for (i = 0; i < n; i++)
		hh[i] = hh_[i] - 1, ll[i] = ll_[i];
	x = 0, y = 0, area2 = 0, boundary = 0;
	for (i = 0; i < n; i++) {
		h = hh[i];
		area2 += cross(x, y, x + dx[h] * ll[i], y + dy[h] * ll[i]);
		boundary += ll[i];
		x += dx[h] * ll[i], y += dy[h] * ll[i];
	}
	if (area2 < 0)
		area2 = -area2;
	internal = (area2 - boundary) / 2 + 1;
	ans1 = (boundary + internal) % MD, ans2 = 0;
	if (b > 0) {
		ans2 = 0;
		for (h = 0; h < 3; h++) {
			ans2 = (ans2 + solve()) % MD;
			for (i = 0; i < n; i++)
				hh[i] = (hh[i] + 1) % 6;
		}
		if (ans2 < 0)
			ans2 += MD;
		ans2 = (long long) ans2 * V2 % MD;
	}
	return ((long long) ans1 * a + (long long) ans2 * b) % MD;
}

Compilation message

hexagon.cpp: In function 'void append(int, int)':
hexagon.cpp:77:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   77 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
# 결과 실행 시간 메모리 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 1 ms 288 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 284 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 412 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 416 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 7 ms 1100 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 10 ms 1484 KB Output is correct
12 Correct 10 ms 1484 KB Output is correct
13 Correct 9 ms 1504 KB Output is correct
14 Correct 10 ms 1600 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 288 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 460 KB Output is correct
9 Correct 7 ms 1072 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 10 ms 1576 KB Output is correct
14 Correct 10 ms 1612 KB Output is correct
15 Correct 9 ms 1484 KB Output is correct
16 Correct 13 ms 1612 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 21 ms 2216 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 21 ms 2936 KB Output is correct
24 Correct 41 ms 4452 KB Output is correct
25 Correct 36 ms 4644 KB Output is correct
26 Correct 18 ms 2500 KB Output is correct
27 Correct 13 ms 1884 KB Output is correct
28 Correct 9 ms 1348 KB Output is correct
29 Correct 39 ms 5064 KB Output is correct
30 Correct 39 ms 5060 KB Output is correct
31 Correct 39 ms 4988 KB Output is correct
32 Correct 47 ms 5088 KB Output is correct
33 Correct 19 ms 2560 KB Output is correct
34 Correct 10 ms 1444 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 412 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 460 KB Output is correct
10 Correct 1 ms 416 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 544 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 284 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 2 ms 344 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 9 ms 1172 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 10 ms 1484 KB Output is correct
29 Correct 10 ms 1580 KB Output is correct
30 Correct 9 ms 1428 KB Output is correct
31 Correct 10 ms 1612 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 47 ms 9924 KB Output is correct
35 Correct 60 ms 5700 KB Output is correct
36 Correct 51 ms 5848 KB Output is correct
37 Correct 84 ms 9440 KB Output is correct
38 Correct 93 ms 9284 KB Output is correct
39 Correct 90 ms 10988 KB Output is correct
40 Correct 88 ms 10892 KB Output is correct
41 Correct 168 ms 20596 KB Output is correct
42 Correct 145 ms 17080 KB Output is correct
43 Correct 136 ms 14904 KB Output is correct
44 Correct 79 ms 18984 KB Output is correct
45 Correct 83 ms 16276 KB Output is correct
46 Correct 78 ms 16068 KB Output is correct
47 Correct 137 ms 21324 KB Output is correct
48 Correct 144 ms 26212 KB Output is correct
49 Correct 157 ms 30292 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 0 ms 204 KB Output is correct
55 Correct 0 ms 204 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 84 ms 8760 KB Output is correct
7 Correct 81 ms 9296 KB Output is correct
8 Correct 91 ms 10736 KB Output is correct
9 Correct 4 ms 716 KB Output is correct
10 Correct 10 ms 1740 KB Output is correct
11 Correct 10 ms 1612 KB Output is correct
12 Correct 178 ms 22844 KB Output is correct
13 Correct 191 ms 24476 KB Output is correct
14 Correct 177 ms 22444 KB Output is correct
15 Correct 110 ms 22448 KB Output is correct
16 Correct 115 ms 19652 KB Output is correct
17 Correct 106 ms 21484 KB Output is correct
18 Correct 161 ms 27232 KB Output is correct
19 Correct 144 ms 32580 KB Output is correct
20 Correct 88 ms 23108 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 416 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 424 KB Output is correct
27 Correct 2 ms 460 KB Output is correct
28 Correct 7 ms 1080 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 292 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 10 ms 1472 KB Output is correct
33 Correct 10 ms 1508 KB Output is correct
34 Correct 9 ms 1484 KB Output is correct
35 Correct 10 ms 1612 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 17 ms 2308 KB Output is correct
40 Correct 2 ms 460 KB Output is correct
41 Correct 2 ms 332 KB Output is correct
42 Correct 28 ms 2900 KB Output is correct
43 Correct 35 ms 4480 KB Output is correct
44 Correct 35 ms 4608 KB Output is correct
45 Correct 24 ms 2444 KB Output is correct
46 Correct 18 ms 1868 KB Output is correct
47 Correct 9 ms 1356 KB Output is correct
48 Correct 38 ms 5112 KB Output is correct
49 Correct 39 ms 5108 KB Output is correct
50 Correct 53 ms 5020 KB Output is correct
51 Correct 39 ms 5060 KB Output is correct
52 Correct 19 ms 2552 KB Output is correct
53 Correct 10 ms 1484 KB Output is correct
54 Correct 46 ms 10052 KB Output is correct
55 Correct 56 ms 5688 KB Output is correct
56 Correct 50 ms 5856 KB Output is correct
57 Correct 84 ms 9452 KB Output is correct
58 Correct 89 ms 9332 KB Output is correct
59 Correct 95 ms 10992 KB Output is correct
60 Correct 91 ms 10820 KB Output is correct
61 Correct 161 ms 20464 KB Output is correct
62 Correct 138 ms 17088 KB Output is correct
63 Correct 133 ms 14900 KB Output is correct
64 Correct 79 ms 18888 KB Output is correct
65 Correct 90 ms 16156 KB Output is correct
66 Correct 78 ms 15812 KB Output is correct
67 Correct 131 ms 21100 KB Output is correct
68 Correct 150 ms 26256 KB Output is correct
69 Correct 171 ms 30372 KB Output is correct
70 Correct 0 ms 204 KB Output is correct
71 Correct 82 ms 8520 KB Output is correct
72 Correct 86 ms 9276 KB Output is correct
73 Correct 103 ms 10768 KB Output is correct
74 Correct 3 ms 716 KB Output is correct
75 Correct 11 ms 1788 KB Output is correct
76 Correct 12 ms 1612 KB Output is correct
77 Correct 176 ms 22840 KB Output is correct
78 Correct 178 ms 24448 KB Output is correct
79 Correct 179 ms 22452 KB Output is correct
80 Correct 118 ms 22564 KB Output is correct
81 Correct 102 ms 19652 KB Output is correct
82 Correct 117 ms 21540 KB Output is correct
83 Correct 161 ms 27368 KB Output is correct
84 Correct 139 ms 32580 KB Output is correct
85 Correct 87 ms 23144 KB Output is correct
86 Runtime error 23 ms 9860 KB Execution killed with signal 11
87 Halted 0 ms 0 KB -