#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define f first
#define s second
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define pii pair<int, int>
#define SZ(x) (int)((x).size())
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define MX(a,b) a = max(a,b)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S && ...y){cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
struct rect{
int x,y,d;
};
int ps[2][100005];
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int n,k; cin>>n>>k;
vector<rect> v; v.reserve(k*4);
ll rr = 0;
REP(i,k) {
ll r1,c1,r2,c2; cin>>r1>>c1>>r2>>c2;
rr += (r2-r1+1) * (c2-c1+1);
--r1; --c1;
v.pb({r2,c2,1});
v.pb({r1,c1,1});
v.pb({r2,c1,-1});
v.pb({r1,c2,-1});
}
ll re = 1e18;
for (int d = 1; d<n; ++d) {
if (n % d == 0) {
ll totb = (n*n + ((n/d)&1?d*d:0))/2;
REP1(i,n) {
if (i) ps[0][i] = ps[0][i-1];
ps[0][i] += ((i-1)/d)%2==0;
ps[1][i] = i-ps[0][i];
}
bug(d, totb);
int over = 0;
for (rect &r : v) {
if (r.x>=0&&r.y>=0) {
int tmp = 0;
int rx = r.x/d, ry = r.y/d;
tmp += (r.x-rx*d) * (ps[((r.x-1)/d)&1][r.y]);
// r.x -= r.x%d;
tmp += (r.y-ry*d) * (ps[((r.y-1)/d)&1][rx*d]);
// r.y -= r.y%d;
// --r.x; --r.y;
if (((rx)&1) && ((ry)&1)) {
tmp += ((rx*ry+1)*d*d)/2;
}else{
tmp += rx*ry*d*d/2;
}
over += tmp * r.d;
}
}
bug(over);
bug(totb + rr - over*2);
// MX(over, rr-over);
re = min(re, totb + rr - over*2);
over = rr-over;
re = min(re, n*n-totb + rr - over*2);
}
}
cout<<re<<endl;
}
/*
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
7148 KB |
Output is correct |
2 |
Correct |
8 ms |
2156 KB |
Output is correct |
3 |
Correct |
21 ms |
5356 KB |
Output is correct |
4 |
Correct |
19 ms |
4716 KB |
Output is correct |
5 |
Correct |
28 ms |
6380 KB |
Output is correct |
6 |
Correct |
22 ms |
4332 KB |
Output is correct |
7 |
Correct |
5 ms |
1388 KB |
Output is correct |
8 |
Correct |
16 ms |
4332 KB |
Output is correct |
9 |
Correct |
40 ms |
9324 KB |
Output is correct |
10 |
Correct |
23 ms |
5612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
16 ms |
2924 KB |
Output is correct |
17 |
Correct |
34 ms |
8256 KB |
Output is correct |
18 |
Correct |
56 ms |
9708 KB |
Output is correct |
19 |
Correct |
249 ms |
8684 KB |
Output is correct |
20 |
Correct |
278 ms |
9600 KB |
Output is correct |
21 |
Correct |
30 ms |
7916 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
41 ms |
4332 KB |
Output is correct |
24 |
Correct |
51 ms |
8940 KB |
Output is correct |
25 |
Correct |
9 ms |
1132 KB |
Output is correct |
26 |
Correct |
32 ms |
5612 KB |
Output is correct |
27 |
Correct |
51 ms |
6764 KB |
Output is correct |
28 |
Correct |
54 ms |
9324 KB |
Output is correct |
29 |
Correct |
14 ms |
3564 KB |
Output is correct |
30 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
7148 KB |
Output is correct |
2 |
Correct |
8 ms |
2156 KB |
Output is correct |
3 |
Correct |
21 ms |
5356 KB |
Output is correct |
4 |
Correct |
19 ms |
4716 KB |
Output is correct |
5 |
Correct |
28 ms |
6380 KB |
Output is correct |
6 |
Correct |
22 ms |
4332 KB |
Output is correct |
7 |
Correct |
5 ms |
1388 KB |
Output is correct |
8 |
Correct |
16 ms |
4332 KB |
Output is correct |
9 |
Correct |
40 ms |
9324 KB |
Output is correct |
10 |
Correct |
23 ms |
5612 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
16 ms |
2924 KB |
Output is correct |
27 |
Correct |
34 ms |
8256 KB |
Output is correct |
28 |
Correct |
56 ms |
9708 KB |
Output is correct |
29 |
Correct |
249 ms |
8684 KB |
Output is correct |
30 |
Correct |
278 ms |
9600 KB |
Output is correct |
31 |
Correct |
30 ms |
7916 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
41 ms |
4332 KB |
Output is correct |
34 |
Correct |
51 ms |
8940 KB |
Output is correct |
35 |
Correct |
9 ms |
1132 KB |
Output is correct |
36 |
Correct |
32 ms |
5612 KB |
Output is correct |
37 |
Correct |
51 ms |
6764 KB |
Output is correct |
38 |
Correct |
54 ms |
9324 KB |
Output is correct |
39 |
Correct |
14 ms |
3564 KB |
Output is correct |
40 |
Correct |
2 ms |
620 KB |
Output is correct |
41 |
Correct |
247 ms |
9708 KB |
Output is correct |
42 |
Correct |
70 ms |
10732 KB |
Output is correct |
43 |
Correct |
135 ms |
9708 KB |
Output is correct |
44 |
Correct |
63 ms |
10476 KB |
Output is correct |
45 |
Correct |
44 ms |
11116 KB |
Output is correct |
46 |
Correct |
291 ms |
10732 KB |
Output is correct |
47 |
Correct |
39 ms |
9964 KB |
Output is correct |
48 |
Correct |
101 ms |
10092 KB |
Output is correct |
49 |
Correct |
57 ms |
9836 KB |
Output is correct |
50 |
Correct |
1340 ms |
10220 KB |
Output is correct |
51 |
Correct |
1441 ms |
10732 KB |
Output is correct |
52 |
Correct |
1347 ms |
10092 KB |
Output is correct |
53 |
Correct |
1416 ms |
10604 KB |
Output is correct |
54 |
Correct |
1296 ms |
9964 KB |
Output is correct |
55 |
Correct |
1476 ms |
10988 KB |
Output is correct |
56 |
Correct |
1271 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
33 ms |
7148 KB |
Output is correct |
10 |
Correct |
8 ms |
2156 KB |
Output is correct |
11 |
Correct |
21 ms |
5356 KB |
Output is correct |
12 |
Correct |
19 ms |
4716 KB |
Output is correct |
13 |
Correct |
28 ms |
6380 KB |
Output is correct |
14 |
Correct |
22 ms |
4332 KB |
Output is correct |
15 |
Correct |
5 ms |
1388 KB |
Output is correct |
16 |
Correct |
16 ms |
4332 KB |
Output is correct |
17 |
Correct |
40 ms |
9324 KB |
Output is correct |
18 |
Correct |
23 ms |
5612 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
16 ms |
2924 KB |
Output is correct |
35 |
Correct |
34 ms |
8256 KB |
Output is correct |
36 |
Correct |
56 ms |
9708 KB |
Output is correct |
37 |
Correct |
249 ms |
8684 KB |
Output is correct |
38 |
Correct |
278 ms |
9600 KB |
Output is correct |
39 |
Correct |
30 ms |
7916 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
41 ms |
4332 KB |
Output is correct |
42 |
Correct |
51 ms |
8940 KB |
Output is correct |
43 |
Correct |
9 ms |
1132 KB |
Output is correct |
44 |
Correct |
32 ms |
5612 KB |
Output is correct |
45 |
Correct |
51 ms |
6764 KB |
Output is correct |
46 |
Correct |
54 ms |
9324 KB |
Output is correct |
47 |
Correct |
14 ms |
3564 KB |
Output is correct |
48 |
Correct |
2 ms |
620 KB |
Output is correct |
49 |
Correct |
247 ms |
9708 KB |
Output is correct |
50 |
Correct |
70 ms |
10732 KB |
Output is correct |
51 |
Correct |
135 ms |
9708 KB |
Output is correct |
52 |
Correct |
63 ms |
10476 KB |
Output is correct |
53 |
Correct |
44 ms |
11116 KB |
Output is correct |
54 |
Correct |
291 ms |
10732 KB |
Output is correct |
55 |
Correct |
39 ms |
9964 KB |
Output is correct |
56 |
Correct |
101 ms |
10092 KB |
Output is correct |
57 |
Correct |
57 ms |
9836 KB |
Output is correct |
58 |
Correct |
1340 ms |
10220 KB |
Output is correct |
59 |
Correct |
1441 ms |
10732 KB |
Output is correct |
60 |
Correct |
1347 ms |
10092 KB |
Output is correct |
61 |
Correct |
1416 ms |
10604 KB |
Output is correct |
62 |
Correct |
1296 ms |
9964 KB |
Output is correct |
63 |
Correct |
1476 ms |
10988 KB |
Output is correct |
64 |
Correct |
1271 ms |
9708 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
1 ms |
364 KB |
Output is correct |
67 |
Correct |
1355 ms |
10348 KB |
Output is correct |
68 |
Correct |
1373 ms |
10476 KB |
Output is correct |
69 |
Correct |
1196 ms |
9196 KB |
Output is correct |
70 |
Correct |
1342 ms |
10092 KB |
Output is correct |
71 |
Correct |
1323 ms |
9964 KB |
Output is correct |
72 |
Correct |
1334 ms |
9708 KB |
Output is correct |
73 |
Correct |
1278 ms |
9580 KB |
Output is correct |
74 |
Correct |
1368 ms |
10348 KB |
Output is correct |
75 |
Correct |
1331 ms |
9836 KB |
Output is correct |
76 |
Correct |
1414 ms |
10476 KB |
Output is correct |
77 |
Correct |
223 ms |
11240 KB |
Output is correct |
78 |
Correct |
62 ms |
10476 KB |
Output is correct |
79 |
Correct |
138 ms |
9836 KB |
Output is correct |
80 |
Correct |
151 ms |
10220 KB |
Output is correct |
81 |
Correct |
144 ms |
9580 KB |
Output is correct |
82 |
Correct |
110 ms |
10732 KB |
Output is correct |
83 |
Correct |
80 ms |
9964 KB |
Output is correct |
84 |
Correct |
844 ms |
10932 KB |
Output is correct |
85 |
Correct |
1487 ms |
10988 KB |
Output is correct |
86 |
Correct |
72 ms |
1644 KB |
Output is correct |
87 |
Correct |
71 ms |
1644 KB |
Output is correct |
88 |
Correct |
1542 ms |
11116 KB |
Output is correct |
89 |
Correct |
329 ms |
3436 KB |
Output is correct |
90 |
Correct |
71 ms |
1644 KB |
Output is correct |