Submission #1112563

#TimeUsernameProblemLanguageResultExecution timeMemory
1112563vjudge1Chessboard (IZhO18_chessboard)C++17
47 / 100
2076 ms27992 KiB
//Dost SEFEROĞLU
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " << 
#define vi vector<int>
#define all(xx) xx.begin(),xx.end()
#define ps(xxx) cout << (xxx) << endl;
const int N = 5e2+1,inf = 1e16,MOD = 1e9+7; 
 

struct ST {
    vector<pii> t;
    vi lazy;
    void build(int node,int l,int r) {
        if (l == r) {
            t[node] = {0,1};
            return;
        }
        int m = (l+r) >> 1;
        build(2*node,l,m),build(2*node+1,m+1,r);
        t[node].ff = 0;
        t[node].ss = t[2*node].ss+t[2*node+1].ss;
    }
    ST(int nn) {
        t.resize(4*nn+1);
        build(1,1,nn);
        lazy.assign(4*nn+1,0);
    }
    void push(int node,bool leaf) {
        if (!lazy[node]) return;
        t[node].ff = t[node].ss-t[node].ff;
        if (!leaf) {
            lazy[2*node]^=1;
            lazy[2*node+1]^=1;
        }
        lazy[node] = 0;
    }

    void update(int node,int l,int r,int L,int R) {
        push(node,l==r);
        if (l > R || r < L) return;
        if (l >= L && r <= R) {
            lazy[node]^=1;
            push(node,l==r);
            return;
        }
        int m = (l+r) >> 1;
        update(2*node,l,m,L,R),update(2*node+1,m+1,r,L,R);
        t[node].ff = t[2*node].ff+t[2*node+1].ff;
    }

    int query(int node,int l,int r,int L,int R) {
        push(node,l==r);
        if (l > R || r < L) return 0;
        if (l >= L && r <= R) return t[node].ff;
        int m = (l+r) >> 1;
        return query(2*node,l,m,L,R)+query(2*node+1,m+1,r,L,R); 
    }
    void clear(int nn) {
        lazy.assign(4*nn+1,0);
        for (auto& it : t) it.ff = 0;
    }
};

void solve() { 
    int n,k;
    cin >> n >> k;
    vector<pii> upds[n+1];
    for (int i=1;i<=k;i++) {
        int x1,y1,x2,y2;
        cin >> x1 >> y1 >> x2 >> y2;
        upds[x1].push_back({y1,y2});
        if (x2 < n) upds[x2+1].push_back({y1,y2});
    }
    vi divs;
    for (int i=1;i<n;i++) if (n%i == 0) divs.push_back(i);
    int ans = inf;
    ST st1(n),st2(n);
    for (int d : divs) {
        st1.clear(n);
        st2.clear(n);
        int cur = 1;
        while (cur <= n) {
            if ((cur/d)%2) st2.update(1,1,n,cur,cur+d-1);
            else st1.update(1,1,n,cur,cur+d-1);
            cur+=d;
        }
        int need = 0;
        for (int j = 1;j<=n;j++) {
            for (auto it : upds[j]) {
                st1.update(1,1,n,it.ff,it.ss);
                st2.update(1,1,n,it.ff,it.ss);
            }
            if (((j-1)/d)%2 == 0) need+=st1.query(1,1,n,1,n);
            else need+=st2.query(1,1,n,1,n);
        }
        ans = min(ans,need);
        ans = min(ans,n*n-need);
    }
    cout << ans << endl;
}  
 
                
                            
signed main() { 
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef Dodi
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int t = 1;
    //cin >> t; 
    while (t --> 0) 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...