#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker,"/stack:20000000")
//#pragma GCC target("sse,sse2,sse3,sse3,ssse3,abm,avx,avx2,tune=native")
#define f first
#define s second
#define endl '\n'
#define vec vector
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define m_p make_pair
#define sz(x) (int)x.size()
#define pw(x) (1LL<<x)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll,int> pli;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
auto rng=bind(uniform_int_distribution<int>(0,1e9),mt19937(time(0)));
template<class T>bool umin(T &a,const T &b) {return (a>b?a=b,1:0);}
template<class T>bool umax(T &a,const T &b) {return (a<b?a=b,1:0);}
template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
//vec<int>tos={}
const int N=1e5+1;
int nxt(int x,int k){
x+=(k-x%k);
return x;
}
int prv(int x,int k){
x-=x%k;
x--;
return x;
}
int get(int x,int k){
return (x/k)&1;
}
pii get(int l,int r,int k){
int dl=r-l+1;
while(get(l,k)!=0) l=nxt(l,k);
while(get(r,k)!=0) r=prv(r,k);
int x=0;
if(l>r) x=0;
else if(l<=r){
if((l/k)==(r/k)){
x=r-l+1;
}
else{
x+=nxt(l,k)-l;
x+=r-prv(r,k);
l=nxt(l,k);r=prv(r,k);
assert(l%k==0);assert(r%k==k-1);
int how=(r-l+1)/k;
how/=2;
x+=how*k;
}
}
return {x,dl-x};
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,q;
cin>>n>>q;
ll ans=1ll*n*n;
vec<vec<array<int,3>>>scan(n+1,vec<array<int,3>>());
for(int i=0;i<q;i++){
int x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
--x1;--x2;--y1;--y2;
scan[y1].pb({x1,x2,1});
scan[y2+1].pb({x1,x2,-1});
}
vec<int>ch(n,0);
for(int x=1;x<n;x++){
if(n%x) continue;
//cerr<<"HERE "<<endl;
vec<int>cnt(2,0);
vec<int>cr(2,0);
for(int j=0;j<n;j++){
ch[j]=((j/x)&1);
cnt[ch[j]]++;
}
vec<ll>answ(2,0);
int k=x;
// cout<<"X "<<x<<endl;
for(int i=0;i<=n;i++){
if(i && ch[i]!=ch[i-1]) swap(cr[0],cr[1]);
for(auto &z : scan[i]){
int l=z[0],r=z[1];
if(ch[i]){
cr[1]+=get(l,r,k).f*z[2];
cr[0]+=get(l,r,k).s*z[2];
}
else{
cr[0]+=get(l,r,k).f*z[2];
cr[1]+=get(l,r,k).s*z[2];
}
}
if(i==n) continue;
int a=cr[0],b=cr[1];
if(ch[i]){
int x=a+cnt[1]-b;
answ[1]+=x;
x=n-x;
answ[0]+=x;
}
else{
int x=a+cnt[1]-b;
answ[0]+=x;
x=n-x;
answ[1]+=x;
}
// cout<<cr[0]<<' '<<cr[1]<<endl;
}
// cout<<x<<' '<<answ[0]<<' '<<answ[1]<<endl;
umin(ans,answ[0]);
umin(ans,answ[1]);
}
cout<<ans;
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
58 ms |
5980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
58 ms |
5980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Incorrect |
58 ms |
5980 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |