Submission #907439

#TimeUsernameProblemLanguageResultExecution timeMemory
907439daoquanglinh2007Chessboard (IZhO18_chessboard)C++17
100 / 100
336 ms5960 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int NM = 1e5, inf = 1e18;

int N, K, x[NM+5], y[NM+5], u[NM+5], v[NM+5], ans = +inf;
int num[2];

void update(int lx, int ly, int rx, int ry, int c){
	if (lx > rx) return;
	if (ly > ry) return;
	int tmp = (rx-lx+1)*(ry-ly+1);
	num[(lx+ly)&1] += (tmp+1)/2 * c;
	num[!((lx+ly)&1)] += tmp/2 * c;
}

void solve(int t){
	num[0] = num[1] = 0;
	for (int i = 1; i <= K; i++){
		int blx = x[i]/t+(x[i]%t > 0), bly = y[i]/t+(y[i]%t > 0), blu = u[i]/t+(u[i]%t > 0), blv = v[i]/t+(v[i]%t > 0);
		if (blx == blu && bly == blv) update(blx, bly, blx, bly, (u[i]-x[i]+1)*(v[i]-y[i]+1));
		else if (blx == blu){
			update(blx, bly, blx, bly, (u[i]-x[i]+1)*(bly*t-y[i]+1));
			update(blx, blv, blx, blv, (u[i]-x[i]+1)*(v[i]-(blv-1)*t));
			update(blx, bly+1, blx, blv-1, (u[i]-x[i]+1)*t);
		}
		else if (bly == blv){
			update(blx, bly, blx, bly, (blx*t-x[i]+1)*(v[i]-y[i]+1));
			update(blu, bly, blu, bly, (u[i]-(blu-1)*t)*(v[i]-y[i]+1));
			update(blx+1, bly, blu-1, bly, t*(v[i]-y[i]+1));
		}
		else{
			update(blx, bly, blx, bly, (blx*t-x[i]+1)*(bly*t-y[i]+1));
			update(blx, blv, blx, blv, (blx*t-x[i]+1)*(v[i]-(blv-1)*t));
			update(blu, bly, blu, bly, (u[i]-(blu-1)*t)*(bly*t-y[i]+1));
			update(blu, blv, blu, blv, (u[i]-(blu-1)*t)*(v[i]-(blv-1)*t));
			
			update(blx, bly+1, blx, blv-1, (blx*t-x[i]+1)*t);
			update(blx+1, bly, blu-1, bly, t*(bly*t-y[i]+1));
			update(blu, bly+1, blu, blv-1, (u[i]-(blu-1)*t)*t);
			update(blx+1, blv, blu-1, blv, t*(v[i]-(blv-1)*t));
			
			update(blx+1, bly+1, blu-1, blv-1, t*t);
		}
	}
	
	int targ = t*t*(N*N/(t*t)/2);
	ans = min(ans, abs(targ-num[1])+num[0]);
	ans = min(ans, abs(N*N-targ-num[0])+num[1]);
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin >> N >> K;
	for (int i = 1; i <= K; i++){
		cin >> x[i] >> y[i] >> u[i] >> v[i];
	}
	for (int i = 1; i*i <= N; i++)
		if (N%i == 0){
			solve(i);
			if (i > 1) solve(N/i);
		}
	cout << ans;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...