#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
#define int long long
using namespace std;
const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);
int ans = 1e18;
int n, k, x[2][N], y[2][N];
int get(int x1, int y1, int x2, int y2, int div,int f){
int blx = (x1 + div - 1ll) / div, blx2 = (x2 + div - 1ll) / div;
int bly = (y1 + div - 1ll) / div, bly2 = (y2 + div - 1ll) / div;
int res = 0ll, odd, even;
if(blx == blx2){
int val = x2 - x1 + 1ll;
if(bly != bly2){
int end = bly * div;
val = val * (end - y1 + 1ll);
bly++;
if(bly2 > bly){
int dif = bly2 - bly;
val += dif / 2ll * ((x2 - x1 + 1ll) * div);
}
int start = (bly2 - 1) * div + 1;
int len = y2 - start + 1;
if((bly2 + blx) % 2ll == f){
val += len * odd;
}
int p = (x2 - x1 + 1ll) * (y2 - y1 + 1ll);
p = p - val;
if(((bly - 1ll) + blx) % 2ll == f){
res += val;
}
else{
res += p;
}
}
else{
int p = (x2 - x1 + 1ll) * (y2 - y1 + 1ll);
if((bly + blx) % 2ll == f){
res += p;
}
}
}
else{
int end = blx * div;
int odd = end - x1 + 1ll;
blx++;
if(blx2 > blx){
int dif = blx2 - blx + 1ll;
odd += dif / 2ll * div;
}
int op = (x2 - x1 + 1);
op -= odd;
if(bly != bly2){
end = bly * div;
int val = end - y1 + 1ll;
val = val * odd;
bly++;
if(bly2 > bly){
int dif = bly2 - bly;
val += dif / 2ll * odd * div;
val += (dif - dif / 2ll) * op * div;
}
int start = (bly2 - 1) * div + 1;
int len = y2 - start + 1;
if((bly2 + blx - 1) % 2ll == f){
val += len * odd;
}
else{
val += len * op;
}
int p = (x2 - x1 + 1ll) * (y2 - y1 + 1ll);
p = p - val;
if((bly + blx) % 2ll == f){
res += val;
}
else{
res += p;
}
}
else{
int p = (y2 - y1 + 1ll) * odd;
if(((blx - 1ll) + bly) % 2ll == f){
res += p;
}
}
}
return res;
}
void calc(int div){
int bl = n / div, cnt = 0ll, x1, y1, cnt1 = 0ll, x2, y2;
if(bl & 1ll){
cnt += div * div * ((bl + 1ll) / 2ll) * ((bl + 1ll) / 2ll);
cnt += div * div * (bl / 2ll) * (bl / 2);
cnt1 = n * n - cnt;
for(int i = 1;i <= k; i++){
x1 = x[0ll][i];
y1 = y[0ll][i];
x2 = x[1ll][i];
y2 = y[1ll][i];
cnt -= get(x1, y1, x2, y2, div, 0ll);
cnt += get(x1, y1, x2, y2, div, 1ll);
cnt1 += get(x1, y1, x2, y2, div, 0ll);
cnt1 -= get(x1, y1, x2, y2, div, 1ll);
}
ans = min(ans, cnt);
ans = min(ans, cnt1);
}
else{
cnt += div * div * (bl / 2ll) * bl;
cnt1 = n * n - cnt;
for(int i = 1ll;i <= k; i++){
x1 = x[0ll][i];
y1 = y[0ll][i];
x2 = x[1ll][i];
y2 = y[1ll][i];
cnt -= get(x1, y1, x2, y2, div, 0ll);
cnt += get(x1, y1, x2, y2, div, 1ll);
cnt1 += get(x1, y1, x2, y2, div, 0ll);
cnt1 -= get(x1, y1, x2, y2, div, 1ll);
}
ans = min(ans, cnt);
ans = min(ans, cnt1);
}
}
main()
{
cin >> n >> k;
for(int i = 1ll;i <= k; i++){
scanf("%lld%lld%lld%lld", &x[0ll][i], &y[0ll][i], &x[1ll][i], &y[1ll][i]);
}
//x1 = x[0ll][1];
//y1 = y[0ll][1];
//x2 = x[1ll][1];
//y2 = y[1ll][1];
//cout << get(x1, y1, x2, y2, 3ll, 0ll);
if(n % 2ll == 1ll){
int x1, y1, x2, y2;
int cnt = n * n / 2ll + 1ll, cnt1 = n * n / 2ll;
for(int i = 1;i <= k; i++){
x1 = x[0ll][i];
y1 = y[0ll][i];
x2 = x[1ll][i];
y2 = y[1ll][i];
cnt -= get(x1, y1, x2, y2, 1ll, 0ll);
cnt += get(x1, y1, x2, y2, 1ll, 1ll);
cnt1 += get(x1, y1, x2, y2, 1ll, 0ll);
cnt1 -= get(x1, y1, x2, y2, 1ll, 1ll);
}
ans = min(ans, cnt);
ans = min(ans, cnt1);
}
else{
int x1, y1,x2, y2;
int cnt = n * n / 2ll, cnt1 = n * n / 2ll;
for(int i = 1;i <= k; i++){
x1 = x[0ll][i];
y1 = y[0ll][i];
x2 = x[1ll][i];
y2 = y[1ll][i];
cnt -= get(x1, y1, x2, y2, 1ll, 0ll);
cnt += get(x1, y1, x2, y2, 1ll, 1ll);
cnt1 += get(x1, y1, x2, y2, 1ll, 0ll);
cnt1 -= get(x1, y1, x2, y2, 1ll, 1ll);
}
ans = min(ans, cnt);
ans = min(ans, cnt1);
}
for(int i = 2ll;i * i <= n; i++){
if(n % i == 0ll){
calc(i);
calc(n / i);
}
}
cout << ans;
}
/*
6 5
3 3 4 4
1 2 1 2
5 5 5 5
2 1 2 1
3 6 3 6
*/
Compilation message
chessboard.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int, long long int)':
chessboard.cpp:22:22: warning: unused variable 'even' [-Wunused-variable]
int res = 0ll, odd, even;
^~~~
chessboard.cpp: At global scope:
chessboard.cpp:138:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
chessboard.cpp: In function 'int main()':
chessboard.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld%lld", &x[0ll][i], &y[0ll][i], &x[1ll][i], &y[1ll][i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chessboard.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int, long long int)':
chessboard.cpp:36:16: warning: 'odd' may be used uninitialized in this function [-Wmaybe-uninitialized]
val += len * odd;
~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
252 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
252 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
2296 KB |
Output is correct |
2 |
Correct |
11 ms |
888 KB |
Output is correct |
3 |
Correct |
25 ms |
1528 KB |
Output is correct |
4 |
Correct |
28 ms |
1784 KB |
Output is correct |
5 |
Correct |
34 ms |
2012 KB |
Output is correct |
6 |
Correct |
22 ms |
1428 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
23 ms |
1400 KB |
Output is correct |
9 |
Correct |
54 ms |
3192 KB |
Output is correct |
10 |
Correct |
31 ms |
1844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
452 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
452 KB |
Output is correct |
13 |
Correct |
3 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
22 ms |
1272 KB |
Output is correct |
17 |
Correct |
44 ms |
2944 KB |
Output is correct |
18 |
Correct |
72 ms |
3448 KB |
Output is correct |
19 |
Correct |
290 ms |
3064 KB |
Output is correct |
20 |
Correct |
324 ms |
3576 KB |
Output is correct |
21 |
Correct |
42 ms |
2808 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
50 ms |
1660 KB |
Output is correct |
24 |
Correct |
66 ms |
3192 KB |
Output is correct |
25 |
Correct |
12 ms |
632 KB |
Output is correct |
26 |
Correct |
42 ms |
2040 KB |
Output is correct |
27 |
Correct |
65 ms |
2560 KB |
Output is correct |
28 |
Correct |
69 ms |
3320 KB |
Output is correct |
29 |
Correct |
19 ms |
1400 KB |
Output is correct |
30 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
2296 KB |
Output is correct |
2 |
Correct |
11 ms |
888 KB |
Output is correct |
3 |
Correct |
25 ms |
1528 KB |
Output is correct |
4 |
Correct |
28 ms |
1784 KB |
Output is correct |
5 |
Correct |
34 ms |
2012 KB |
Output is correct |
6 |
Correct |
22 ms |
1428 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
23 ms |
1400 KB |
Output is correct |
9 |
Correct |
54 ms |
3192 KB |
Output is correct |
10 |
Correct |
31 ms |
1844 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
452 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
22 ms |
1272 KB |
Output is correct |
27 |
Correct |
44 ms |
2944 KB |
Output is correct |
28 |
Correct |
72 ms |
3448 KB |
Output is correct |
29 |
Correct |
290 ms |
3064 KB |
Output is correct |
30 |
Correct |
324 ms |
3576 KB |
Output is correct |
31 |
Correct |
42 ms |
2808 KB |
Output is correct |
32 |
Correct |
3 ms |
376 KB |
Output is correct |
33 |
Correct |
50 ms |
1660 KB |
Output is correct |
34 |
Correct |
66 ms |
3192 KB |
Output is correct |
35 |
Correct |
12 ms |
632 KB |
Output is correct |
36 |
Correct |
42 ms |
2040 KB |
Output is correct |
37 |
Correct |
65 ms |
2560 KB |
Output is correct |
38 |
Correct |
69 ms |
3320 KB |
Output is correct |
39 |
Correct |
19 ms |
1400 KB |
Output is correct |
40 |
Correct |
4 ms |
376 KB |
Output is correct |
41 |
Correct |
244 ms |
3036 KB |
Output is correct |
42 |
Correct |
77 ms |
3320 KB |
Output is correct |
43 |
Correct |
139 ms |
2984 KB |
Output is correct |
44 |
Correct |
74 ms |
3192 KB |
Output is correct |
45 |
Correct |
59 ms |
3320 KB |
Output is correct |
46 |
Correct |
271 ms |
3324 KB |
Output is correct |
47 |
Correct |
52 ms |
3064 KB |
Output is correct |
48 |
Correct |
108 ms |
3036 KB |
Output is correct |
49 |
Correct |
69 ms |
2988 KB |
Output is correct |
50 |
Correct |
1258 ms |
3292 KB |
Output is correct |
51 |
Correct |
1340 ms |
3404 KB |
Output is correct |
52 |
Correct |
1250 ms |
3196 KB |
Output is correct |
53 |
Correct |
1332 ms |
3376 KB |
Output is correct |
54 |
Correct |
1234 ms |
3156 KB |
Output is correct |
55 |
Correct |
1383 ms |
3448 KB |
Output is correct |
56 |
Correct |
1194 ms |
3068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
252 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
252 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
38 ms |
2296 KB |
Output is correct |
10 |
Correct |
11 ms |
888 KB |
Output is correct |
11 |
Correct |
25 ms |
1528 KB |
Output is correct |
12 |
Correct |
28 ms |
1784 KB |
Output is correct |
13 |
Correct |
34 ms |
2012 KB |
Output is correct |
14 |
Correct |
22 ms |
1428 KB |
Output is correct |
15 |
Correct |
6 ms |
504 KB |
Output is correct |
16 |
Correct |
23 ms |
1400 KB |
Output is correct |
17 |
Correct |
54 ms |
3192 KB |
Output is correct |
18 |
Correct |
31 ms |
1844 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
452 KB |
Output is correct |
31 |
Correct |
3 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
376 KB |
Output is correct |
33 |
Correct |
3 ms |
376 KB |
Output is correct |
34 |
Correct |
22 ms |
1272 KB |
Output is correct |
35 |
Correct |
44 ms |
2944 KB |
Output is correct |
36 |
Correct |
72 ms |
3448 KB |
Output is correct |
37 |
Correct |
290 ms |
3064 KB |
Output is correct |
38 |
Correct |
324 ms |
3576 KB |
Output is correct |
39 |
Correct |
42 ms |
2808 KB |
Output is correct |
40 |
Correct |
3 ms |
376 KB |
Output is correct |
41 |
Correct |
50 ms |
1660 KB |
Output is correct |
42 |
Correct |
66 ms |
3192 KB |
Output is correct |
43 |
Correct |
12 ms |
632 KB |
Output is correct |
44 |
Correct |
42 ms |
2040 KB |
Output is correct |
45 |
Correct |
65 ms |
2560 KB |
Output is correct |
46 |
Correct |
69 ms |
3320 KB |
Output is correct |
47 |
Correct |
19 ms |
1400 KB |
Output is correct |
48 |
Correct |
4 ms |
376 KB |
Output is correct |
49 |
Correct |
244 ms |
3036 KB |
Output is correct |
50 |
Correct |
77 ms |
3320 KB |
Output is correct |
51 |
Correct |
139 ms |
2984 KB |
Output is correct |
52 |
Correct |
74 ms |
3192 KB |
Output is correct |
53 |
Correct |
59 ms |
3320 KB |
Output is correct |
54 |
Correct |
271 ms |
3324 KB |
Output is correct |
55 |
Correct |
52 ms |
3064 KB |
Output is correct |
56 |
Correct |
108 ms |
3036 KB |
Output is correct |
57 |
Correct |
69 ms |
2988 KB |
Output is correct |
58 |
Correct |
1258 ms |
3292 KB |
Output is correct |
59 |
Correct |
1340 ms |
3404 KB |
Output is correct |
60 |
Correct |
1250 ms |
3196 KB |
Output is correct |
61 |
Correct |
1332 ms |
3376 KB |
Output is correct |
62 |
Correct |
1234 ms |
3156 KB |
Output is correct |
63 |
Correct |
1383 ms |
3448 KB |
Output is correct |
64 |
Correct |
1194 ms |
3068 KB |
Output is correct |
65 |
Correct |
2 ms |
376 KB |
Output is correct |
66 |
Correct |
2 ms |
376 KB |
Output is correct |
67 |
Incorrect |
1351 ms |
3320 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |