#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e18;
struct Fenwick {
vector<int> tree;
int n;
void init(int len) {
n = len;
tree.assign(n + 1, 0);
}
void update(int i, int v) {
i++;
while (i <= n) {
tree[i] += v;
i += i & (-i);
}
}
int get(int i) {
i++;
int sum = 0;
while (i > 0) {
sum += tree[i];
i -= i & (-i);
}
return sum;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
};
void solve() {
int n, k;
cin >> n >> k;
vector<vector<pair<int, int>>> in(n + 1), out(n + 1);
for (int i = 0; i < k; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
in[x1].emplace_back(y1, y2);
out[x2].emplace_back(y1, y2);
}
vector<int> del;
for (int i = 1; i * i <= n; i++) {
if (n % i) continue;
del.emplace_back(i);
if (i != 1 and i * i != n) del.emplace_back(n / i);
}
auto get_black = [&](int x, int p) -> int {
if (x == 0) return 0;
int ans = (x - 1) / (2 * p) * p;
int md = (x - 1) % (2 * p);
ans += min(p, md + 1);
return ans;
};
auto get_white = [&](int x, int p) -> int {
if (x == 0) return 0;
int ans = (x - 1) / (2 * p) * p;
int md = (x - 1) % (2 * p);
if (md + 1 > p) ans += md + 1 - p;
return ans;
};
auto color = [&](int x, int p) -> int {
int md = (x - 1) % (2 * p);
return md < p;
};
ll ans = 1e18;
for (int p: del) {
int b_size = get_black(n, p), w_size = get_white(n, p);
// cout << p << ' ' << b_size << ' ' << w_size << endl;
for (int s = 0; s < 2; s++) {
ll need = 0;
int bib = 0, biw = 0;
for (int i = 1; i <= n; i++) {
for (auto [l, r]: in[i]) {
int cnt = get_black(r, p) - get_black(l, p) + color(l, p);
bib += cnt;
biw += (r - l + 1) - cnt;
}
int wib = b_size - bib;
int wiw = w_size - biw;
bool wh = true;
if ((i - 1) / p % 2 == 0) wh = !wh;
if (s) wh = !wh;
need += wh ? biw + wib : bib + wiw;
for (auto [l, r]: out[i]) {
int cnt = get_black(r, p) - get_black(l, p) + color(l, p);
bib -= cnt;
biw -= (r - l + 1) - cnt;
}
}
// cout << p << ' ' << s << ' ' << need << endl;
ans = min(ans, need);
}
}
cout << ans;
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int queries = 1;
// cin >> queries;
for (int test_case = 1; test_case <= queries; test_case++) {
#ifdef sunnatov
cout << "Test case: " << test_case << endl;
#endif
solve();
cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
6992 KB |
Output is correct |
2 |
Correct |
7 ms |
1884 KB |
Output is correct |
3 |
Correct |
19 ms |
7032 KB |
Output is correct |
4 |
Correct |
16 ms |
2520 KB |
Output is correct |
5 |
Correct |
24 ms |
6236 KB |
Output is correct |
6 |
Correct |
17 ms |
4956 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
17 ms |
5112 KB |
Output is correct |
9 |
Correct |
40 ms |
5980 KB |
Output is correct |
10 |
Correct |
22 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
508 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
508 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
10 ms |
1628 KB |
Output is correct |
17 |
Correct |
22 ms |
3676 KB |
Output is correct |
18 |
Correct |
33 ms |
3728 KB |
Output is correct |
19 |
Correct |
109 ms |
3648 KB |
Output is correct |
20 |
Correct |
121 ms |
3672 KB |
Output is correct |
21 |
Correct |
27 ms |
3664 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
21 ms |
1884 KB |
Output is correct |
24 |
Correct |
33 ms |
3676 KB |
Output is correct |
25 |
Correct |
6 ms |
860 KB |
Output is correct |
26 |
Correct |
20 ms |
2392 KB |
Output is correct |
27 |
Correct |
28 ms |
3160 KB |
Output is correct |
28 |
Correct |
32 ms |
3420 KB |
Output is correct |
29 |
Correct |
10 ms |
1920 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
6992 KB |
Output is correct |
2 |
Correct |
7 ms |
1884 KB |
Output is correct |
3 |
Correct |
19 ms |
7032 KB |
Output is correct |
4 |
Correct |
16 ms |
2520 KB |
Output is correct |
5 |
Correct |
24 ms |
6236 KB |
Output is correct |
6 |
Correct |
17 ms |
4956 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
17 ms |
5112 KB |
Output is correct |
9 |
Correct |
40 ms |
5980 KB |
Output is correct |
10 |
Correct |
22 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
464 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
508 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
10 ms |
1628 KB |
Output is correct |
27 |
Correct |
22 ms |
3676 KB |
Output is correct |
28 |
Correct |
33 ms |
3728 KB |
Output is correct |
29 |
Correct |
109 ms |
3648 KB |
Output is correct |
30 |
Correct |
121 ms |
3672 KB |
Output is correct |
31 |
Correct |
27 ms |
3664 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
21 ms |
1884 KB |
Output is correct |
34 |
Correct |
33 ms |
3676 KB |
Output is correct |
35 |
Correct |
6 ms |
860 KB |
Output is correct |
36 |
Correct |
20 ms |
2392 KB |
Output is correct |
37 |
Correct |
28 ms |
3160 KB |
Output is correct |
38 |
Correct |
32 ms |
3420 KB |
Output is correct |
39 |
Correct |
10 ms |
1920 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
148 ms |
10112 KB |
Output is correct |
42 |
Correct |
52 ms |
11356 KB |
Output is correct |
43 |
Correct |
87 ms |
10848 KB |
Output is correct |
44 |
Correct |
51 ms |
11088 KB |
Output is correct |
45 |
Correct |
43 ms |
11600 KB |
Output is correct |
46 |
Correct |
158 ms |
11276 KB |
Output is correct |
47 |
Correct |
38 ms |
10916 KB |
Output is correct |
48 |
Correct |
67 ms |
10832 KB |
Output is correct |
49 |
Correct |
47 ms |
10880 KB |
Output is correct |
50 |
Correct |
645 ms |
10272 KB |
Output is correct |
51 |
Correct |
672 ms |
10580 KB |
Output is correct |
52 |
Correct |
661 ms |
10240 KB |
Output is correct |
53 |
Correct |
699 ms |
10680 KB |
Output is correct |
54 |
Correct |
632 ms |
10324 KB |
Output is correct |
55 |
Correct |
686 ms |
10748 KB |
Output is correct |
56 |
Correct |
616 ms |
10028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
27 ms |
6992 KB |
Output is correct |
10 |
Correct |
7 ms |
1884 KB |
Output is correct |
11 |
Correct |
19 ms |
7032 KB |
Output is correct |
12 |
Correct |
16 ms |
2520 KB |
Output is correct |
13 |
Correct |
24 ms |
6236 KB |
Output is correct |
14 |
Correct |
17 ms |
4956 KB |
Output is correct |
15 |
Correct |
4 ms |
1884 KB |
Output is correct |
16 |
Correct |
17 ms |
5112 KB |
Output is correct |
17 |
Correct |
40 ms |
5980 KB |
Output is correct |
18 |
Correct |
22 ms |
4956 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
464 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
508 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
10 ms |
1628 KB |
Output is correct |
35 |
Correct |
22 ms |
3676 KB |
Output is correct |
36 |
Correct |
33 ms |
3728 KB |
Output is correct |
37 |
Correct |
109 ms |
3648 KB |
Output is correct |
38 |
Correct |
121 ms |
3672 KB |
Output is correct |
39 |
Correct |
27 ms |
3664 KB |
Output is correct |
40 |
Correct |
1 ms |
600 KB |
Output is correct |
41 |
Correct |
21 ms |
1884 KB |
Output is correct |
42 |
Correct |
33 ms |
3676 KB |
Output is correct |
43 |
Correct |
6 ms |
860 KB |
Output is correct |
44 |
Correct |
20 ms |
2392 KB |
Output is correct |
45 |
Correct |
28 ms |
3160 KB |
Output is correct |
46 |
Correct |
32 ms |
3420 KB |
Output is correct |
47 |
Correct |
10 ms |
1920 KB |
Output is correct |
48 |
Correct |
2 ms |
604 KB |
Output is correct |
49 |
Correct |
148 ms |
10112 KB |
Output is correct |
50 |
Correct |
52 ms |
11356 KB |
Output is correct |
51 |
Correct |
87 ms |
10848 KB |
Output is correct |
52 |
Correct |
51 ms |
11088 KB |
Output is correct |
53 |
Correct |
43 ms |
11600 KB |
Output is correct |
54 |
Correct |
158 ms |
11276 KB |
Output is correct |
55 |
Correct |
38 ms |
10916 KB |
Output is correct |
56 |
Correct |
67 ms |
10832 KB |
Output is correct |
57 |
Correct |
47 ms |
10880 KB |
Output is correct |
58 |
Correct |
645 ms |
10272 KB |
Output is correct |
59 |
Correct |
672 ms |
10580 KB |
Output is correct |
60 |
Correct |
661 ms |
10240 KB |
Output is correct |
61 |
Correct |
699 ms |
10680 KB |
Output is correct |
62 |
Correct |
632 ms |
10324 KB |
Output is correct |
63 |
Correct |
686 ms |
10748 KB |
Output is correct |
64 |
Correct |
616 ms |
10028 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
673 ms |
9860 KB |
Output is correct |
68 |
Correct |
649 ms |
9776 KB |
Output is correct |
69 |
Correct |
600 ms |
9060 KB |
Output is correct |
70 |
Correct |
633 ms |
9044 KB |
Output is correct |
71 |
Correct |
643 ms |
9484 KB |
Output is correct |
72 |
Correct |
624 ms |
9260 KB |
Output is correct |
73 |
Correct |
626 ms |
9292 KB |
Output is correct |
74 |
Correct |
643 ms |
9436 KB |
Output is correct |
75 |
Correct |
617 ms |
9044 KB |
Output is correct |
76 |
Correct |
658 ms |
9796 KB |
Output is correct |
77 |
Correct |
119 ms |
10764 KB |
Output is correct |
78 |
Correct |
50 ms |
10324 KB |
Output is correct |
79 |
Correct |
85 ms |
10256 KB |
Output is correct |
80 |
Correct |
88 ms |
10068 KB |
Output is correct |
81 |
Correct |
83 ms |
10076 KB |
Output is correct |
82 |
Correct |
71 ms |
10584 KB |
Output is correct |
83 |
Correct |
56 ms |
10068 KB |
Output is correct |
84 |
Correct |
411 ms |
10576 KB |
Output is correct |
85 |
Correct |
688 ms |
9808 KB |
Output is correct |
86 |
Correct |
114 ms |
4368 KB |
Output is correct |
87 |
Correct |
114 ms |
4188 KB |
Output is correct |
88 |
Correct |
684 ms |
9816 KB |
Output is correct |
89 |
Correct |
225 ms |
5532 KB |
Output is correct |
90 |
Correct |
116 ms |
4188 KB |
Output is correct |