Submission #497218

#TimeUsernameProblemLanguageResultExecution timeMemory
497218GurbanChessboard (IZhO18_chessboard)C++17
100 / 100
1985 ms12772 KiB
#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]; bool col(int pos,int nw,int X){ int now = pos / X; now %= 2; return now ^ nw; } ll f(int X,int tp){ ll now = 0; int nw = tp ^ 1; for(int i = 0;i < n;i++){ if(i % X == 0) nw ^= 1; if(nw) now += 1ll * (n / X + 1)/2 * X; else now += 1ll * (n/X) / 2 * X; } nw = tp ^ 1; ll cnt[2]; cnt[0] = cnt[1] = 0; for(int i = 0;i < n;i++){ if(i % X == 0){ nw ^= 1; swap(cnt[0],cnt[1]); } for(auto j : v[i]){ int z = xl[j] + (X - xl[j] % X); if(z > xr[j]){ int cl = col(xl[j],nw,X); cnt[cl] += (xr[j] - xl[j] + 1); continue; } if(z + X - 1 >= xr[j]){ int cl = col(xl[j],nw,X); cnt[cl] += (z - xl[j]); cl = col(z,nw,X); cnt[cl] += (xr[j] - z + 1); continue; } int pos = xr[j] - (xr[j] % X); pos--; int cl = col(xl[j],nw,X); cnt[cl] += (z - xl[j]); cl = col(xr[j],nw,X); cnt[cl] += (xr[j] - pos); cl = col(z,nw,X); cnt[cl] += ((pos-z+1)/X+1)/2*X; cnt[cl ^ 1] += ((pos-z+1)/X)/2*X; } // if(X == 2 and tp == 1) cout<<i<<' '<<cnt[0]<<' '<<cnt[1]<<'\n'; now += cnt[0]; now -= cnt[1]; for(auto j : p[i]){ int z = xl[j] + (X - xl[j] % X); if(z > xr[j]){ int cl = col(xl[j],nw,X); cnt[cl] -= (xr[j] - xl[j] + 1); continue; } if(z + X - 1 >= xr[j]){ int cl = col(xl[j],nw,X); cnt[cl] -= (z - xl[j]); cl = col(z,nw,X); cnt[cl] -= (xr[j] - z + 1); continue; } int pos = xr[j] - (xr[j] % X); pos--; int cl = col(xl[j],nw,X); cnt[cl] -= (z - xl[j]); cl = col(xr[j],nw,X); cnt[cl] -= (xr[j] - pos); cl = col(z,nw,X); cnt[cl] -= ((pos-z+1)/X+1)/2*X; cnt[cl ^ 1] -= ((pos-z+1)/X)/2*X; } } // 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]; 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...