// 以上帝的名义
// 候选硕士
#include <bits/stdc++.h>
#ifdef local
#include "algo/debug.h"
#else
#define dbg(x...) 0
#endif
using namespace std ;
using ll = long long ;
#define int ll
bool isPrime(int x) {
if (x < 2) return 0 ;
for (int i = 2 ; i * i <= x ; i++) {
if (x % i == 0) {
return 0 ;
}
}
return 1 ;
}
struct Fenwick {
vector<ll> t ;
int n;
Fenwick(int len) {
n = len ;
t.assign(n + 1, 0) ;
}
void upd(int i, ll x) {
while (i <= n) {
t[i] += x;
i += (i & (-i)) ;
}
}
ll get(int i) {
ll sum = 0 ;
while (i) {
sum += t[i] ;
i -= (i & (-i)) ;
}
return sum ;
}
ll get(int l, int r) {
return get(r) - get(l - 1) ;
}
};
int32_t main() {
cin.tie(0)->sync_with_stdio(false) ;
ll n, k ; cin >> n >> k ;
struct rect {
int x1, y1, x2, y2;
void read() {
cin >> x1 >> y1 >> x2 >> y2 ;
x1-- , y1-- , x2-- , y2-- ;
}
};
vector<rect> a(k) ;
for (int i = 0 ; i < k ; i++) a[i].read() ;
vector<int> d;
for (int i = 1 ; i * i <= n ; i++) {
if (n % i) continue ;
d.push_back(i) ;
if (n / i != i) d.push_back(n / i) ;
}
auto calc = [&](ll del) -> ll{
vector<vector<ll>> cnt(del, vector<ll>(2, 0)) ;
int block = n / del ;
vector<Fenwick> f(2, Fenwick(n + 2)) ;
for (int i = 0 ; i < k ; i++) {
int x1 = a[i].x1 , y1 = a[i].y1 , x2 = a[i].x2, y2 = a[i].y2 ;
ll leny = (y2 - y1 + 1) ;
ll lenx = (x2 - x1 + 1) ;
ll bL = y1 / block , bR = y2 / block ;
ll even = 0 , odd = 0 ;
if (bL == bR) {
if (bL & 1) {
odd += leny;
} else {
even += leny ;
}
} else {
ll ost = bR - bL - 1 ;
if (ost > 0) {
if ((bL + 1) & 1) {
even += (ost / 2) * block ;
odd += (ost + 1) / 2 * block ;
} else {
even += (ost + 1) / 2 * block ;
odd += (ost / 2) * block ;
}
}
ll L = y1, R = (bL + 1) * block - 1 ;
ll LN = (R - L + 1) ;
if (bL & 1) {
odd += LN ;
} else {
even += LN ;
}
L = bR * block , R = y2 ;
LN = (R - L + 1) ;
if (bR & 1) {
odd += LN ;
} else {
even += LN ;
}
}
dbg(even , odd) ;
f[0].upd(x1 + 1, even) ;
f[0].upd(x2 + 2, -even) ;
f[1].upd(x1 + 1, odd) ;
f[1].upd(x2 + 2, -odd) ;
}
for (int i = 1 ; i <= n ; i++) {
dbg(f[0].get(i), f[1].get(i)) ;
cnt[(i - 1) / block][0] += f[0].get(i) ;
cnt[(i - 1) / block][1] += f[1].get(i) ;
}
dbg(del, cnt) ;
ll ret = 0 , sum = 0 ;
for (int i = 0 ; i < del ; i++) {
// for (int j = 0 ; j < del ; j++) {
if (i % 2 == 0) {
ll c = ((del + 1) / 2) * (block * block) ;
ll cur = c - cnt[i][0] + cnt[i][1] ;
sum += cur ;
} else {
ll c = (del / 2) * (block * block) ;
ll cur = c - cnt[i][1] + cnt[i][0] ;
sum += cur ;
}
// }
}
ret = sum ;
sum = 0 ;
for (int i = 0 ; i < del ; i++) {
// for (int j = 0 ; j < del ; j++) {
if (i % 2 != 0) {
ll c = ((del + 1) / 2) * (block * block) ;
ll cur = c - cnt[i][0] + cnt[i][1] ;
sum += cur ;
} else {
ll c = (del / 2) * (block * block) ;
ll cur = c - cnt[i][1] + cnt[i][0] ;
sum += cur ;
}
// }
}
ret = min(ret, sum) ;
return ret ;
};
ll ret = LLONG_MAX ;
for (int i : d) {
if (i > 1) {
ret = min(ret, calc(i)) ;
}
}
cout << ret ;
return 0 ;
}
// 希望白银
Compilation message
chessboard.cpp: In lambda function:
chessboard.cpp:8:19: warning: statement has no effect [-Wunused-value]
8 | #define dbg(x...) 0
| ^
chessboard.cpp:112:13: note: in expansion of macro 'dbg'
112 | dbg(even , odd) ;
| ^~~
chessboard.cpp:77:16: warning: unused variable 'lenx' [-Wunused-variable]
77 | ll lenx = (x2 - x1 + 1) ;
| ^~~~
chessboard.cpp:8:19: warning: statement has no effect [-Wunused-value]
8 | #define dbg(x...) 0
| ^
chessboard.cpp:119:13: note: in expansion of macro 'dbg'
119 | dbg(f[0].get(i), f[1].get(i)) ;
| ^~~
chessboard.cpp:8:19: warning: statement has no effect [-Wunused-value]
8 | #define dbg(x...) 0
| ^
chessboard.cpp:123:9: note: in expansion of macro 'dbg'
123 | dbg(del, cnt) ;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
7296 KB |
Output is correct |
2 |
Correct |
9 ms |
2012 KB |
Output is correct |
3 |
Correct |
22 ms |
8024 KB |
Output is correct |
4 |
Correct |
13 ms |
1884 KB |
Output is correct |
5 |
Correct |
21 ms |
6492 KB |
Output is correct |
6 |
Correct |
16 ms |
5212 KB |
Output is correct |
7 |
Correct |
4 ms |
2396 KB |
Output is correct |
8 |
Correct |
14 ms |
5208 KB |
Output is correct |
9 |
Correct |
29 ms |
5468 KB |
Output is correct |
10 |
Correct |
25 ms |
4864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
1372 KB |
Output is correct |
17 |
Correct |
20 ms |
3168 KB |
Output is correct |
18 |
Correct |
35 ms |
3420 KB |
Output is correct |
19 |
Correct |
106 ms |
3164 KB |
Output is correct |
20 |
Correct |
114 ms |
3420 KB |
Output is correct |
21 |
Correct |
19 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
20 ms |
1628 KB |
Output is correct |
24 |
Correct |
39 ms |
3260 KB |
Output is correct |
25 |
Correct |
5 ms |
600 KB |
Output is correct |
26 |
Correct |
18 ms |
2244 KB |
Output is correct |
27 |
Correct |
26 ms |
2652 KB |
Output is correct |
28 |
Correct |
35 ms |
3440 KB |
Output is correct |
29 |
Correct |
10 ms |
1624 KB |
Output is correct |
30 |
Correct |
1 ms |
540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
7296 KB |
Output is correct |
2 |
Correct |
9 ms |
2012 KB |
Output is correct |
3 |
Correct |
22 ms |
8024 KB |
Output is correct |
4 |
Correct |
13 ms |
1884 KB |
Output is correct |
5 |
Correct |
21 ms |
6492 KB |
Output is correct |
6 |
Correct |
16 ms |
5212 KB |
Output is correct |
7 |
Correct |
4 ms |
2396 KB |
Output is correct |
8 |
Correct |
14 ms |
5208 KB |
Output is correct |
9 |
Correct |
29 ms |
5468 KB |
Output is correct |
10 |
Correct |
25 ms |
4864 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
476 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
9 ms |
1372 KB |
Output is correct |
27 |
Correct |
20 ms |
3168 KB |
Output is correct |
28 |
Correct |
35 ms |
3420 KB |
Output is correct |
29 |
Correct |
106 ms |
3164 KB |
Output is correct |
30 |
Correct |
114 ms |
3420 KB |
Output is correct |
31 |
Correct |
19 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
20 ms |
1628 KB |
Output is correct |
34 |
Correct |
39 ms |
3260 KB |
Output is correct |
35 |
Correct |
5 ms |
600 KB |
Output is correct |
36 |
Correct |
18 ms |
2244 KB |
Output is correct |
37 |
Correct |
26 ms |
2652 KB |
Output is correct |
38 |
Correct |
35 ms |
3440 KB |
Output is correct |
39 |
Correct |
10 ms |
1624 KB |
Output is correct |
40 |
Correct |
1 ms |
540 KB |
Output is correct |
41 |
Correct |
175 ms |
10840 KB |
Output is correct |
42 |
Correct |
52 ms |
11088 KB |
Output is correct |
43 |
Correct |
129 ms |
10836 KB |
Output is correct |
44 |
Correct |
49 ms |
11100 KB |
Output is correct |
45 |
Correct |
36 ms |
11212 KB |
Output is correct |
46 |
Correct |
205 ms |
11052 KB |
Output is correct |
47 |
Correct |
32 ms |
10836 KB |
Output is correct |
48 |
Correct |
73 ms |
10844 KB |
Output is correct |
49 |
Correct |
44 ms |
10832 KB |
Output is correct |
50 |
Correct |
769 ms |
9556 KB |
Output is correct |
51 |
Correct |
832 ms |
9888 KB |
Output is correct |
52 |
Correct |
772 ms |
9564 KB |
Output is correct |
53 |
Correct |
832 ms |
9808 KB |
Output is correct |
54 |
Correct |
796 ms |
9564 KB |
Output is correct |
55 |
Correct |
853 ms |
10076 KB |
Output is correct |
56 |
Correct |
748 ms |
9556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
24 ms |
7296 KB |
Output is correct |
10 |
Correct |
9 ms |
2012 KB |
Output is correct |
11 |
Correct |
22 ms |
8024 KB |
Output is correct |
12 |
Correct |
13 ms |
1884 KB |
Output is correct |
13 |
Correct |
21 ms |
6492 KB |
Output is correct |
14 |
Correct |
16 ms |
5212 KB |
Output is correct |
15 |
Correct |
4 ms |
2396 KB |
Output is correct |
16 |
Correct |
14 ms |
5208 KB |
Output is correct |
17 |
Correct |
29 ms |
5468 KB |
Output is correct |
18 |
Correct |
25 ms |
4864 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
476 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
352 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
9 ms |
1372 KB |
Output is correct |
35 |
Correct |
20 ms |
3168 KB |
Output is correct |
36 |
Correct |
35 ms |
3420 KB |
Output is correct |
37 |
Correct |
106 ms |
3164 KB |
Output is correct |
38 |
Correct |
114 ms |
3420 KB |
Output is correct |
39 |
Correct |
19 ms |
2908 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
20 ms |
1628 KB |
Output is correct |
42 |
Correct |
39 ms |
3260 KB |
Output is correct |
43 |
Correct |
5 ms |
600 KB |
Output is correct |
44 |
Correct |
18 ms |
2244 KB |
Output is correct |
45 |
Correct |
26 ms |
2652 KB |
Output is correct |
46 |
Correct |
35 ms |
3440 KB |
Output is correct |
47 |
Correct |
10 ms |
1624 KB |
Output is correct |
48 |
Correct |
1 ms |
540 KB |
Output is correct |
49 |
Correct |
175 ms |
10840 KB |
Output is correct |
50 |
Correct |
52 ms |
11088 KB |
Output is correct |
51 |
Correct |
129 ms |
10836 KB |
Output is correct |
52 |
Correct |
49 ms |
11100 KB |
Output is correct |
53 |
Correct |
36 ms |
11212 KB |
Output is correct |
54 |
Correct |
205 ms |
11052 KB |
Output is correct |
55 |
Correct |
32 ms |
10836 KB |
Output is correct |
56 |
Correct |
73 ms |
10844 KB |
Output is correct |
57 |
Correct |
44 ms |
10832 KB |
Output is correct |
58 |
Correct |
769 ms |
9556 KB |
Output is correct |
59 |
Correct |
832 ms |
9888 KB |
Output is correct |
60 |
Correct |
772 ms |
9564 KB |
Output is correct |
61 |
Correct |
832 ms |
9808 KB |
Output is correct |
62 |
Correct |
796 ms |
9564 KB |
Output is correct |
63 |
Correct |
853 ms |
10076 KB |
Output is correct |
64 |
Correct |
748 ms |
9556 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
845 ms |
9792 KB |
Output is correct |
68 |
Correct |
823 ms |
12028 KB |
Output is correct |
69 |
Correct |
731 ms |
11300 KB |
Output is correct |
70 |
Correct |
734 ms |
11856 KB |
Output is correct |
71 |
Correct |
773 ms |
12012 KB |
Output is correct |
72 |
Correct |
749 ms |
11604 KB |
Output is correct |
73 |
Correct |
779 ms |
11420 KB |
Output is correct |
74 |
Correct |
826 ms |
11868 KB |
Output is correct |
75 |
Correct |
729 ms |
11756 KB |
Output is correct |
76 |
Correct |
826 ms |
12112 KB |
Output is correct |
77 |
Correct |
136 ms |
13652 KB |
Output is correct |
78 |
Correct |
49 ms |
13020 KB |
Output is correct |
79 |
Correct |
106 ms |
12696 KB |
Output is correct |
80 |
Correct |
108 ms |
12948 KB |
Output is correct |
81 |
Correct |
100 ms |
12608 KB |
Output is correct |
82 |
Correct |
79 ms |
13324 KB |
Output is correct |
83 |
Correct |
63 ms |
12892 KB |
Output is correct |
84 |
Correct |
525 ms |
13572 KB |
Output is correct |
85 |
Correct |
910 ms |
12392 KB |
Output is correct |
86 |
Correct |
120 ms |
6748 KB |
Output is correct |
87 |
Correct |
118 ms |
6748 KB |
Output is correct |
88 |
Correct |
840 ms |
12384 KB |
Output is correct |
89 |
Correct |
257 ms |
7896 KB |
Output is correct |
90 |
Correct |
117 ms |
6748 KB |
Output is correct |