//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
const int N = 1e5 + 7, inf = 1e18;
int n, k, x[N], y[N], x2[N], y2[N], ans = inf;
int A, S;
int num(int x) {
return ((x - 1) / A) & 1;
}
int open(int x) {
if (x == 0) {
return 0;
}
return (x - 1) / A + 1;
}
int close(int x) {
return x / A;
}
int last_close(int x) {
return close(x) * A;
}
int next_close(int x) {
int tmp = last_close(x);
if (x == tmp) {
return x;
}
return tmp + A;
}
int last_open(int x) {
return (open(x) - 1) * A + 1;
}
int next_open(int x) {
int tmp = last_open(x);
if (x == tmp) {
return x;
}
return tmp + A;
}
pii get_colors(int l, int r) {
int blocks = close(r) - open(l - 1);
int a = 0, b = 0;
if (blocks > 0) {
if (num(l) == 0) {
int len = next_open(l) - l;
a += len;
if (len > 0) {
a += (blocks / 2) * A;
b += ((blocks + 1) / 2) * A;
} else {
a += ((blocks + 1) / 2) * A;
b += (blocks / 2) * A;
}
} else {
int len = next_open(l) - l;
b += len;
if (len > 0) {
a += ((blocks + 1) / 2) * A;
b += (blocks / 2) * A;
} else {
a += (blocks / 2) * A;
b += ((blocks + 1) / 2) * A;
}
}
if (num(r) == 0) {
a += r - last_close(r);
} else {
b += r - last_close(r);
}
} else {
if (num(l) == 0) {
a += next_open(l) - l;
} else {
b += next_open(l) - l;
}
if (num(r) == 0) {
a += r - next_open(l) + 1;
} else {
b += r - next_open(l) + 1;
}
}
return {a, b};
}
pii get_squares() {
int even = (n / A) / 2, odd = ((n / A) + 1) / 2;
int a = even * odd + odd * even;
int b = odd * odd + even * even;
return {a * A * A, b * A * A};
}
int get_color(int x, int y, int z) {
return (num(x) + num(y) + z) & 1;
}
int get_ans() {
int res0 = 0, res1 = 0;
for (int i = 0; i < k; i++) {
pii t1 = get_colors(y[i], y2[i]);
pii t2 = get_colors(x[i], x2[i]);
int a = t2.fi, b = t2.se;
int c = t1.fi, d = t1.se;
res0 += a * c + b * d;
res1 += b * c + a * d;
}
pii tmp = get_squares();
return min(tmp.se - S + 2LL * res1, tmp.fi - S + 2LL * res0);
}
void solve(int tc) {
cin >> n >> k;
for (int i = 0; i < k; i++) {
cin >> x[i] >> y[i] >> x2[i] >> y2[i];
S += (x2[i] - x[i] + 1) * (y2[i] - y[i] + 1);
}
for (int i = 1; i < n; i++) {
if (n % i == 0) {
A = i;
ans = min(ans, get_ans());
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 0; i < tc; i++) {
solve(i);
// cleanup();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
2300 KB |
Output is correct |
2 |
Correct |
12 ms |
888 KB |
Output is correct |
3 |
Correct |
26 ms |
1656 KB |
Output is correct |
4 |
Correct |
29 ms |
1716 KB |
Output is correct |
5 |
Correct |
36 ms |
2040 KB |
Output is correct |
6 |
Correct |
24 ms |
1404 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
24 ms |
1528 KB |
Output is correct |
9 |
Correct |
55 ms |
3192 KB |
Output is correct |
10 |
Correct |
32 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
348 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 |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 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 |
380 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 |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
348 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 |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
3 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 |
380 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 |
380 KB |
Output is correct |
16 |
Correct |
24 ms |
1144 KB |
Output is correct |
17 |
Correct |
44 ms |
2936 KB |
Output is correct |
18 |
Correct |
79 ms |
3488 KB |
Output is correct |
19 |
Correct |
378 ms |
3172 KB |
Output is correct |
20 |
Correct |
421 ms |
3448 KB |
Output is correct |
21 |
Correct |
44 ms |
2808 KB |
Output is correct |
22 |
Correct |
3 ms |
380 KB |
Output is correct |
23 |
Correct |
63 ms |
1656 KB |
Output is correct |
24 |
Correct |
73 ms |
3164 KB |
Output is correct |
25 |
Correct |
14 ms |
632 KB |
Output is correct |
26 |
Correct |
46 ms |
2300 KB |
Output is correct |
27 |
Correct |
75 ms |
2424 KB |
Output is correct |
28 |
Correct |
78 ms |
3388 KB |
Output is correct |
29 |
Correct |
19 ms |
1400 KB |
Output is correct |
30 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
2300 KB |
Output is correct |
2 |
Correct |
12 ms |
888 KB |
Output is correct |
3 |
Correct |
26 ms |
1656 KB |
Output is correct |
4 |
Correct |
29 ms |
1716 KB |
Output is correct |
5 |
Correct |
36 ms |
2040 KB |
Output is correct |
6 |
Correct |
24 ms |
1404 KB |
Output is correct |
7 |
Correct |
7 ms |
632 KB |
Output is correct |
8 |
Correct |
24 ms |
1528 KB |
Output is correct |
9 |
Correct |
55 ms |
3192 KB |
Output is correct |
10 |
Correct |
32 ms |
1912 KB |
Output is correct |
11 |
Correct |
3 ms |
348 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 |
3 ms |
376 KB |
Output is correct |
18 |
Correct |
3 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 |
380 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 |
380 KB |
Output is correct |
26 |
Correct |
24 ms |
1144 KB |
Output is correct |
27 |
Correct |
44 ms |
2936 KB |
Output is correct |
28 |
Correct |
79 ms |
3488 KB |
Output is correct |
29 |
Correct |
378 ms |
3172 KB |
Output is correct |
30 |
Correct |
421 ms |
3448 KB |
Output is correct |
31 |
Correct |
44 ms |
2808 KB |
Output is correct |
32 |
Correct |
3 ms |
380 KB |
Output is correct |
33 |
Correct |
63 ms |
1656 KB |
Output is correct |
34 |
Correct |
73 ms |
3164 KB |
Output is correct |
35 |
Correct |
14 ms |
632 KB |
Output is correct |
36 |
Correct |
46 ms |
2300 KB |
Output is correct |
37 |
Correct |
75 ms |
2424 KB |
Output is correct |
38 |
Correct |
78 ms |
3388 KB |
Output is correct |
39 |
Correct |
19 ms |
1400 KB |
Output is correct |
40 |
Correct |
4 ms |
376 KB |
Output is correct |
41 |
Correct |
316 ms |
2988 KB |
Output is correct |
42 |
Correct |
85 ms |
3372 KB |
Output is correct |
43 |
Correct |
172 ms |
3000 KB |
Output is correct |
44 |
Correct |
82 ms |
3192 KB |
Output is correct |
45 |
Correct |
59 ms |
3448 KB |
Output is correct |
46 |
Correct |
351 ms |
3292 KB |
Output is correct |
47 |
Correct |
54 ms |
3064 KB |
Output is correct |
48 |
Correct |
130 ms |
3068 KB |
Output is correct |
49 |
Correct |
76 ms |
2936 KB |
Output is correct |
50 |
Correct |
1740 ms |
3244 KB |
Output is correct |
51 |
Correct |
1849 ms |
3420 KB |
Output is correct |
52 |
Correct |
1721 ms |
3216 KB |
Output is correct |
53 |
Correct |
1815 ms |
3408 KB |
Output is correct |
54 |
Correct |
1696 ms |
3176 KB |
Output is correct |
55 |
Correct |
1975 ms |
3520 KB |
Output is correct |
56 |
Correct |
1645 ms |
3092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 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 |
40 ms |
2300 KB |
Output is correct |
10 |
Correct |
12 ms |
888 KB |
Output is correct |
11 |
Correct |
26 ms |
1656 KB |
Output is correct |
12 |
Correct |
29 ms |
1716 KB |
Output is correct |
13 |
Correct |
36 ms |
2040 KB |
Output is correct |
14 |
Correct |
24 ms |
1404 KB |
Output is correct |
15 |
Correct |
7 ms |
632 KB |
Output is correct |
16 |
Correct |
24 ms |
1528 KB |
Output is correct |
17 |
Correct |
55 ms |
3192 KB |
Output is correct |
18 |
Correct |
32 ms |
1912 KB |
Output is correct |
19 |
Correct |
3 ms |
348 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 |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 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 |
380 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 |
380 KB |
Output is correct |
34 |
Correct |
24 ms |
1144 KB |
Output is correct |
35 |
Correct |
44 ms |
2936 KB |
Output is correct |
36 |
Correct |
79 ms |
3488 KB |
Output is correct |
37 |
Correct |
378 ms |
3172 KB |
Output is correct |
38 |
Correct |
421 ms |
3448 KB |
Output is correct |
39 |
Correct |
44 ms |
2808 KB |
Output is correct |
40 |
Correct |
3 ms |
380 KB |
Output is correct |
41 |
Correct |
63 ms |
1656 KB |
Output is correct |
42 |
Correct |
73 ms |
3164 KB |
Output is correct |
43 |
Correct |
14 ms |
632 KB |
Output is correct |
44 |
Correct |
46 ms |
2300 KB |
Output is correct |
45 |
Correct |
75 ms |
2424 KB |
Output is correct |
46 |
Correct |
78 ms |
3388 KB |
Output is correct |
47 |
Correct |
19 ms |
1400 KB |
Output is correct |
48 |
Correct |
4 ms |
376 KB |
Output is correct |
49 |
Correct |
316 ms |
2988 KB |
Output is correct |
50 |
Correct |
85 ms |
3372 KB |
Output is correct |
51 |
Correct |
172 ms |
3000 KB |
Output is correct |
52 |
Correct |
82 ms |
3192 KB |
Output is correct |
53 |
Correct |
59 ms |
3448 KB |
Output is correct |
54 |
Correct |
351 ms |
3292 KB |
Output is correct |
55 |
Correct |
54 ms |
3064 KB |
Output is correct |
56 |
Correct |
130 ms |
3068 KB |
Output is correct |
57 |
Correct |
76 ms |
2936 KB |
Output is correct |
58 |
Correct |
1740 ms |
3244 KB |
Output is correct |
59 |
Correct |
1849 ms |
3420 KB |
Output is correct |
60 |
Correct |
1721 ms |
3216 KB |
Output is correct |
61 |
Correct |
1815 ms |
3408 KB |
Output is correct |
62 |
Correct |
1696 ms |
3176 KB |
Output is correct |
63 |
Correct |
1975 ms |
3520 KB |
Output is correct |
64 |
Correct |
1645 ms |
3092 KB |
Output is correct |
65 |
Correct |
2 ms |
376 KB |
Output is correct |
66 |
Correct |
2 ms |
376 KB |
Output is correct |
67 |
Correct |
1802 ms |
3308 KB |
Output is correct |
68 |
Correct |
1796 ms |
5532 KB |
Output is correct |
69 |
Correct |
1559 ms |
4984 KB |
Output is correct |
70 |
Correct |
1697 ms |
5376 KB |
Output is correct |
71 |
Correct |
1698 ms |
5340 KB |
Output is correct |
72 |
Correct |
1664 ms |
5212 KB |
Output is correct |
73 |
Correct |
1664 ms |
5092 KB |
Output is correct |
74 |
Correct |
1807 ms |
5496 KB |
Output is correct |
75 |
Correct |
1691 ms |
5268 KB |
Output is correct |
76 |
Correct |
1803 ms |
5596 KB |
Output is correct |
77 |
Correct |
264 ms |
5756 KB |
Output is correct |
78 |
Correct |
86 ms |
5368 KB |
Output is correct |
79 |
Correct |
180 ms |
5172 KB |
Output is correct |
80 |
Correct |
194 ms |
5244 KB |
Output is correct |
81 |
Correct |
176 ms |
4952 KB |
Output is correct |
82 |
Correct |
145 ms |
5496 KB |
Output is correct |
83 |
Correct |
105 ms |
5112 KB |
Output is correct |
84 |
Correct |
1057 ms |
5720 KB |
Output is correct |
85 |
Correct |
1905 ms |
5816 KB |
Output is correct |
86 |
Correct |
4 ms |
376 KB |
Output is correct |
87 |
Correct |
3 ms |
376 KB |
Output is correct |
88 |
Correct |
1899 ms |
5904 KB |
Output is correct |
89 |
Correct |
365 ms |
1400 KB |
Output is correct |
90 |
Correct |
3 ms |
376 KB |
Output is correct |