#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn=1e5+5;
int n,k;
int xl[maxn],yl[maxn];
int xr[maxn],yr[maxn];
vector<int>v[maxn],p[maxn];
ll f(int X,int tp){
ll now = 0;
int nw = tp ^ 1;
for(int i = 1;i <= n;i++){
if(X == 1 or i % X == 1) nw ^= 1;
if(!(tp ^ nw)) now += 1ll * (n/X + 1)/2 * X;
else now += 1ll * (n/X) / 2 * X;
}
// if(X == 2 and tp == 1) cout<<now<<'\n';
nw = tp ^ 1;
ll cnt[2]; cnt[0] = cnt[1] = 0;
for(int i = 1;i <= n;i++){
if(i % X == 1 or X == 1){
nw ^= 1;
swap(cnt[0],cnt[1]);
}
for(auto j : v[i]){
int z = xl[j] + (X + 1 - (xl[j] % X));
if(xr[j] < z){
int cln = (xl[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] += (xr[j] - xl[j] + 1);
continue;
}
if(xr[j] < z + X - 1){
int cln = (xl[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] += (z - xl[j]);
cln = (xr[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] += (xr[j] - z + 1);
continue;
}
int pos = xr[j] - (xr[j] % X) + 1;
int cln = (z / X) % 2;
cln = nw ^ cln;
cnt[cln] += ((pos - z) / X + 1)/2 * X;
cnt[cln ^ 1] += ((pos-z)/X)/2 * X;
cln = (xl[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] += (z - xl[j]);
cln = (xr[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] += (xr[j] - pos + 1);
}
now += cnt[0];
now -= cnt[1];
// if(X == 2 and tp == 1) cout<<i<<' '<<cnt[0]<<' '<<cnt[1]<<'\n';
for(auto j : p[i]){
int z = xl[j] + (X + 1 - (xl[j] % X));
if(xr[j] < z){
int cln = (xl[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] -= (xr[j] - xl[j] + 1);
continue;
}
if(xr[j] < z + X - 1){
int cln = (xl[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] -= (z - xl[j]);
cln = (xr[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] -= (xr[j] - z + 1);
continue;
}
int pos = xr[j] - (xr[j] % X) + 1;
int cln = (z / X) % 2;
cln = nw ^ cln;
cnt[cln] -= ((pos - z) / X + 1)/2 * X;
cnt[cln ^ 1] -= ((pos-z)/X)/2 * X;
cln = (xl[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] -= (z - xl[j]);
cln = (xr[j] / X) % 2;
cln = nw ^ cln;
cnt[cln] -= (xr[j] - pos + 1);
}
}
// cout<<X<<' '<<tp<<' '<<now<<'\n';
return now;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1;i <= k;i++){
cin >> xl[i] >> yl[i] >> xr[i] >> yr[i];
v[yl[i]].push_back(i);
p[yr[i]].push_back(i);
}
ll ans = 1ll * n * n;
for(int i = 1;i < n;i++) if(n % i == 0){
ans=min(ans,f(i,0));
ans=min(ans,f(i,1));
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
8412 KB |
Output is correct |
2 |
Incorrect |
12 ms |
6212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
8412 KB |
Output is correct |
2 |
Incorrect |
12 ms |
6212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |