Submission #378660

# Submission time Handle Problem Language Result Execution time Memory
378660 2021-03-17T02:59:57 Z Seanliu Chessboard (IZhO18_chessboard) C++14
70 / 100
794 ms 4332 KB
#include <iostream>
#include <cassert>
#define int long long int
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxN = 1e5 + 326;

struct Rect{
	int l, r, d, u, A;
	Rect(){}
	Rect(int l, int r, int d, int u): l(l), r(r), d(d), u(u){
		A = (r - l + 1) * (u - d + 1);
	}
} rects[maxN];

inline bool onDiag(int x, int y, int d){
	return	(((x % (2 * d)) < d) == ((y % (2 * d)) < d)); 
}

inline int id(int x, int y, int d){
	return x / d * maxN + y / d;		
}

inline int getArea(int a, int b){
	if(a < 0 || b < 0) return 0;
	return a * b;
}

inline int Area(int l, int r, int d, int u, int div){
	if(l > r || d > u) return 0;
	if(div == 1){
		if(((r - l + 1) & 1) && ((u - d + 1) & 1) && onDiag(l, u, div)) return ((r - l + 1) * (u - d + 1) + 1)/ 2;
		return ((r - l + 1) * (u - d + 1))/ 2;
	}
	//cout << "running: " << l << " " << r << " " << d << " " << u << " " << div << endl;
	int _l = (l + div - 1) / div * div, _r = r / div * div, _u = u / div * div, _d = (d + div - 1) / div * div, res = 0;
	if((l / div == r / div) && (u / div == d / div)){
		return (u - d + 1) * (r - l + 1) * onDiag(l, u, div);
	}
	if(l / div == r / div){
		int k = 0;
		k += (u - _u + 1) * onDiag(l, u, div);
		k += (_d - d) * onDiag(l, d, div);
		if(onDiag(l, _d, div) && ((_u - _d) / div) & 1) k += ((_u - _d) / div / 2 + 1) * d;
		else k += ((_u - _d) / div / 2) * d; 
		return k * (r - l + 1);
	} 
	if(u / div == d / div){
		int k = 0;
		k += (_l - l) * onDiag(l, u, div);
		k += (r - _r + 1) * onDiag(r, u, div);
		if(onDiag(_l, u, div) && ((_r - _l) / div) & 1) k += ((_r - _l) / div / 2 + 1) * d;
		else k += ((_r - _l) / div / 2) * d; 
		return k * (u - d + 1);
	}
	res = (_r - _l) / div * (_u - _d) / div;
	if(onDiag(_l, _d, div) && (res & 1)) res = (res + 1) / 2; 
	else res = res / 2;
	res *= div * div;
	if(onDiag(l, u, div)){
		res += getArea((_l - l), (u - _u + 1));
	} 
	if(onDiag(r, u, div)){
		res += getArea((r - _r + 1), (u - _u + 1));
	} 
	if(onDiag(r, d, div)){
		res += getArea((r - _r + 1), (_d - d));
	}	
	if(onDiag(l, d, div)){
		res += getArea((_l - l), (_d - d));
	} 
	res += Area(_l, _r - 1, _u, u, div);
	res += Area(_l, _r - 1, d, _d - 1, div);
	res += Area(l, _l - 1, _d, _u - 1, div);
	res += Area(_r, r, _d, _u - 1, div);
	//cout <<  l << " " << r << " " << d << " " << u << " " << div << ", res = " << res << endl;
	return res;
}

int N, K;

inline int check(int d){
	//top left is black
	//cout << "d = " << d << endl;
	int _ans = maxN * maxN, tmp = ((N / d) * (N / d) + 1) / 2 * (d * d), tmp2 = ((N / d) * (N / d)) / 2 * (d * d);
	//cout << "1tmp = " << tmp << endl;
	for(int i = 0; i < K; i++){
		int A = Area(rects[i].l, rects[i].r, rects[i].d, rects[i].u, d), B = rects[i].A - A;
		tmp += B - A;
		tmp2 += A - B;
	}
	//cout << "d: " << min(tmp, tmp2) << endl;
	return min(tmp, tmp2);
	//top right is black
}

signed main(){
	ericxiao;
	cin >> N >> K;
	int l, r, d, u;
	for(int i = 0; i < K; i++){
		cin >> l >> d >> r >> u;
		l--; d--; r--; u--;
		rects[i] = Rect(l, r, d, u);
		//cout << "rects[i] a = " << rects[i].A << endl;
	}
	int ans = maxN * maxN;
	for(int div = 1; div * div <= N; div++){
		if(!(N % div)){
			ans = min(ans, check(div));
			if(div != 1) ans = min(ans, check(N / div));
		}
	}
	cout << ans << endl;
}

/*
 * 
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
*/

Compilation message

chessboard.cpp: In function 'long long int check(long long int)':
chessboard.cpp:86:6: warning: unused variable '_ans' [-Wunused-variable]
   86 |  int _ans = maxN * maxN, tmp = ((N / d) * (N / d) + 1) / 2 * (d * d), tmp2 = ((N / d) * (N / d)) / 2 * (d * d);
      |      ^~~~
# 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 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 2796 KB Output is correct
2 Correct 8 ms 1004 KB Output is correct
3 Correct 16 ms 1900 KB Output is correct
4 Correct 17 ms 2156 KB Output is correct
5 Correct 21 ms 2412 KB Output is correct
6 Correct 13 ms 1644 KB Output is correct
7 Correct 3 ms 620 KB Output is correct
8 Correct 15 ms 1772 KB Output is correct
9 Correct 35 ms 3820 KB Output is correct
10 Correct 22 ms 2284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 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 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 1 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 384 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 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 1 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 13 ms 1516 KB Output is correct
17 Correct 32 ms 3692 KB Output is correct
18 Correct 42 ms 4224 KB Output is correct
19 Correct 136 ms 3820 KB Output is correct
20 Correct 153 ms 4332 KB Output is correct
21 Correct 34 ms 3564 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 28 ms 2028 KB Output is correct
24 Correct 38 ms 3820 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 25 ms 2540 KB Output is correct
27 Correct 36 ms 3052 KB Output is correct
28 Correct 45 ms 4076 KB Output is correct
29 Correct 11 ms 1644 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 2796 KB Output is correct
2 Correct 8 ms 1004 KB Output is correct
3 Correct 16 ms 1900 KB Output is correct
4 Correct 17 ms 2156 KB Output is correct
5 Correct 21 ms 2412 KB Output is correct
6 Correct 13 ms 1644 KB Output is correct
7 Correct 3 ms 620 KB Output is correct
8 Correct 15 ms 1772 KB Output is correct
9 Correct 35 ms 3820 KB Output is correct
10 Correct 22 ms 2284 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 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 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 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 13 ms 1516 KB Output is correct
27 Correct 32 ms 3692 KB Output is correct
28 Correct 42 ms 4224 KB Output is correct
29 Correct 136 ms 3820 KB Output is correct
30 Correct 153 ms 4332 KB Output is correct
31 Correct 34 ms 3564 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 28 ms 2028 KB Output is correct
34 Correct 38 ms 3820 KB Output is correct
35 Correct 6 ms 640 KB Output is correct
36 Correct 25 ms 2540 KB Output is correct
37 Correct 36 ms 3052 KB Output is correct
38 Correct 45 ms 4076 KB Output is correct
39 Correct 11 ms 1644 KB Output is correct
40 Correct 2 ms 492 KB Output is correct
41 Correct 125 ms 3692 KB Output is correct
42 Correct 46 ms 3948 KB Output is correct
43 Correct 76 ms 3652 KB Output is correct
44 Correct 44 ms 3948 KB Output is correct
45 Correct 37 ms 4204 KB Output is correct
46 Correct 142 ms 3948 KB Output is correct
47 Correct 33 ms 3692 KB Output is correct
48 Correct 63 ms 3820 KB Output is correct
49 Correct 41 ms 3564 KB Output is correct
50 Correct 622 ms 3948 KB Output is correct
51 Correct 671 ms 4204 KB Output is correct
52 Correct 619 ms 3920 KB Output is correct
53 Correct 688 ms 4204 KB Output is correct
54 Correct 613 ms 3948 KB Output is correct
55 Correct 686 ms 4332 KB Output is correct
56 Correct 588 ms 3820 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 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 24 ms 2796 KB Output is correct
10 Correct 8 ms 1004 KB Output is correct
11 Correct 16 ms 1900 KB Output is correct
12 Correct 17 ms 2156 KB Output is correct
13 Correct 21 ms 2412 KB Output is correct
14 Correct 13 ms 1644 KB Output is correct
15 Correct 3 ms 620 KB Output is correct
16 Correct 15 ms 1772 KB Output is correct
17 Correct 35 ms 3820 KB Output is correct
18 Correct 22 ms 2284 KB Output is correct
19 Correct 1 ms 384 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 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 1 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 13 ms 1516 KB Output is correct
35 Correct 32 ms 3692 KB Output is correct
36 Correct 42 ms 4224 KB Output is correct
37 Correct 136 ms 3820 KB Output is correct
38 Correct 153 ms 4332 KB Output is correct
39 Correct 34 ms 3564 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 28 ms 2028 KB Output is correct
42 Correct 38 ms 3820 KB Output is correct
43 Correct 6 ms 640 KB Output is correct
44 Correct 25 ms 2540 KB Output is correct
45 Correct 36 ms 3052 KB Output is correct
46 Correct 45 ms 4076 KB Output is correct
47 Correct 11 ms 1644 KB Output is correct
48 Correct 2 ms 492 KB Output is correct
49 Correct 125 ms 3692 KB Output is correct
50 Correct 46 ms 3948 KB Output is correct
51 Correct 76 ms 3652 KB Output is correct
52 Correct 44 ms 3948 KB Output is correct
53 Correct 37 ms 4204 KB Output is correct
54 Correct 142 ms 3948 KB Output is correct
55 Correct 33 ms 3692 KB Output is correct
56 Correct 63 ms 3820 KB Output is correct
57 Correct 41 ms 3564 KB Output is correct
58 Correct 622 ms 3948 KB Output is correct
59 Correct 671 ms 4204 KB Output is correct
60 Correct 619 ms 3920 KB Output is correct
61 Correct 688 ms 4204 KB Output is correct
62 Correct 613 ms 3948 KB Output is correct
63 Correct 686 ms 4332 KB Output is correct
64 Correct 588 ms 3820 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 1 ms 364 KB Output is correct
67 Incorrect 794 ms 3948 KB Output isn't correct
68 Halted 0 ms 0 KB -