#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const ll inf = (ll)1e15 + 5;
const int mxN = 100005;
int n, k;
vector<pii> in[mxN];
vector<pii> out[mxN];
ll resi(int d){
if(d == n)return inf;
ll ans1 = 0, ans2 = 0; int cnt[2] = {0, 0};
ff(i,0,n - 1){
for(auto c : in[i]){
int l = c.fi;
int r = c.se;
if(l / d == r / d){
int m = l / d;
cnt[m % 2] += r - l + 1;
continue;
}
if(l % d != 0){
int m = l / d;
cnt[m % 2] += (m + 1) * d - l;
l = (m + 1) * d;
}
if(r % d != d - 1){
int m = r / d;
cnt[m % 2] += r - m * d + 1;
r = m * d - 1;
}
if(l <= r){
int mL = l / d;
int mR = r / d;
int len = mR - mL + 1;
if(mL % 2 == 0){
cnt[0] += ((len + 1) / 2) * d;
cnt[1] += (len / 2) * d;
}
else{
cnt[0] += (len / 2) * d;
cnt[1] += ((len + 1) / 2) * d;
}
}
}
int m = i / d;
if(m % 2 == 0){
ans1 += cnt[0] + ((n / d) / 2) * d - cnt[1];
ans2 += ((n / d + 1) / 2) * d - cnt[0] + cnt[1];
}
else{
ans1 += ((n / d + 1) / 2) * d - cnt[0] + cnt[1];
ans2 += cnt[0] + ((n / d) / 2) * d - cnt[1];
}
for(auto c : out[i]){
int l = c.fi;
int r = c.se;
if(l / d == r / d){
int m = l / d;
cnt[m % 2] -= r - l + 1;
continue;
}
if(l % d != 0){
int m = l / d;
cnt[m % 2] -= (m + 1) * d - l;
l = (m + 1) * d;
}
if(r % d != d - 1){
int m = r / d;
cnt[m % 2] -= r - m * d + 1;
r = m * d - 1;
}
if(l <= r){
int mL = l / d;
int mR = r / d;
int len = mR - mL + 1;
if(mL % 2 == 0){
cnt[0] -= ((len + 1) / 2) * d;
cnt[1] -= (len / 2) * d;
}
else{
cnt[0] -= (len / 2) * d;
cnt[1] -= ((len + 1) / 2) * d;
}
}
}
}
return min(ans1, ans2);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
ff(i,1,k){
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2; x1--,y1--,x2--,y2--;
in[x1].pb({y1, y2});
out[x2].pb({y1, y2});
}
ll rez = inf;
for(int i = 1; i * i <= n; i++){
if(n % i == 0){
rez = min(rez, resi(i));
if(n / i != i)rez = min(rez, resi(n / i));
}
}
cout << rez << '\n';
return 0;
}
/*
2 0
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
4 1
4 1 4 4
// probati bojenje sahovski
*/
# | 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... |