#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll, ll> pl;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
void ckmin(ll& a, ll b){
a = min(a, b);
}
void ADD(pl& a, pl b){
a.f+=b.f;
a.s+=b.s;
}
void print(pi a){
cout << a.f << " " << a.s << "\n";
cout.flush();
}
void print(pl a){
cout << "pl: " << a.f << " " << a.s << "\n";
cout.flush();
}
const ll INF = 1e18;
const int mx = 100005;
int N, K;
int X1[mx];
int Y1[mx];
int X2[mx];
int Y2[mx];
pl shouldBe(int X, int Y, int SZ, bool white_corn){
assert(X % SZ == 0 && Y % SZ == 0);
ll squares = ll(X/SZ)*ll(Y/SZ);
ll white_squares = (squares+1)/2;
if(!white_corn){
white_squares = squares-white_squares;
}
ll should_be_white = white_squares*ll(SZ)*ll(SZ);
// cout << "squares: " << squares << "\n";
// cout << "should_be_white: " << should_be_white << "\n";
return mp(should_be_white, ll(X)*ll(Y)-should_be_white);
}
int SZ;
bool shouldBeWhite(const pi& a){
return (a.f/SZ+a.s/SZ + 1) % 2;
}
pl doSingle(pi corner11, pi corner22){
ll area = ll(corner22.s+1-corner11.s)*ll(corner22.f+1-corner11.f);
if(shouldBeWhite(corner11)){
return mp(area, 0);
}
return mp(0, area);
}
pl doEdge(pi corner11, pi corner22){
// cout << corner11.f << " " << corner11.s << " " << corner22.f << " " << corner22.s << "\n";
// cout.flush();
if(corner11.s/SZ == corner22.s/SZ && corner11.f % SZ == 0 && (corner22.f+1) % SZ == 0){
swap(corner11.f, corner11.s);
swap(corner22.f, corner22.s);
}
assert((corner22.s+1) % SZ == 0);
assert(corner11.f/SZ == corner22.f/SZ);
int rect_num = (corner22.s+1-corner11.s)/SZ;
ll rect_area = ll(corner22.f-corner11.f+1)*SZ;
int white_num = (rect_num+1)/2;
int black_num = rect_num-white_num;
if(!shouldBeWhite(corner11)){
swap(white_num, black_num);
}
return mp(rect_area*white_num, rect_area*black_num);
}
pl doBig(pi corner11, pi corner22){
assert(corner11.f % SZ == 0 && corner11.s % SZ == 0);
assert((corner22.f+1) % SZ == 0 && (corner22.s+1) % SZ == 0);
ll rect_num = ll(corner22.f+1-corner11.f)/SZ*ll(corner22.s+1-corner11.s)/SZ;
ll white_num = (rect_num+1)/2;
ll black_num = rect_num-white_num;
if(!shouldBeWhite(corner11)){
swap(white_num, black_num);
}
ll square_area = ll(SZ)*ll(SZ);
return mp(square_area*white_num, square_area*black_num);
}
ll getAns(){ //bottom left should be white, count # of flips
// cout << "SZ = " << SZ << "\n";
// cout.flush();
ll flip_to_white = 0;
ll flip_to_black = 0;
ll area_sum = 0;
for(int i = 1; i <= K; i++){
pi corner11 = mp(X1[i], Y1[i]);
pi corner22 = mp(X2[i], Y2[i]);
area_sum+=ll(corner22.s-corner11.s+1)*ll(corner22.f-corner11.f+1);
pl res = mp(0, 0);
if(corner11.f/SZ < corner22.f/SZ && corner11.s/SZ < corner22.s/SZ){
pi CORNER11 = mp(corner11.f/SZ*SZ+SZ-1, corner11.s/SZ*SZ+SZ-1);
pi CORNER22 = mp(corner22.f/SZ*SZ, corner22.s/SZ*SZ);
// print(corner11);
// print(CORNER11);
// print(CORNER22);
// print(corner22);
ADD(res, doSingle(corner11, CORNER11));
ADD(res, doSingle(CORNER22, corner22));
ADD(res, doSingle(mp(corner11.f, CORNER22.s), mp(CORNER11.f, corner22.s)));
ADD(res, doSingle(mp(CORNER22.f, corner11.s), mp(corner22.f, CORNER11.s)));
// print(res);
ADD(res, doEdge(mp(corner11.f, CORNER11.s+1), mp(CORNER11.f, CORNER22.s-1)));
ADD(res, doEdge(mp(CORNER11.f+1, corner11.s), mp(CORNER22.f-1, CORNER11.s)));
ADD(res, doEdge(mp(CORNER22.f, CORNER11.s+1), mp(corner22.f, CORNER22.s-1)));
ADD(res, doEdge(mp(CORNER11.f+1, CORNER22.s), mp(CORNER22.f-1, corner22.s)));
// print(res);
ADD(res, doBig(mp(CORNER11.f+1, CORNER11.s+1), mp(CORNER22.f-1, CORNER22.s-1)));
// print(res);
}
else if(corner11.f/SZ == corner22.f/SZ && corner11.s/SZ == corner22.s/SZ){
ADD(res, doSingle(corner11, corner22));
}
else{
pi CORNER11 = mp(-1, -1);
pi CORNER22 = mp(-1, -1);
if(corner11.f/SZ == corner22.f/SZ){
CORNER11 = mp(corner22.f, corner11.s/SZ*SZ+SZ-1);
CORNER22 = mp(corner11.f, corner22.s/SZ*SZ);
}
else{
CORNER11 = mp(corner11.f/SZ*SZ+SZ-1, corner22.s);
CORNER22 = mp(corner22.f/SZ*SZ, corner11.s);
}
ADD(res, doSingle(corner11, CORNER11));
// cout << "EDGE" << " " << res.f << " " << res.s << "\n";
ADD(res, doSingle(CORNER22, corner22));
// cout << "EDGE" << " " << res.f << " " << res.s << "\n";
// print(corner11);
// print(CORNER11);
// print(CORNER22);
// print(corner22);
if(corner11.f/SZ == corner22.f/SZ){
ADD(res, doEdge(mp(corner11.f, CORNER11.s+1), mp(corner22.f, CORNER22.s-1)));
// cout << "CASE 1 " << "\n";
}
else{
ADD(res, doEdge(mp(CORNER11.f+1, corner11.s), mp(CORNER22.f-1, corner22.s)));
}
// cout << "EDGE" << " " << res.f << " " << res.s << "\n";
}
// cout << i << " (" << res.f << ", " << res.s << ") \n";
flip_to_white+=res.f;
flip_to_black+=res.s;
}
// assert(area_sum == flip_to_white+flip_to_black);
// cout << "flip_to_white, black: " << flip_to_white << ", " << flip_to_black << "\n";
ll should_be_black = shouldBe(N, N, SZ, 1).s;
return flip_to_white+(should_be_black-flip_to_black);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> N >> K;
for(int i = 1; i <= K; i++){
cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
X1[i]--; Y1[i]--; X2[i]--; Y2[i]--;
}
ll ans = INF;
for(int i = 1; i < N; i++){
if(N % i == 0){
SZ = i;
ll res = getAns();
// cout << "i, res: " << i << ", " << res << "\n";
ckmin(ans, res);
ckmin(ans, ll(N)*ll(N)-res);
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
1284 KB |
Output is correct |
2 |
Correct |
7 ms |
588 KB |
Output is correct |
3 |
Correct |
17 ms |
844 KB |
Output is correct |
4 |
Correct |
20 ms |
1044 KB |
Output is correct |
5 |
Correct |
24 ms |
1176 KB |
Output is correct |
6 |
Correct |
16 ms |
764 KB |
Output is correct |
7 |
Correct |
4 ms |
332 KB |
Output is correct |
8 |
Correct |
17 ms |
844 KB |
Output is correct |
9 |
Correct |
40 ms |
1688 KB |
Output is correct |
10 |
Correct |
27 ms |
1032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
12 ms |
716 KB |
Output is correct |
17 |
Correct |
37 ms |
1568 KB |
Output is correct |
18 |
Correct |
42 ms |
1804 KB |
Output is correct |
19 |
Correct |
91 ms |
1604 KB |
Output is correct |
20 |
Correct |
104 ms |
1852 KB |
Output is correct |
21 |
Correct |
36 ms |
1668 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
23 ms |
888 KB |
Output is correct |
24 |
Correct |
39 ms |
1636 KB |
Output is correct |
25 |
Correct |
5 ms |
332 KB |
Output is correct |
26 |
Correct |
26 ms |
1220 KB |
Output is correct |
27 |
Correct |
32 ms |
1276 KB |
Output is correct |
28 |
Correct |
42 ms |
1804 KB |
Output is correct |
29 |
Correct |
13 ms |
844 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
1284 KB |
Output is correct |
2 |
Correct |
7 ms |
588 KB |
Output is correct |
3 |
Correct |
17 ms |
844 KB |
Output is correct |
4 |
Correct |
20 ms |
1044 KB |
Output is correct |
5 |
Correct |
24 ms |
1176 KB |
Output is correct |
6 |
Correct |
16 ms |
764 KB |
Output is correct |
7 |
Correct |
4 ms |
332 KB |
Output is correct |
8 |
Correct |
17 ms |
844 KB |
Output is correct |
9 |
Correct |
40 ms |
1688 KB |
Output is correct |
10 |
Correct |
27 ms |
1032 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
12 ms |
716 KB |
Output is correct |
27 |
Correct |
37 ms |
1568 KB |
Output is correct |
28 |
Correct |
42 ms |
1804 KB |
Output is correct |
29 |
Correct |
91 ms |
1604 KB |
Output is correct |
30 |
Correct |
104 ms |
1852 KB |
Output is correct |
31 |
Correct |
36 ms |
1668 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
23 ms |
888 KB |
Output is correct |
34 |
Correct |
39 ms |
1636 KB |
Output is correct |
35 |
Correct |
5 ms |
332 KB |
Output is correct |
36 |
Correct |
26 ms |
1220 KB |
Output is correct |
37 |
Correct |
32 ms |
1276 KB |
Output is correct |
38 |
Correct |
42 ms |
1804 KB |
Output is correct |
39 |
Correct |
13 ms |
844 KB |
Output is correct |
40 |
Correct |
2 ms |
332 KB |
Output is correct |
41 |
Correct |
87 ms |
1564 KB |
Output is correct |
42 |
Correct |
57 ms |
1748 KB |
Output is correct |
43 |
Correct |
60 ms |
1496 KB |
Output is correct |
44 |
Correct |
45 ms |
1732 KB |
Output is correct |
45 |
Correct |
45 ms |
1732 KB |
Output is correct |
46 |
Correct |
97 ms |
1732 KB |
Output is correct |
47 |
Correct |
38 ms |
1640 KB |
Output is correct |
48 |
Correct |
52 ms |
1612 KB |
Output is correct |
49 |
Correct |
41 ms |
1608 KB |
Output is correct |
50 |
Correct |
357 ms |
1600 KB |
Output is correct |
51 |
Correct |
383 ms |
1860 KB |
Output is correct |
52 |
Correct |
364 ms |
1728 KB |
Output is correct |
53 |
Correct |
382 ms |
1808 KB |
Output is correct |
54 |
Correct |
346 ms |
1696 KB |
Output is correct |
55 |
Correct |
389 ms |
1936 KB |
Output is correct |
56 |
Correct |
340 ms |
1656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
33 ms |
1284 KB |
Output is correct |
10 |
Correct |
7 ms |
588 KB |
Output is correct |
11 |
Correct |
17 ms |
844 KB |
Output is correct |
12 |
Correct |
20 ms |
1044 KB |
Output is correct |
13 |
Correct |
24 ms |
1176 KB |
Output is correct |
14 |
Correct |
16 ms |
764 KB |
Output is correct |
15 |
Correct |
4 ms |
332 KB |
Output is correct |
16 |
Correct |
17 ms |
844 KB |
Output is correct |
17 |
Correct |
40 ms |
1688 KB |
Output is correct |
18 |
Correct |
27 ms |
1032 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
12 ms |
716 KB |
Output is correct |
35 |
Correct |
37 ms |
1568 KB |
Output is correct |
36 |
Correct |
42 ms |
1804 KB |
Output is correct |
37 |
Correct |
91 ms |
1604 KB |
Output is correct |
38 |
Correct |
104 ms |
1852 KB |
Output is correct |
39 |
Correct |
36 ms |
1668 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
23 ms |
888 KB |
Output is correct |
42 |
Correct |
39 ms |
1636 KB |
Output is correct |
43 |
Correct |
5 ms |
332 KB |
Output is correct |
44 |
Correct |
26 ms |
1220 KB |
Output is correct |
45 |
Correct |
32 ms |
1276 KB |
Output is correct |
46 |
Correct |
42 ms |
1804 KB |
Output is correct |
47 |
Correct |
13 ms |
844 KB |
Output is correct |
48 |
Correct |
2 ms |
332 KB |
Output is correct |
49 |
Correct |
87 ms |
1564 KB |
Output is correct |
50 |
Correct |
57 ms |
1748 KB |
Output is correct |
51 |
Correct |
60 ms |
1496 KB |
Output is correct |
52 |
Correct |
45 ms |
1732 KB |
Output is correct |
53 |
Correct |
45 ms |
1732 KB |
Output is correct |
54 |
Correct |
97 ms |
1732 KB |
Output is correct |
55 |
Correct |
38 ms |
1640 KB |
Output is correct |
56 |
Correct |
52 ms |
1612 KB |
Output is correct |
57 |
Correct |
41 ms |
1608 KB |
Output is correct |
58 |
Correct |
357 ms |
1600 KB |
Output is correct |
59 |
Correct |
383 ms |
1860 KB |
Output is correct |
60 |
Correct |
364 ms |
1728 KB |
Output is correct |
61 |
Correct |
382 ms |
1808 KB |
Output is correct |
62 |
Correct |
346 ms |
1696 KB |
Output is correct |
63 |
Correct |
389 ms |
1936 KB |
Output is correct |
64 |
Correct |
340 ms |
1656 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
0 ms |
332 KB |
Output is correct |
67 |
Correct |
607 ms |
1828 KB |
Output is correct |
68 |
Correct |
595 ms |
1764 KB |
Output is correct |
69 |
Correct |
499 ms |
1604 KB |
Output is correct |
70 |
Correct |
504 ms |
1708 KB |
Output is correct |
71 |
Correct |
546 ms |
1688 KB |
Output is correct |
72 |
Correct |
524 ms |
1664 KB |
Output is correct |
73 |
Correct |
568 ms |
1604 KB |
Output is correct |
74 |
Correct |
585 ms |
1816 KB |
Output is correct |
75 |
Correct |
539 ms |
1672 KB |
Output is correct |
76 |
Correct |
583 ms |
4032 KB |
Output is correct |
77 |
Correct |
136 ms |
4224 KB |
Output is correct |
78 |
Correct |
58 ms |
3728 KB |
Output is correct |
79 |
Correct |
93 ms |
3552 KB |
Output is correct |
80 |
Correct |
98 ms |
3780 KB |
Output is correct |
81 |
Correct |
95 ms |
3488 KB |
Output is correct |
82 |
Correct |
83 ms |
4004 KB |
Output is correct |
83 |
Correct |
75 ms |
3648 KB |
Output is correct |
84 |
Correct |
371 ms |
4124 KB |
Output is correct |
85 |
Correct |
614 ms |
4164 KB |
Output is correct |
86 |
Correct |
3 ms |
332 KB |
Output is correct |
87 |
Correct |
2 ms |
204 KB |
Output is correct |
88 |
Correct |
614 ms |
4256 KB |
Output is correct |
89 |
Correct |
143 ms |
1056 KB |
Output is correct |
90 |
Correct |
1 ms |
332 KB |
Output is correct |