This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
const int N = 1e5 + 7, inf = 1e18;
int n, k, x[N], y[N], x2[N], y2[N], ans = inf;
int A, S;
int num(int x) {
return ((x - 1) / A) & 1;
}
int open(int x) {
if (x == 0) {
return 0;
}
return (x - 1) / A + 1;
}
int close(int x) {
return x / A;
}
int last_close(int x) {
return close(x) * A;
}
int next_close(int x) {
int tmp = last_close(x);
if (x == tmp) {
return x;
}
return tmp + A;
}
int last_open(int x) {
return (open(x) - 1) * A + 1;
}
int next_open(int x) {
int tmp = last_open(x);
if (x == tmp) {
return x;
}
return tmp + A;
}
pii get_colors(int l, int r) {
int blocks = close(r) - open(l - 1);
int a = 0, b = 0;
if (blocks > 0) {
if (num(l) == 0) {
int len = next_open(l) - l;
a += len;
if (len > 0) {
a += (blocks / 2) * A;
b += ((blocks + 1) / 2) * A;
} else {
a += ((blocks + 1) / 2) * A;
b += (blocks / 2) * A;
}
} else {
int len = next_open(l) - l;
b += len;
if (len > 0) {
a += ((blocks + 1) / 2) * A;
b += (blocks / 2) * A;
} else {
a += (blocks / 2) * A;
b += ((blocks + 1) / 2) * A;
}
}
if (num(r) == 0) {
a += r - last_close(r);
} else {
b += r - last_close(r);
}
} else {
if (num(l) == 0) {
a += next_open(l) - l;
} else {
b += next_open(l) - l;
}
if (num(r) == 0) {
a += r - next_open(l) + 1;
} else {
b += r - next_open(l) + 1;
}
}
return {a, b};
}
pii get_squares() {
int even = (n / A) / 2, odd = ((n / A) + 1) / 2;
int a = even * odd + odd * even;
int b = odd * odd + even * even;
return {a * A * A, b * A * A};
}
int get_color(int x, int y, int z) {
return (num(x) + num(y) + z) & 1;
}
int get_ans() {
int res0 = 0, res1 = 0;
for (int i = 0; i < k; i++) {
pii t1 = get_colors(y[i], y2[i]);
pii t2 = get_colors(x[i], x2[i]);
int a = t2.fi, b = t2.se;
int c = t1.fi, d = t1.se;
res0 += a * c + b * d;
res1 += b * c + a * d;
}
pii tmp = get_squares();
return min(tmp.se - S + 2LL * res1, tmp.fi - S + 2LL * res0);
}
void solve(int tc) {
cin >> n >> k;
for (int i = 0; i < k; i++) {
cin >> x[i] >> y[i] >> x2[i] >> y2[i];
S += (x2[i] - x[i] + 1) * (y2[i] - y[i] + 1);
}
for (int i = 1; i < n; i++) {
if (n % i == 0) {
A = i;
ans = min(ans, get_ans());
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 0; i < tc; i++) {
solve(i);
// cleanup();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |