/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
#define int ll
int n, k;
vector<int> Up, Down, Left, Right;
ll check(ll d, ll extra) {
ll need = (n / d * n / d + extra) / 2;
need = need * d * d;
for (int i = 1; i <= k; i++) {
int U, D, L, R;
bool isU, isD, isL, isR;
isU = isD, isL = isR = true;
if ((Up[i] - 1) % d == 0)
isU = false;
if (Right[i] % d == 0)
isR = false;
if (Down[i] % d == 0)
isD = false;
if ((Left[i] - 1) % d == 0)
isL = false;
U = (Up[i] + d - 1) / d;
D = (Down[i] + d - 1) / d;
L = (Left[i] + d - 1) / d;
R = (Right[i] + d - 1) / d;
if (U == D && L == R) {
if (extra == 1) {
if ((U + L) % 2 == 0)
need -= (Down[i] - Up[i] + 1) * (Right[i] - Left[i] + 1);
else
need += (Down[i] - Up[i] + 1) * (Right[i] - Left[i] + 1);
} else {
if ((U + L) % 2 == 1)
need -= (Down[i] - Up[i] + 1) * (Right[i] - Left[i] + 1);
else
need += (Down[i] - Up[i] + 1) * (Right[i] - Left[i] + 1);
}
continue;
}
if (isU && isL) {
if ((U + L) % 2 == extra)
need -= (U * d - Up[i] + 1) * (L * d - Left[i] + 1);
else
need += (U * d - Up[i] + 1) * (L * d - Left[i] + 1);
}
if (isU && isR) {
if ((U + R) % 2 == extra)
need -= (U * d - Up[i] + 1) * (Right[i] - (d * (R - 1) + 1) + 1);
else
need += (U * d - Up[i] + 1) * (Right[i] - (d * (R - 1) + 1) + 1);
}
if (isD && isL) {
if ((D + L) % 2 == extra)
need -= (Down[i] - (d * (D - 1) + 1) + 1) * (L * d - Left[i] + 1);
else
need += (Down[i] - (d * (D - 1) + 1) + 1) * (L * d - Left[i] + 1);
}
if (isD && isR) {
if ((D + R) % 2 == extra)
need -= (Down[i] - (d * (D - 1) + 1) + 1) * (Right[i] - (d * (R - 1) + 1) + 1);
else
need += (Down[i] - (d * (D - 1) + 1) + 1) * (Right[i] - (d * (R - 1) + 1) + 1);
}
if (isU) {
if (isL)
L++;
if (isR)
R--;
if (L <= R) {
int x = (U * d - Up[i] + 1) * d;
if (1 == extra) {
int cntB, cntW;
if ((U + L) % 2 == 0) {
cntB = (R - L + 2) / 2;
cntW = (R - L + 1) - cntB;
} else {
cntB = (R - L + 1) / 2;
cntW = (R - L + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
} else {
int cntB, cntW;
if ((U + L) % 2 == 1) {
cntB = (R - L + 2) / 2;
cntW = (R - L + 1) - cntB;
} else {
cntB = (R - L + 1) / 2;
cntW = (R - L + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
}
}
if (isL)
L--;
if (isR)
R++;
}
if (isD) {
if (isL)
L++;
if (isR)
R--;
if (L <= R) {
int x = (Down[i] - (d * (D - 1) + 1) + 1) * d;
if (1 == extra) {
int cntB, cntW;
if ((D + L) % 2 == 0) {
cntB = (R - L + 2) / 2;
cntW = (R - L + 1) - cntB;
} else {
cntB = (R - L + 1) / 2;
cntW = (R - L + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
} else {
int cntB, cntW;
if ((D + L) % 2 == 1) {
cntB = (R - L + 2) / 2;
cntW = (R - L + 1) - cntB;
} else {
cntB = (R - L + 1) / 2;
cntW = (R - L + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
}
}
if (isL)
L--;
if (isR)
R++;
}
if (isL) {
if (isU)
U++;
if (isD)
D--;
if (U <= D) {
int x = (L * d - Left[i] + 1) * d;
if (1 == extra) {
int cntB, cntW;
if ((U + L) % 2 == 0) {
cntB = (D - U + 2) / 2;
cntW = (D - U + 1) - cntB;
} else {
cntB = (D - U + 1) / 2;
cntW = (D - U + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
} else {
int cntB, cntW;
if ((U + L) % 2 == 1) {
cntB = (D - U + 2) / 2;
cntW = (D - U + 1) - cntB;
} else {
cntB = (D - U + 1) / 2;
cntW = (D - U + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
}
}
if (isU)
U--;
if (isD)
D++;
}
if (isR) {
if (isU)
U++;
if (isD)
D--;
if (U <= D) {
int x = (Right[i] - (d * (R - 1) + 1) + 1) * d;
if (1 == extra) {
int cntB, cntW;
if ((U + R) % 2 == 0) {
cntB = (D - U + 2) / 2;
cntW = (D - U + 1) - cntB;
} else {
cntB = (D - U + 1) / 2;
cntW = (D - U + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
} else {
int cntB, cntW;
if ((U + R) % 2 == 1) {
cntB = (D - U + 2) / 2;
cntW = (D - U + 1) - cntB;
} else {
cntB = (D - U + 1) / 2;
cntW = (D - U + 1) - cntB;
}
need -= x * cntB;
need += x * cntW;
}
}
if (isU)
U--;
if (isD)
D++;
}
if (isU)
U++;
if (isD)
D--;
if (isL)
L++;
if (isR)
R--;
if (U <= D && L <= R) {
if (extra == 1) {
if ((U + L) % 2 == 0) {
int cntB = ((D - U + 1) * (R - L + 1) + 1) / 2;
int cntW = ((D - U + 1) * (R - L + 1)) - cntB;
need -= cntB * d * d;
need += cntW * d * d;
} else {
int cntB = ((D - U + 1) * (R - L + 1)) / 2;
int cntW = ((D - U + 1) * (R - L + 1)) - cntB;
need -= cntB * d * d;
need += cntW * d * d;
}
} else {
if ((U + L) % 2 == 0) {
int cntB = ((D - U + 1) * (R - L + 1)) / 2;
int cntW = ((D - U + 1) * (R - L + 1)) - cntB;
need -= cntB * d * d;
need += cntW * d * d;
} else {
int cntB = ((D - U + 1) * (R - L + 1) + 1) / 2;
int cntW = ((D - U + 1) * (R - L + 1)) - cntB;
need -= cntB * d * d;
need += cntW * d * d;
}
}
}
}
return need;
}
void solve() {
cin >> n >> k;
Up.resize(k + 1);
Down.resize(k + 1);
Left.resize(k + 1);
Right.resize(k + 1);
for (int i = 1; i <= k; i++) {
cin >> Up[i] >> Left[i] >> Down[i] >> Right[i];
}
ll ans = n * n * 4;
ans = min(ans, check(1, 0));
debug(check(1, 0), check(1, 1));
ans = min(ans, check(1, 1));
for (int len = 2; len * len <= n; len++) {
ans = min(ans, check(len, 0));
ans = min(ans, check(len, 1));
debug(check(len, 0), check(len, 1));
ans = min(ans, check(n / len, 0));
ans = min(ans, check(n / len, 1));
debug(check(n / len, 0), check(n / len, 1));
}
cout << ans << '\n';
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message
chessboard.cpp: In function 'void solve()':
chessboard.cpp:57:20: warning: statement has no effect [-Wunused-value]
57 | #define debug(...) 47
| ^~
chessboard.cpp:328:5: note: in expansion of macro 'debug'
328 | debug(check(1, 0), check(1, 1));
| ^~~~~
chessboard.cpp:57:20: warning: statement has no effect [-Wunused-value]
57 | #define debug(...) 47
| ^~
chessboard.cpp:333:6: note: in expansion of macro 'debug'
333 | debug(check(len, 0), check(len, 1));
| ^~~~~
chessboard.cpp:57:20: warning: statement has no effect [-Wunused-value]
57 | #define debug(...) 47
| ^~
chessboard.cpp:336:6: note: in expansion of macro 'debug'
336 | debug(check(n / len, 0), check(n / len, 1));
| ^~~~~
chessboard.cpp: In function 'void setIO(const string&)':
chessboard.cpp:359:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
359 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chessboard.cpp:360:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
360 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2077 ms |
2252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2077 ms |
2252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |