#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);
}
ll ans = 1e18;
for (int p: del) {
vector<int> b, w;
for (int i = 1; i <= n; i++) {
if ((i - 1) / p % 2) w.emplace_back(i);
else b.emplace_back(i);
}
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]) {
bib += upper_bound(b.begin(), b.end(), r) - lower_bound(b.begin(), b.end(), l);
biw += upper_bound(w.begin(), w.end(), r) - lower_bound(w.begin(), w.end(), l);
}
int wib = (int) b.size() - bib;
int wiw = (int) 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]) {
bib -= upper_bound(b.begin(), b.end(), r) - lower_bound(b.begin(), b.end(), l);
biw -= upper_bound(w.begin(), w.end(), r) - lower_bound(w.begin(), w.end(), l);
}
}
// 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
108 ms |
8020 KB |
Output is correct |
2 |
Correct |
23 ms |
2140 KB |
Output is correct |
3 |
Correct |
67 ms |
7856 KB |
Output is correct |
4 |
Correct |
54 ms |
2652 KB |
Output is correct |
5 |
Correct |
87 ms |
7052 KB |
Output is correct |
6 |
Correct |
56 ms |
5468 KB |
Output is correct |
7 |
Correct |
12 ms |
2136 KB |
Output is correct |
8 |
Correct |
56 ms |
5532 KB |
Output is correct |
9 |
Correct |
133 ms |
7004 KB |
Output is correct |
10 |
Correct |
77 ms |
5448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
504 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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
60 ms |
1640 KB |
Output is correct |
17 |
Correct |
82 ms |
4072 KB |
Output is correct |
18 |
Correct |
213 ms |
4440 KB |
Output is correct |
19 |
Correct |
1292 ms |
4064 KB |
Output is correct |
20 |
Correct |
1433 ms |
4436 KB |
Output is correct |
21 |
Correct |
75 ms |
4008 KB |
Output is correct |
22 |
Correct |
5 ms |
348 KB |
Output is correct |
23 |
Correct |
189 ms |
2040 KB |
Output is correct |
24 |
Correct |
187 ms |
4188 KB |
Output is correct |
25 |
Correct |
37 ms |
860 KB |
Output is correct |
26 |
Correct |
117 ms |
2652 KB |
Output is correct |
27 |
Correct |
213 ms |
3476 KB |
Output is correct |
28 |
Correct |
192 ms |
4192 KB |
Output is correct |
29 |
Correct |
31 ms |
1884 KB |
Output is correct |
30 |
Correct |
6 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
8020 KB |
Output is correct |
2 |
Correct |
23 ms |
2140 KB |
Output is correct |
3 |
Correct |
67 ms |
7856 KB |
Output is correct |
4 |
Correct |
54 ms |
2652 KB |
Output is correct |
5 |
Correct |
87 ms |
7052 KB |
Output is correct |
6 |
Correct |
56 ms |
5468 KB |
Output is correct |
7 |
Correct |
12 ms |
2136 KB |
Output is correct |
8 |
Correct |
56 ms |
5532 KB |
Output is correct |
9 |
Correct |
133 ms |
7004 KB |
Output is correct |
10 |
Correct |
77 ms |
5448 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
60 ms |
1640 KB |
Output is correct |
27 |
Correct |
82 ms |
4072 KB |
Output is correct |
28 |
Correct |
213 ms |
4440 KB |
Output is correct |
29 |
Correct |
1292 ms |
4064 KB |
Output is correct |
30 |
Correct |
1433 ms |
4436 KB |
Output is correct |
31 |
Correct |
75 ms |
4008 KB |
Output is correct |
32 |
Correct |
5 ms |
348 KB |
Output is correct |
33 |
Correct |
189 ms |
2040 KB |
Output is correct |
34 |
Correct |
187 ms |
4188 KB |
Output is correct |
35 |
Correct |
37 ms |
860 KB |
Output is correct |
36 |
Correct |
117 ms |
2652 KB |
Output is correct |
37 |
Correct |
213 ms |
3476 KB |
Output is correct |
38 |
Correct |
192 ms |
4192 KB |
Output is correct |
39 |
Correct |
31 ms |
1884 KB |
Output is correct |
40 |
Correct |
6 ms |
604 KB |
Output is correct |
41 |
Execution timed out |
2045 ms |
11856 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
108 ms |
8020 KB |
Output is correct |
10 |
Correct |
23 ms |
2140 KB |
Output is correct |
11 |
Correct |
67 ms |
7856 KB |
Output is correct |
12 |
Correct |
54 ms |
2652 KB |
Output is correct |
13 |
Correct |
87 ms |
7052 KB |
Output is correct |
14 |
Correct |
56 ms |
5468 KB |
Output is correct |
15 |
Correct |
12 ms |
2136 KB |
Output is correct |
16 |
Correct |
56 ms |
5532 KB |
Output is correct |
17 |
Correct |
133 ms |
7004 KB |
Output is correct |
18 |
Correct |
77 ms |
5448 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
456 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
504 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
504 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
60 ms |
1640 KB |
Output is correct |
35 |
Correct |
82 ms |
4072 KB |
Output is correct |
36 |
Correct |
213 ms |
4440 KB |
Output is correct |
37 |
Correct |
1292 ms |
4064 KB |
Output is correct |
38 |
Correct |
1433 ms |
4436 KB |
Output is correct |
39 |
Correct |
75 ms |
4008 KB |
Output is correct |
40 |
Correct |
5 ms |
348 KB |
Output is correct |
41 |
Correct |
189 ms |
2040 KB |
Output is correct |
42 |
Correct |
187 ms |
4188 KB |
Output is correct |
43 |
Correct |
37 ms |
860 KB |
Output is correct |
44 |
Correct |
117 ms |
2652 KB |
Output is correct |
45 |
Correct |
213 ms |
3476 KB |
Output is correct |
46 |
Correct |
192 ms |
4192 KB |
Output is correct |
47 |
Correct |
31 ms |
1884 KB |
Output is correct |
48 |
Correct |
6 ms |
604 KB |
Output is correct |
49 |
Execution timed out |
2045 ms |
11856 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |