Submission #378814

# Submission time Handle Problem Language Result Execution time Memory
378814 2021-03-17T05:09:18 Z cheissmart Chessboard (IZhO18_chessboard) C++14
70 / 100
1436 ms 4204 KB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
#define int ll

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7;
const ll oo = 1e18;

signed main()
{
	IO_OP;

	int n, k;
	cin >> n >> k;
	V<array<int, 4>> a(k);
	for(int i = 0; i < k; i++) {
		for(int j = 0; j < 4; j++)
			cin >> a[i][j], a[i][j]--;
	}

	ll ans = oo;
	auto go = [&] (int b) {
		assert(n % b == 0);
		int m = n / b;
		ll black = (ll) m * m / 2 * b * b, white = (ll) n * n - black;
		int black_type = 1;
		for(int _ = 0; _ < 2; _++) {
			ll in_black = 0, in_white = 0;
			for(int i = 0; i < k; i++) {
				function<void(int, int, int, int)> solve = [&] (int x1, int y1, int x2, int y2) {
					if(x1 > x2 || y1 > y2) return;
					// int x1 = a[i][0], y1 = a[i][1];
					// int x2 = a[i][2], y2 = a[i][3];
					if(x1 / b == x2 / b && y1 / b == y2 / b) {
						if((x1 / b + y1 / b) % 2 == black_type)
							in_black += (ll) (x2 - x1 + 1) * (y2 - y1 + 1);
						else
							in_white += (ll) (x2 - x1 + 1) * (y2 - y1 + 1);
					} else if(y1 / b == y2 / b || x1 / b == x2 / b) {
						if(y1 / b == y2 / b) {
							swap(x1, y1);
							swap(x2, y2);
						}
						int bl = y1 / b + 1;
						int br = y2 / b - 1;
						ll l_rem = (ll) (bl * b - y1) * (x2 - x1 + 1);
						ll r_rem = (ll) (y2 - br * b) * (x2 - x1 + 1);
						if((x1 / b + y1 / b) % 2 == black_type) in_black += l_rem;
						else in_white += l_rem;
						if((x2 / b + y2 / b) % 2 == black_type) in_black += r_rem;
						else in_white += r_rem;
						if((br - bl + 1) % 2 == 1) {
							if((bl + x1 / b) % 2 == black_type)
								in_black += (ll) b * (x2 - x1 + 1);
							else
								in_white += (ll) b * (x2 - x1 + 1);
						}
					} else {
						int bl = y1 / b + 1, br = y2 / b - 1;
						int bu = x1 / b + 1, bd = x2 / b - 1;
						solve(x1, bl * b, bu * b - 1, br * b + b - 1);
						solve(br * b + b, bl * b, x2, br * b + b - 1);
						solve(bu * b, y1, bd * b + b - 1, bl * b - 1);
						solve(bu * b, br * b + b, bd * b + b - 1, y2);

						solve(x1, y1, bl * b - 1, br * b - 1);
						solve(x1, br * b + b, bl * b - 1, y2);
						solve(bd * b + b, y1, x2, bl * b - 1);
						solve(bd * b + b, br * b + b, x2, y2);
						if((br - bl + 1) % 2 == 1 && (bd - bu + 1) % 2 == 1) {
							if((bl + bu) % 2 == black_type)
								in_black += (ll) b * b;
							else
								in_white += (ll) b * b;
						}
					}
				};
				int x1 = a[i][0], y1 = a[i][1];
				int x2 = a[i][2], y2 = a[i][3];
				solve(x1, y1, x2, y2);
			}
			assert(in_black <= black);
			assert(in_white <= white);
			ans = min(ans, in_white + black - in_black);
			swap(black, white);
			black_type ^= 1;
		}
	};

	for(int i = 1; i * i <= n; i++) {
		if(n % i == 0) {
			go(i);
			if(n / i != i && n / i != n) go(n / i);
		}
	}
	cout << ans << '\n';

}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2668 KB Output is correct
2 Correct 9 ms 1132 KB Output is correct
3 Correct 21 ms 2028 KB Output is correct
4 Correct 23 ms 2176 KB Output is correct
5 Correct 28 ms 2412 KB Output is correct
6 Correct 18 ms 1900 KB Output is correct
7 Correct 4 ms 748 KB Output is correct
8 Correct 19 ms 1900 KB Output is correct
9 Correct 46 ms 3948 KB Output is correct
10 Correct 26 ms 2284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 2 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 19 ms 1516 KB Output is correct
17 Correct 37 ms 3436 KB Output is correct
18 Correct 65 ms 3948 KB Output is correct
19 Correct 280 ms 3564 KB Output is correct
20 Correct 313 ms 4076 KB Output is correct
21 Correct 36 ms 3308 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 46 ms 2028 KB Output is correct
24 Correct 59 ms 3820 KB Output is correct
25 Correct 10 ms 748 KB Output is correct
26 Correct 38 ms 2540 KB Output is correct
27 Correct 59 ms 2924 KB Output is correct
28 Correct 63 ms 3948 KB Output is correct
29 Correct 19 ms 1900 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2668 KB Output is correct
2 Correct 9 ms 1132 KB Output is correct
3 Correct 21 ms 2028 KB Output is correct
4 Correct 23 ms 2176 KB Output is correct
5 Correct 28 ms 2412 KB Output is correct
6 Correct 18 ms 1900 KB Output is correct
7 Correct 4 ms 748 KB Output is correct
8 Correct 19 ms 1900 KB Output is correct
9 Correct 46 ms 3948 KB Output is correct
10 Correct 26 ms 2284 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 2 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 19 ms 1516 KB Output is correct
27 Correct 37 ms 3436 KB Output is correct
28 Correct 65 ms 3948 KB Output is correct
29 Correct 280 ms 3564 KB Output is correct
30 Correct 313 ms 4076 KB Output is correct
31 Correct 36 ms 3308 KB Output is correct
32 Correct 2 ms 364 KB Output is correct
33 Correct 46 ms 2028 KB Output is correct
34 Correct 59 ms 3820 KB Output is correct
35 Correct 10 ms 748 KB Output is correct
36 Correct 38 ms 2540 KB Output is correct
37 Correct 59 ms 2924 KB Output is correct
38 Correct 63 ms 3948 KB Output is correct
39 Correct 19 ms 1900 KB Output is correct
40 Correct 2 ms 492 KB Output is correct
41 Correct 242 ms 3564 KB Output is correct
42 Correct 76 ms 3948 KB Output is correct
43 Correct 135 ms 3564 KB Output is correct
44 Correct 68 ms 3820 KB Output is correct
45 Correct 50 ms 4076 KB Output is correct
46 Correct 282 ms 3820 KB Output is correct
47 Correct 43 ms 3764 KB Output is correct
48 Correct 102 ms 3692 KB Output is correct
49 Correct 60 ms 3564 KB Output is correct
50 Correct 1310 ms 3820 KB Output is correct
51 Correct 1392 ms 4204 KB Output is correct
52 Correct 1296 ms 3820 KB Output is correct
53 Correct 1383 ms 3948 KB Output is correct
54 Correct 1288 ms 3692 KB Output is correct
55 Correct 1436 ms 4076 KB Output is correct
56 Correct 1244 ms 3692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 32 ms 2668 KB Output is correct
10 Correct 9 ms 1132 KB Output is correct
11 Correct 21 ms 2028 KB Output is correct
12 Correct 23 ms 2176 KB Output is correct
13 Correct 28 ms 2412 KB Output is correct
14 Correct 18 ms 1900 KB Output is correct
15 Correct 4 ms 748 KB Output is correct
16 Correct 19 ms 1900 KB Output is correct
17 Correct 46 ms 3948 KB Output is correct
18 Correct 26 ms 2284 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 2 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 19 ms 1516 KB Output is correct
35 Correct 37 ms 3436 KB Output is correct
36 Correct 65 ms 3948 KB Output is correct
37 Correct 280 ms 3564 KB Output is correct
38 Correct 313 ms 4076 KB Output is correct
39 Correct 36 ms 3308 KB Output is correct
40 Correct 2 ms 364 KB Output is correct
41 Correct 46 ms 2028 KB Output is correct
42 Correct 59 ms 3820 KB Output is correct
43 Correct 10 ms 748 KB Output is correct
44 Correct 38 ms 2540 KB Output is correct
45 Correct 59 ms 2924 KB Output is correct
46 Correct 63 ms 3948 KB Output is correct
47 Correct 19 ms 1900 KB Output is correct
48 Correct 2 ms 492 KB Output is correct
49 Correct 242 ms 3564 KB Output is correct
50 Correct 76 ms 3948 KB Output is correct
51 Correct 135 ms 3564 KB Output is correct
52 Correct 68 ms 3820 KB Output is correct
53 Correct 50 ms 4076 KB Output is correct
54 Correct 282 ms 3820 KB Output is correct
55 Correct 43 ms 3764 KB Output is correct
56 Correct 102 ms 3692 KB Output is correct
57 Correct 60 ms 3564 KB Output is correct
58 Correct 1310 ms 3820 KB Output is correct
59 Correct 1392 ms 4204 KB Output is correct
60 Correct 1296 ms 3820 KB Output is correct
61 Correct 1383 ms 3948 KB Output is correct
62 Correct 1288 ms 3692 KB Output is correct
63 Correct 1436 ms 4076 KB Output is correct
64 Correct 1244 ms 3692 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Incorrect 1 ms 396 KB Output isn't correct
67 Halted 0 ms 0 KB -