Submission #523575

#TimeUsernameProblemLanguageResultExecution timeMemory
523575RaresFelixChessboard (IZhO18_chessboard)C++17
70 / 100
952 ms4980 KiB
#include <iostream> #include <climits> #include <tuple> #include <vector> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #define MN 167171 using namespace std; using ll = long long; ll n, k; tuple<int, int, int, int> R[MN]; ll P[MN], D[MN]; ll P2[MN], D2[MN]; static inline ll calc(ll a, ll b, ll l) { if(P[a] == 0 && P[b] == 0) return ((D[a] * D[b] + 1) >> 1) * l * l; if(P[a]) { ll nrseg; if((D[a] & 1) == 0) { if(P2[b] <= l) nrseg = D2[b] * l + P2[b]; else nrseg = (D2[b] + 1) * l; } else { if(P2[b] <= l) nrseg = D2[b] * l; else nrseg = (D2[b] - 1) * l + P2[b]; } return calc(a - P[a], b, l) + P[a] * nrseg; } return calc(b, a, l); } ll sol(ll l) { for(int i = 1; i <= n; ++i) { D[i] = D[i-1]; P[i] = P[i-1] + 1; if(P[i] == l) ++D[i], P[i] = 0; D2[i] = D2[i-1]; P2[i] = P2[i-1] + 1; if(P2[i] == (l << 1)) ++D2[i], P2[i] = 0; } ll arie = ((n / l) * (n / l) + 1) / 2 * l * l, selec; for(int i = 1; i <= k; ++i) { auto &[a, b, c, d] = R[i]; selec = calc(c, d, l) + calc(a-1, b-1, l) - calc(c, b-1, l) - calc(a-1, d, l); arie += (c - a + 1) * (d - b + 1) - 2 * selec; } return min(arie, 1ll * n * n - arie); } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> n >> k; for(int i = 1, a, b, c, d; i <= k; ++i) cin >> a >> b >> c >> d, R[i] = make_tuple(a, b, c, d); ll re = LLONG_MAX; for(int i = 1; i < n; ++i) if(n % i == 0) re = min(re, sol(i)); cout << re << "\n"; return 0; }
#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...