#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pii pair<int, int>
#define pll pair<lli, lli>
#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define test(x) cout << #x << ' ' << x << endl
#define printv(x) {\
for (auto a : x) cout << x << ' ';\
cout << endl;\
}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 2000000;
struct range {
int l, r;
};
vector <int> p;
int sz;
struct Seg {
int l, r, m;
bool lz;
int tag[sz], sum[sz];
Seg* ch[2];
Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1), lz(false) {
if (r - l > 1) {
ch[0] = new Seg(l, m);
ch[1] = new Seg(m, r);
for (int i = 0; i < sz; ++i) tag[i] = ch[0]->tag[i] + ch[1]->tag[i];
} else {
for (int i = 0; i < sz; ++i) tag[i] = (l / p[i]) & 1 ? -1 : 1;
}
}
void pull() {
for (int i = 0; i < sz; ++i) {
if (lz) sum[i] = tag[i];
else if (r - l == 1) sum[i] = 0;
else sum[i] = ch[0]->sum[i] + ch[1]->sum[i];
}
}
void modify(int a, int b, bool v) {
if (a <= l && r <= b) lz = v;
else {
if (a < m) ch[0]->modify(a, b, v);
if (m < b) ch[1]->modify(a, b, v);
}
pull();
}
};
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i < n; ++i) if (n % i == 0) p.pb(i), sz++;
vector <range> block[n + 1][2];
for (int i = 0, x1, y1, x2, y2; i < m; ++i) {
cin >> x1 >> y1 >> x2 >> y2, --x1, --y1;
block[x1][1].pb({y1, y2});
block[x2][0].pb({y1, y2});
}
lli ans = 1ll * n * n;
vector <lli> cur(sz);
for (int i = 0; i < sz; ++i) cur[i] = 1ll * p[i] * p[i] * (1ll * (n / p[i]) * (n / p[i]) / 2);
Seg root(0, n);
for (int i = 0; i <= n; ++i) {
for (range &j : block[i][0]) root.modify(j.l, j.r, false);
for (range &j : block[i][1]) root.modify(j.l, j.r, true);
for (int j = 0; j < sz; ++j) {
if ((i / p[j]) & 1) cur[j] -= root.sum[j];
else cur[j] += root.sum[j];
ans = min({ans, cur[j], 1ll * n * n - cur[j]});
}
}
cout << ans << '\n';
}
/*
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:30:15: error: array bound is not an integer constant before ']' token
30 | int tag[sz], sum[sz];
| ^
chessboard.cpp:30:24: error: array bound is not an integer constant before ']' token
30 | int tag[sz], sum[sz];
| ^
chessboard.cpp: In constructor 'Seg::Seg(int, int)':
chessboard.cpp:32:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | Seg (int _l, int _r) : l(_l), r(_r), m(l + r >> 1), lz(false) {
| ~~^~~
chessboard.cpp:36:42: error: 'tag' was not declared in this scope; did you mean 'tan'?
36 | for (int i = 0; i < sz; ++i) tag[i] = ch[0]->tag[i] + ch[1]->tag[i];
| ^~~
| tan
chessboard.cpp:36:58: error: 'struct Seg' has no member named 'tag'
36 | for (int i = 0; i < sz; ++i) tag[i] = ch[0]->tag[i] + ch[1]->tag[i];
| ^~~
chessboard.cpp:36:74: error: 'struct Seg' has no member named 'tag'
36 | for (int i = 0; i < sz; ++i) tag[i] = ch[0]->tag[i] + ch[1]->tag[i];
| ^~~
chessboard.cpp:38:42: error: 'tag' was not declared in this scope; did you mean 'tan'?
38 | for (int i = 0; i < sz; ++i) tag[i] = (l / p[i]) & 1 ? -1 : 1;
| ^~~
| tan
chessboard.cpp: In member function 'void Seg::pull()':
chessboard.cpp:43:21: error: 'sum' was not declared in this scope
43 | if (lz) sum[i] = tag[i];
| ^~~
chessboard.cpp:43:30: error: 'tag' was not declared in this scope; did you mean 'tan'?
43 | if (lz) sum[i] = tag[i];
| ^~~
| tan
chessboard.cpp:44:34: error: 'sum' was not declared in this scope
44 | else if (r - l == 1) sum[i] = 0;
| ^~~
chessboard.cpp:45:18: error: 'sum' was not declared in this scope
45 | else sum[i] = ch[0]->sum[i] + ch[1]->sum[i];
| ^~~
chessboard.cpp:45:34: error: 'struct Seg' has no member named 'sum'
45 | else sum[i] = ch[0]->sum[i] + ch[1]->sum[i];
| ^~~
chessboard.cpp:45:50: error: 'struct Seg' has no member named 'sum'
45 | else sum[i] = ch[0]->sum[i] + ch[1]->sum[i];
| ^~~
chessboard.cpp: In function 'int main()':
chessboard.cpp:78:48: error: 'struct Seg' has no member named 'sum'
78 | if ((i / p[j]) & 1) cur[j] -= root.sum[j];
| ^~~
chessboard.cpp:79:33: error: 'struct Seg' has no member named 'sum'
79 | else cur[j] += root.sum[j];
| ^~~