Submission #65167

#TimeUsernameProblemLanguageResultExecution timeMemory
65167kingpig9Chessboard (IZhO18_chessboard)C++11
70 / 100
2060 ms56148 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;

//#define debug(...) fprintf(stderr, __VA_ARGS__)
#define debug(...)
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define fillchar(a, s) memset((a), (s), sizeof(a))

template<class T>
void setmin (T &a, T b) {
	if (b < a) {
		a = b;
	}
}

ll N, K, D;
ll X1[MAXN], X2[MAXN], Y1[MAXN], Y2[MAXN];
ll totrect;

ll delta (ll xmx, ll ymx) {
	if (xmx == 0 || ymx == 0) {
		return 0;
	}

	//which one is covered?
	ll xbound = xmx / D, ybound = ymx / D;
	//0 <= x <= xbound, 0 <= y <= ybound -- how many ways x + y is even?
	ll xodd = xbound >> 1, yodd = ybound >> 1, xeven = xbound - xodd, yeven = ybound - yodd;
	ll ans = (xeven * yeven + xodd * yodd) * D * D;
	
	ll xlen = xmx - xbound * D, ylen = ymx - ybound * D;

	if (xlen) {
		//one
		//0 <= y < ybound * D
		if ((xbound & 1) == 0) {
			//starts with white
			ans += yeven * D * xlen;
		} else {
			//starts with black
			ans += yodd * D * xlen;
		}

		//both
		if (ylen) {
			if ((xbound & 1) == (ybound & 1)) {
				ans += xlen * ylen;
			}
		}
	}

	if (ylen) {
		if (ybound % 2 == 0) {
			ans += xeven * D * ylen;
		} else {
			ans += xodd * D * ylen;
		}
	}

	return ans;
}

#warning int overflow!
ll go() {
	ll ans = (N / D) * (N / D) / 2 * D * D - totrect;
	for (ll i = 0; i < K; i++) {
		ans += (delta(X1[i], Y1[i]) + delta(X2[i], Y2[i]) - delta(X1[i], Y2[i]) - delta(X2[i], Y1[i])) << 1;
	}
	return min(N * N - ans, ans);
}

int main() {
	scanf("%lld %lld", &N, &K);
	for (ll i = 0; i < K; i++) {
		scanf("%lld %lld %lld %lld", &X1[i], &Y1[i], &X2[i], &Y2[i]);
		X1[i]--;
		Y1[i]--;
		totrect += (X2[i] - X1[i]) * (Y2[i] - Y1[i]);
	}

	ll ans = ll(N) * N;
	for (D = 1; D < N; D++) {
		if (N % D == 0) {
			setmin(ans, go());
		}
	}
	printf("%lld\n", ans);
}

Compilation message (stderr)

chessboard.cpp:68:2: warning: #warning int overflow! [-Wcpp]
 #warning int overflow!
  ^~~~~~~
chessboard.cpp: In function 'int main()':
chessboard.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld %lld", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
chessboard.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld %lld %lld", &X1[i], &Y1[i], &X2[i], &Y2[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...