#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
ll N, K, l[100005], d[100005], r[100005], u[100005];
ll ans;
pll operator+(pll p1, pll p2)
{
return pll(p1.F + p2.F, p1.S + p2.S);
}
bool isB(int x, int y, int M)
{
return (x / M + y / M) % 2;
}
pll calcCorner(ll L, ll R, ll D, ll U, int M)
{
if (isB(L, D, M))
return pll((R - L) * (U - D), 0);
else
return pll(0, (R - L) * (U - D));
}
pll calcColumn(ll L, ll R, ll D, ll U, int M)
{
pll res;
ll blocks = (U - D) / M;
if (blocks % 2 == 1)
res = pll((R - L) * (blocks / 2 + 1) * M, (R - L) * (blocks / 2) * M);
else
res = pll((R - L) * (blocks / 2) * M, (R - L) * (blocks / 2) * M);
if (!isB(L, D, M) && blocks % 2 == 1)
swap(res.F, res.S);
return res;
}
pll calcBlock(ll L, ll R, ll D, ll U, ll M)
{
pll res;
ll blocks = (R - L) * (U - D) / M / M;
if (blocks % 2 == 1)
res = pll(M * M * (blocks / 2 + 1), M * M * (blocks / 2));
else
res = pll(M * M * (blocks / 2), M * M * (blocks / 2));
if (!isB(L, D, M) && blocks % 2 == 1)
swap(res.F, res.S);
return res;
}
void solve(int M)
{
pll res = pll(0, 0), board = calcBlock(0, N, 0, N, M);
for (int i = 1; i <= K; i++)
{
ll mL = (l[i] + M - 1) / M * M,
mR = r[i] / M * M,
mD = (d[i] + M - 1) / M * M,
mU = u[i] / M * M;
pll cur;
if (mL > mR && mD > mU)
cur = calcCorner(l[i], r[i], d[i], u[i], M);
else if (mL > mR)
cur = calcCorner(l[i], r[i], d[i], mD, M) +
calcColumn(l[i], r[i], mD, mU, M) +
calcCorner(l[i], r[i], mU, u[i], M);
else if (mD > mU)
cur = calcCorner(l[i], mL, d[i], u[i], M) +
calcColumn(d[i], u[i], mL, mR, M) +
calcCorner(mR, r[i], d[i], u[i], M);
else
cur = calcCorner(l[i], mL, d[i], mD, M) + calcColumn(d[i], mD, mL, mR, M) + calcCorner(mR, r[i], d[i], mD, M) +
calcColumn(l[i], mL, mD, mU, M) + calcBlock(mL, mR, mD, mU, M) + calcColumn(mR, r[i], mD, mU, M) +
calcCorner(l[i], mL, mU, u[i], M) + calcColumn(mU, u[i], mL, mR, M) + calcCorner(mR, r[i], mU, u[i], M);
res = res + cur;
}
// cerr << M << ' ' << board.F << ' ' << board.S << '\n';
ans = min({ans, board.F - res.F + res.S, res.F + board.S - res.S});
}
signed main()
{
cin >> N >> K;
ans = N * N;
for (int i = 1; i <= K; i++)
{
cin >> l[i] >> d[i] >> r[i] >> u[i];
l[i]--, d[i]--;
}
for (int i = 1; i < N; i++)
if (N % i == 0)
solve(i);
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
2244 KB |
Output is correct |
2 |
Correct |
18 ms |
724 KB |
Output is correct |
3 |
Correct |
46 ms |
1484 KB |
Output is correct |
4 |
Correct |
45 ms |
1732 KB |
Output is correct |
5 |
Correct |
72 ms |
1980 KB |
Output is correct |
6 |
Correct |
40 ms |
1356 KB |
Output is correct |
7 |
Correct |
8 ms |
468 KB |
Output is correct |
8 |
Correct |
40 ms |
1392 KB |
Output is correct |
9 |
Correct |
101 ms |
3036 KB |
Output is correct |
10 |
Correct |
56 ms |
1804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
28 ms |
1184 KB |
Output is correct |
17 |
Correct |
76 ms |
2852 KB |
Output is correct |
18 |
Correct |
101 ms |
3392 KB |
Output is correct |
19 |
Correct |
184 ms |
3040 KB |
Output is correct |
20 |
Correct |
197 ms |
3360 KB |
Output is correct |
21 |
Correct |
75 ms |
2832 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
51 ms |
1612 KB |
Output is correct |
24 |
Correct |
88 ms |
3020 KB |
Output is correct |
25 |
Correct |
11 ms |
468 KB |
Output is correct |
26 |
Correct |
57 ms |
2136 KB |
Output is correct |
27 |
Correct |
77 ms |
2328 KB |
Output is correct |
28 |
Correct |
97 ms |
3284 KB |
Output is correct |
29 |
Correct |
30 ms |
1228 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
2244 KB |
Output is correct |
2 |
Correct |
18 ms |
724 KB |
Output is correct |
3 |
Correct |
46 ms |
1484 KB |
Output is correct |
4 |
Correct |
45 ms |
1732 KB |
Output is correct |
5 |
Correct |
72 ms |
1980 KB |
Output is correct |
6 |
Correct |
40 ms |
1356 KB |
Output is correct |
7 |
Correct |
8 ms |
468 KB |
Output is correct |
8 |
Correct |
40 ms |
1392 KB |
Output is correct |
9 |
Correct |
101 ms |
3036 KB |
Output is correct |
10 |
Correct |
56 ms |
1804 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
28 ms |
1184 KB |
Output is correct |
27 |
Correct |
76 ms |
2852 KB |
Output is correct |
28 |
Correct |
101 ms |
3392 KB |
Output is correct |
29 |
Correct |
184 ms |
3040 KB |
Output is correct |
30 |
Correct |
197 ms |
3360 KB |
Output is correct |
31 |
Correct |
75 ms |
2832 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
51 ms |
1612 KB |
Output is correct |
34 |
Correct |
88 ms |
3020 KB |
Output is correct |
35 |
Correct |
11 ms |
468 KB |
Output is correct |
36 |
Correct |
57 ms |
2136 KB |
Output is correct |
37 |
Correct |
77 ms |
2328 KB |
Output is correct |
38 |
Correct |
97 ms |
3284 KB |
Output is correct |
39 |
Correct |
30 ms |
1228 KB |
Output is correct |
40 |
Correct |
3 ms |
340 KB |
Output is correct |
41 |
Correct |
180 ms |
2876 KB |
Output is correct |
42 |
Correct |
116 ms |
3220 KB |
Output is correct |
43 |
Correct |
135 ms |
2972 KB |
Output is correct |
44 |
Correct |
117 ms |
3072 KB |
Output is correct |
45 |
Correct |
114 ms |
3384 KB |
Output is correct |
46 |
Correct |
202 ms |
3280 KB |
Output is correct |
47 |
Correct |
98 ms |
2944 KB |
Output is correct |
48 |
Correct |
121 ms |
3024 KB |
Output is correct |
49 |
Correct |
105 ms |
2912 KB |
Output is correct |
50 |
Correct |
560 ms |
3252 KB |
Output is correct |
51 |
Correct |
593 ms |
3332 KB |
Output is correct |
52 |
Correct |
559 ms |
3072 KB |
Output is correct |
53 |
Correct |
597 ms |
3412 KB |
Output is correct |
54 |
Correct |
549 ms |
3020 KB |
Output is correct |
55 |
Correct |
615 ms |
3388 KB |
Output is correct |
56 |
Correct |
534 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
71 ms |
2244 KB |
Output is correct |
10 |
Correct |
18 ms |
724 KB |
Output is correct |
11 |
Correct |
46 ms |
1484 KB |
Output is correct |
12 |
Correct |
45 ms |
1732 KB |
Output is correct |
13 |
Correct |
72 ms |
1980 KB |
Output is correct |
14 |
Correct |
40 ms |
1356 KB |
Output is correct |
15 |
Correct |
8 ms |
468 KB |
Output is correct |
16 |
Correct |
40 ms |
1392 KB |
Output is correct |
17 |
Correct |
101 ms |
3036 KB |
Output is correct |
18 |
Correct |
56 ms |
1804 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
28 ms |
1184 KB |
Output is correct |
35 |
Correct |
76 ms |
2852 KB |
Output is correct |
36 |
Correct |
101 ms |
3392 KB |
Output is correct |
37 |
Correct |
184 ms |
3040 KB |
Output is correct |
38 |
Correct |
197 ms |
3360 KB |
Output is correct |
39 |
Correct |
75 ms |
2832 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
51 ms |
1612 KB |
Output is correct |
42 |
Correct |
88 ms |
3020 KB |
Output is correct |
43 |
Correct |
11 ms |
468 KB |
Output is correct |
44 |
Correct |
57 ms |
2136 KB |
Output is correct |
45 |
Correct |
77 ms |
2328 KB |
Output is correct |
46 |
Correct |
97 ms |
3284 KB |
Output is correct |
47 |
Correct |
30 ms |
1228 KB |
Output is correct |
48 |
Correct |
3 ms |
340 KB |
Output is correct |
49 |
Correct |
180 ms |
2876 KB |
Output is correct |
50 |
Correct |
116 ms |
3220 KB |
Output is correct |
51 |
Correct |
135 ms |
2972 KB |
Output is correct |
52 |
Correct |
117 ms |
3072 KB |
Output is correct |
53 |
Correct |
114 ms |
3384 KB |
Output is correct |
54 |
Correct |
202 ms |
3280 KB |
Output is correct |
55 |
Correct |
98 ms |
2944 KB |
Output is correct |
56 |
Correct |
121 ms |
3024 KB |
Output is correct |
57 |
Correct |
105 ms |
2912 KB |
Output is correct |
58 |
Correct |
560 ms |
3252 KB |
Output is correct |
59 |
Correct |
593 ms |
3332 KB |
Output is correct |
60 |
Correct |
559 ms |
3072 KB |
Output is correct |
61 |
Correct |
597 ms |
3412 KB |
Output is correct |
62 |
Correct |
549 ms |
3020 KB |
Output is correct |
63 |
Correct |
615 ms |
3388 KB |
Output is correct |
64 |
Correct |
534 ms |
3028 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
650 ms |
3348 KB |
Output is correct |
68 |
Correct |
643 ms |
5524 KB |
Output is correct |
69 |
Correct |
558 ms |
4772 KB |
Output is correct |
70 |
Correct |
606 ms |
5196 KB |
Output is correct |
71 |
Correct |
611 ms |
5204 KB |
Output is correct |
72 |
Correct |
603 ms |
5196 KB |
Output is correct |
73 |
Correct |
594 ms |
4996 KB |
Output is correct |
74 |
Correct |
639 ms |
5324 KB |
Output is correct |
75 |
Correct |
619 ms |
5276 KB |
Output is correct |
76 |
Correct |
649 ms |
5432 KB |
Output is correct |
77 |
Correct |
181 ms |
5712 KB |
Output is correct |
78 |
Correct |
109 ms |
5316 KB |
Output is correct |
79 |
Correct |
146 ms |
5052 KB |
Output is correct |
80 |
Correct |
152 ms |
5164 KB |
Output is correct |
81 |
Correct |
136 ms |
4780 KB |
Output is correct |
82 |
Correct |
139 ms |
5368 KB |
Output is correct |
83 |
Correct |
123 ms |
5148 KB |
Output is correct |
84 |
Correct |
429 ms |
5624 KB |
Output is correct |
85 |
Correct |
680 ms |
5824 KB |
Output is correct |
86 |
Correct |
2 ms |
212 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |
88 |
Correct |
690 ms |
5760 KB |
Output is correct |
89 |
Correct |
138 ms |
1236 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |