제출 #1154910

#제출 시각아이디문제언어결과실행 시간메모리
1154910arkanefuryChessboard (IZhO18_chessboard)C++20
70 / 100
120 ms528 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define in insert
#define lb lower_bound
#define F first
#define S second
#define sz size()
#define int long long
#define all(v) v.begin(),v.end()
#define FOR1(x, n) for(int j = x; j <= n; j ++)
#define FOR(x, n, m, d) for(int x = n; x <= m; x += d)
#define FORR(x, n, m, d) for(int x = n; x >= m; x -= d)
#define nikita ios_base::sync_with_stdio(0), cin.tie(0);
const int N =  1e6+5;
int b[N], a[N];
int n,m,k,sum=0,x,y, ans, r,pref[N], cnt, l, mod = 1e9+7, cal,lp = 1;
void solve(){
    cin >> n >> m;
    vector<int>v;
    k = n/2;
    FOR(i, 1, k, 1){
        if(n%i==0)v.pb(i);
    }
    FOR(i, 0, v.sz-1, 1){
        k = n / v[i];
        if(k%2==0)b[i] = (k/2) * v[i] * v[i] * k;
        else{
            b[i] = ((k+1)/2) * v[i] * v[i] * ((k+1)/2);
            b[i] += (k/2) * v[i]*v[i] * (k/2);
        }
    }
    FOR(i, 1, m, 1){
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        x1--,y1--, x2--, y2--;
        FOR(j, 0, v.sz-1, 1){
            bool f = 0;
        int ini = b[j];
            x = x1 / v[j], y = y1 / v[j];
            
            if((x+1)*v[j] <= x2)
            {
            k = (x+1)*v[j] - x1;
            if( x % 2 == y % 2 ) b[j] -= k;
            else b[j] += k, f = 1;
            sum = x2 - x1 + 1 - k;
            if( ( sum / v[j] ) % 2 ){
                if(f)b[j] -= k;
                else b[j] += k;
                f ^= 1;
            }
            sum %= v[j];
            if(!f)b[j] += sum;
            else b[j] -= sum;
            }
            else{
                b[j] -= (x2-x1+1) * (x%2==y%2 ? 1 : -1);
            }
            int lp = (ini - b[j]) * -1;
            b[j] = ini;
            f = 0;
            if((y+1)*v[j] <= y2 ){
            sum = (y+1)*v[j] - y1;
            b[j] += sum * lp;
            int obshi = y2 - y1 + 1 - sum;
            if( (obshi / v[j]) % 2 )b[j] -= v[j] * lp, f = 1;
            obshi %= v[j];
            b[j] -= obshi * lp * (f != 1 ? 1 : -1);
            }
            else{
                b[j] += (y2-y1+1) * lp;
            }
        }
    }
    ans = 1e18;
    FOR(j, 0, v.sz-1, 1){
        if(b[j])ans = min({ans, b[j], n*n-b[j]});
    }
    cout << ans;
}
signed main(){
    nikita
    int tt = 1;
    if(!tt)cin >> tt;
    FOR(i, 1, tt, 1)solve();
}
#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...