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>
#define ll long long
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const int N = 1e5 + 2;
int n, sum[100002], k, lazy[400002], aint[400002];
struct query {
int type, l, r;
bool operator < (const query &other) {
return type < other.type;
}
};
vector<query>q[100002];
void build (int nod, int st, int dr) {
aint[nod] = sum[dr] - (st > 0 ? sum[st - 1] : 0);
if (st == dr)
return;
int mij = (st + dr) / 2;
build(2 * nod, st, mij);
build(2 * nod + 1, mij + 1, dr);
}
void push (int nod, int st, int dr) {
if (lazy[nod] != -1) {
if (st != dr) {
lazy[2 * nod] = lazy[nod];
lazy[2 * nod + 1] = lazy[nod];
}
if (lazy[nod] == 0)
aint[nod] = sum[dr] - (st > 0 ? sum[st - 1] : 0);
else
aint[nod] = (dr - st + 1) - (sum[dr] - (st > 0 ? sum[st - 1] : 0));
}
lazy[nod] = -1;
}
void update (int nod, int st, int dr, int a, int b, int val) {
push(nod, st, dr);
if (a <= st && dr <= b) {
if (val == 0)
aint[nod] = sum[dr] - (st > 0 ? sum[st - 1] : 0);
else
aint[nod] = (dr - st + 1) - (sum[dr] - (st > 0 ? sum[st - 1] : 0));
if (st != dr) {
lazy[2 * nod] = val;
lazy[2 * nod + 1] = val;
}
return;
}
int mij = (st + dr) / 2;
if (a <= mij)
update(2 * nod, st, mij, a, b, val);
else
update(2 * nod + 1, mij + 1, dr, a, b, val);
aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k;
while (k--) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--; y1--; x2--; y2--;
q[x1].push_back({1, y1, y2});
q[x2 + 1].push_back({0, y1, y2});
}
for (int i = 0; i < n; i++)
sort(q[i].begin(), q[i].end());
ll sol = n * n;
for (int l = 1; l < n; l++) {
if (n % l == 0) {
for (int i = 0; i < n; i++) {
sum[i] = (i / l) % 2 + (i > 0 ? sum[i - 1] : 0);
}
ll nr = 0;
for (int i = 1; i <= 4 * n; i++) {
lazy[i] = -1;
}
build(1, 0, n - 1);
for (int i = 0; i < n; i++) {
for (auto it : q[i]) {
if (it.type == 0)
update(1, 0, n - 1, it.l, it.r, 0);
else
update(1, 0, n - 1, it.l, it.r, 1);
}
if ((i / l) % 2)
nr += n - aint[1];
else
nr += aint[1];
}
sol = min(sol, min(nr, 1l * n *n - nr));
}
}
cout << sol;
return 0;
}
# | 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... |