This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
#define all(xx) xx.begin(),xx.end()
#define ps(xxx) cout << (xxx) << endl;
const int N = 5e2+1,inf = 1e16,MOD = 1e9+7;
void solve() {
int n,k;
cin >> n >> k;
vector<pii> adds[n+2],rems[n+2];
for (int i=1;i<=k;i++) {
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
adds[x1].push_back({y1,y2});
rems[x2+1].push_back({y1,y2});
}
vi divs;
for (int i=1;i<n;i++) if (n%i == 0) divs.push_back(i);
int ans = inf;
vi p(n+1,0);
for (int d : divs) {
p[0] = 0;
for (int i=1;i<=n;i++) {
int val = (((i-1)/d)%2 == 0);
p[i] = p[i-1]+val;
}
int sm = p[n];
int need = 0;
for (int j = 1;j<=n+1;j++) {
for (auto it : adds[j]) {
int old = p[it.ss]-p[it.ff-1];
sm-=old;
sm+=(it.ss-it.ff+1)-old;
}
for (auto it : rems[j]) {
sm-=(it.ss-it.ff+1)-(p[it.ss]-p[it.ff-1]);
sm+=p[it.ss]-p[it.ff-1];
}
if (j > n) break;
if (((j-1)/d)%2 == 0) need+=sm;
else need+=n-sm;
}
ans = min(ans,need);
ans = min(ans,n*n-need);
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | 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... |