#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 << a << ' ';\
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[128]{}, sum[128]{};
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;
assert(n > 10);
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; --x1, --y1;// >> x2 >> y2, --x1, --y1;
x2 = x1 + 1, y2 = y1 + 1;
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: 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) {
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
135276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
135276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |