Submission #378655

# Submission time Handle Problem Language Result Execution time Memory
378655 2021-03-17T02:55:55 Z Seanliu Chessboard (IZhO18_chessboard) C++14
8 / 100
24 ms 2796 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)) 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;
	}
	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:87:6: warning: unused variable '_ans' [-Wunused-variable]
   87 |  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 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 2796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 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 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 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 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 2796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 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 Incorrect 24 ms 2796 KB Output isn't correct
10 Halted 0 ms 0 KB -