#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;
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];
}
}
for (int j = 0; j < sz; ++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) {
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
136216 KB |
Output is correct |
2 |
Correct |
45 ms |
30060 KB |
Output is correct |
3 |
Correct |
195 ms |
181228 KB |
Output is correct |
4 |
Correct |
44 ms |
7148 KB |
Output is correct |
5 |
Correct |
190 ms |
123756 KB |
Output is correct |
6 |
Correct |
130 ms |
105708 KB |
Output is correct |
7 |
Correct |
42 ms |
48748 KB |
Output is correct |
8 |
Correct |
141 ms |
107372 KB |
Output is correct |
9 |
Correct |
199 ms |
68716 KB |
Output is correct |
10 |
Correct |
139 ms |
83564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
25 ms |
3180 KB |
Output is correct |
17 |
Correct |
60 ms |
4936 KB |
Output is correct |
18 |
Correct |
81 ms |
4844 KB |
Output is correct |
19 |
Correct |
149 ms |
4716 KB |
Output is correct |
20 |
Correct |
180 ms |
4608 KB |
Output is correct |
21 |
Correct |
55 ms |
4716 KB |
Output is correct |
22 |
Correct |
3 ms |
2560 KB |
Output is correct |
23 |
Correct |
41 ms |
3564 KB |
Output is correct |
24 |
Correct |
68 ms |
4844 KB |
Output is correct |
25 |
Correct |
10 ms |
2668 KB |
Output is correct |
26 |
Correct |
43 ms |
3692 KB |
Output is correct |
27 |
Correct |
57 ms |
4332 KB |
Output is correct |
28 |
Correct |
72 ms |
4716 KB |
Output is correct |
29 |
Correct |
24 ms |
3308 KB |
Output is correct |
30 |
Correct |
4 ms |
2540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
136216 KB |
Output is correct |
2 |
Correct |
45 ms |
30060 KB |
Output is correct |
3 |
Correct |
195 ms |
181228 KB |
Output is correct |
4 |
Correct |
44 ms |
7148 KB |
Output is correct |
5 |
Correct |
190 ms |
123756 KB |
Output is correct |
6 |
Correct |
130 ms |
105708 KB |
Output is correct |
7 |
Correct |
42 ms |
48748 KB |
Output is correct |
8 |
Correct |
141 ms |
107372 KB |
Output is correct |
9 |
Correct |
199 ms |
68716 KB |
Output is correct |
10 |
Correct |
139 ms |
83564 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
25 ms |
3180 KB |
Output is correct |
27 |
Correct |
60 ms |
4936 KB |
Output is correct |
28 |
Correct |
81 ms |
4844 KB |
Output is correct |
29 |
Correct |
149 ms |
4716 KB |
Output is correct |
30 |
Correct |
180 ms |
4608 KB |
Output is correct |
31 |
Correct |
55 ms |
4716 KB |
Output is correct |
32 |
Correct |
3 ms |
2560 KB |
Output is correct |
33 |
Correct |
41 ms |
3564 KB |
Output is correct |
34 |
Correct |
68 ms |
4844 KB |
Output is correct |
35 |
Correct |
10 ms |
2668 KB |
Output is correct |
36 |
Correct |
43 ms |
3692 KB |
Output is correct |
37 |
Correct |
57 ms |
4332 KB |
Output is correct |
38 |
Correct |
72 ms |
4716 KB |
Output is correct |
39 |
Correct |
24 ms |
3308 KB |
Output is correct |
40 |
Correct |
4 ms |
2540 KB |
Output is correct |
41 |
Correct |
525 ms |
218092 KB |
Output is correct |
42 |
Correct |
366 ms |
218860 KB |
Output is correct |
43 |
Correct |
439 ms |
218476 KB |
Output is correct |
44 |
Correct |
358 ms |
218932 KB |
Output is correct |
45 |
Correct |
350 ms |
218604 KB |
Output is correct |
46 |
Correct |
554 ms |
218476 KB |
Output is correct |
47 |
Correct |
322 ms |
218096 KB |
Output is correct |
48 |
Correct |
407 ms |
218476 KB |
Output is correct |
49 |
Correct |
357 ms |
218564 KB |
Output is correct |
50 |
Correct |
1239 ms |
182508 KB |
Output is correct |
51 |
Correct |
1350 ms |
182764 KB |
Output is correct |
52 |
Correct |
1223 ms |
182508 KB |
Output is correct |
53 |
Correct |
1309 ms |
182712 KB |
Output is correct |
54 |
Correct |
1230 ms |
182380 KB |
Output is correct |
55 |
Correct |
1349 ms |
182916 KB |
Output is correct |
56 |
Correct |
1230 ms |
182380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
240 ms |
136216 KB |
Output is correct |
10 |
Correct |
45 ms |
30060 KB |
Output is correct |
11 |
Correct |
195 ms |
181228 KB |
Output is correct |
12 |
Correct |
44 ms |
7148 KB |
Output is correct |
13 |
Correct |
190 ms |
123756 KB |
Output is correct |
14 |
Correct |
130 ms |
105708 KB |
Output is correct |
15 |
Correct |
42 ms |
48748 KB |
Output is correct |
16 |
Correct |
141 ms |
107372 KB |
Output is correct |
17 |
Correct |
199 ms |
68716 KB |
Output is correct |
18 |
Correct |
139 ms |
83564 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
2 ms |
492 KB |
Output is correct |
34 |
Correct |
25 ms |
3180 KB |
Output is correct |
35 |
Correct |
60 ms |
4936 KB |
Output is correct |
36 |
Correct |
81 ms |
4844 KB |
Output is correct |
37 |
Correct |
149 ms |
4716 KB |
Output is correct |
38 |
Correct |
180 ms |
4608 KB |
Output is correct |
39 |
Correct |
55 ms |
4716 KB |
Output is correct |
40 |
Correct |
3 ms |
2560 KB |
Output is correct |
41 |
Correct |
41 ms |
3564 KB |
Output is correct |
42 |
Correct |
68 ms |
4844 KB |
Output is correct |
43 |
Correct |
10 ms |
2668 KB |
Output is correct |
44 |
Correct |
43 ms |
3692 KB |
Output is correct |
45 |
Correct |
57 ms |
4332 KB |
Output is correct |
46 |
Correct |
72 ms |
4716 KB |
Output is correct |
47 |
Correct |
24 ms |
3308 KB |
Output is correct |
48 |
Correct |
4 ms |
2540 KB |
Output is correct |
49 |
Correct |
525 ms |
218092 KB |
Output is correct |
50 |
Correct |
366 ms |
218860 KB |
Output is correct |
51 |
Correct |
439 ms |
218476 KB |
Output is correct |
52 |
Correct |
358 ms |
218932 KB |
Output is correct |
53 |
Correct |
350 ms |
218604 KB |
Output is correct |
54 |
Correct |
554 ms |
218476 KB |
Output is correct |
55 |
Correct |
322 ms |
218096 KB |
Output is correct |
56 |
Correct |
407 ms |
218476 KB |
Output is correct |
57 |
Correct |
357 ms |
218564 KB |
Output is correct |
58 |
Correct |
1239 ms |
182508 KB |
Output is correct |
59 |
Correct |
1350 ms |
182764 KB |
Output is correct |
60 |
Correct |
1223 ms |
182508 KB |
Output is correct |
61 |
Correct |
1309 ms |
182712 KB |
Output is correct |
62 |
Correct |
1230 ms |
182380 KB |
Output is correct |
63 |
Correct |
1349 ms |
182916 KB |
Output is correct |
64 |
Correct |
1230 ms |
182380 KB |
Output is correct |
65 |
Correct |
0 ms |
364 KB |
Output is correct |
66 |
Correct |
0 ms |
364 KB |
Output is correct |
67 |
Correct |
1205 ms |
183148 KB |
Output is correct |
68 |
Correct |
1199 ms |
183580 KB |
Output is correct |
69 |
Correct |
1036 ms |
182824 KB |
Output is correct |
70 |
Correct |
1097 ms |
183020 KB |
Output is correct |
71 |
Correct |
1149 ms |
182892 KB |
Output is correct |
72 |
Correct |
1098 ms |
183020 KB |
Output is correct |
73 |
Correct |
1125 ms |
183088 KB |
Output is correct |
74 |
Correct |
1216 ms |
183148 KB |
Output is correct |
75 |
Correct |
1244 ms |
182784 KB |
Output is correct |
76 |
Correct |
1210 ms |
183404 KB |
Output is correct |
77 |
Correct |
407 ms |
219372 KB |
Output is correct |
78 |
Correct |
284 ms |
219244 KB |
Output is correct |
79 |
Correct |
359 ms |
218604 KB |
Output is correct |
80 |
Correct |
403 ms |
218860 KB |
Output is correct |
81 |
Correct |
362 ms |
219096 KB |
Output is correct |
82 |
Correct |
394 ms |
218824 KB |
Output is correct |
83 |
Correct |
310 ms |
218920 KB |
Output is correct |
84 |
Correct |
865 ms |
219372 KB |
Output is correct |
85 |
Correct |
1374 ms |
183116 KB |
Output is correct |
86 |
Correct |
262 ms |
178784 KB |
Output is correct |
87 |
Correct |
263 ms |
178796 KB |
Output is correct |
88 |
Correct |
1248 ms |
183276 KB |
Output is correct |
89 |
Correct |
469 ms |
179948 KB |
Output is correct |
90 |
Correct |
257 ms |
178796 KB |
Output is correct |