This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |