답안 #501205

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
501205 2022-01-02T15:48:15 Z dxz05 Chessboard (IZhO18_chessboard) C++14
16 / 100
34 ms 4152 KB
#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

#define MP make_pair

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 998244353;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e6 + 3e2;
const int M = 222;

#define rect pair<pair<ll,ll>,pair<ll,ll>>
#define x1 first.first
#define y1 first.second
#define x2 second.first
#define y2 second.second

void solve(int TC) {
    int n, k;
    cin >> n >> k;

    vector<rect> rectangles(k);
    for (auto &r : rectangles){
        cin >> r.x1 >> r.y1 >> r.x2 >> r.y2;
    }

    vector<ll> divisors;
    for (int d = 1; d < n; d++){
        if (n % d == 0) divisors.push_back(d);
    }

    ll ans = 1ll * n * n;

    for (ll d : divisors){ /// (1, 1) = black
        ll cost = ((n / d) * (n / d) + 1) / 2 * d * d;
        for (auto r : rectangles){
            ll i1 = (r.x1 + d - 2) / d + 1;
            ll j1 = (r.y1 + d - 2) / d + 1;
            ll i2 = r.x2 / d;
            ll j2 = r.y2 / d;
            ll id = i2 - i1 + 1, jd = j2 - j1 + 1;
            debug(i1, j1, i2, j2, id, jd);

            if (id < 0 || jd < 0){
                ll S = (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1);
                if ((i1 + j1) % 2 == 0) cost += S; else
                    cost -= S;
                continue;
            }

            ll white = 0;
            white += (id * jd / 2) * d * d;
            if ((id * jd) % 2 == 1){
                if ((i1 + j1) % 2 == 1) white += d * d;
            }

            ll h = (i1 - 1) * d - r.x1 + 1;
            h = max(h, 0ll);
            white += jd / 2 * h * d;
            if (jd % 2 == 1){
                if ((i1 + j1) % 2 == 0) white += h * d;
            }

            h = r.x2 - i2 * d;
            h = max(h, 0ll);
            white += jd / 2 * h * d;
            if (jd % 2 == 1){
                if ((i2 + j1) % 2 == 0) white += h * d;
            }

            ll w = (j1 - 1) * d - r.y1 + 1;
            w = max(w, 0ll);
            white += id / 2 * w * d;
            if (id % 2 == 1){
                if ((i1 + j1) % 2 == 0) white += w * d;
            }

            w = r.y2 - j2 * d;
            w = max(w, 0ll);
            white += id / 2 * w * d;
            if (id % 2 == 1){
                if ((i1 + j2) % 2 == 0) white += w * d;
            }

            h = (i1 - 1) * d - r.x1 + 1, w = (j1 - 1) * d - r.y1 + 1;
            h = max(h, 0ll);
            w = max(w, 0ll);
            if ((i1 + j1) % 2 == 1) white += w * h;

            w = r.y2 - j2 * d;
            w = max(w, 0ll);
            if ((i1 + j2) % 2 == 1) white += w * h;

            h = r.x2 - i2 * d;
            h = max(h, 0ll);
            if ((i2 + j2) % 2 == 1) white += w * h;

            h = r.x2 - i2 * d, w = (j1 - 1) * d - r.y1 + 1;
            h = max(h, 0ll);
            w = max(w, 0ll);
            if ((i2 + j1) % 2 == 1) white += w * h;

            ll black = (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1) - white;
            cost += white;
            cost -= black;

            debug(white, black);
        }

        ans = min(ans, cost);
        debug(d, cost);
    }

    for (ll d : divisors){ /// (1, 1) = white
        ll cost = (n / d) * (n / d) / 2 * d * d;
        for (auto r : rectangles){
            ll i1 = (r.x1 + d - 2) / d + 1;
            ll j1 = (r.y1 + d - 2) / d + 1;
            ll i2 = r.x2 / d;
            ll j2 = r.y2 / d;
            ll id = i2 - i1 + 1, jd = j2 - j1 + 1;
            debug(i1, j1, i2, j2, id, jd);

            if (id < 0 || jd < 0){
                ll S = (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1);
                if ((i1 + j1) % 2 == 0) cost += S; else
                    cost -= S;
                continue;
            }

            ll white = 0;
            white += (id * jd / 2) * d * d;
            if ((id * jd) % 2 == 1){
                if ((i1 + j1) % 2 == 0) white += d * d;
            }

            ll h = (i1 - 1) * d - r.x1 + 1;
            h = max(h, 0ll);
            white += jd / 2 * h * d;
            if (jd % 2 == 1){
                if ((i1 + j1) % 2 == 1) white += h * d;
            }

            h = r.x2 - i2 * d;
            h = max(h, 0ll);
            white += jd / 2 * h * d;
            if (jd % 2 == 1){
                if ((i2 + j1) % 2 == 1) white += h * d;
            }

            ll w = (j1 - 1) * d - r.y1 + 1;
            w = max(w, 0ll);
            white += id / 2 * w * d;
            if (id % 2 == 1){
                if ((i1 + j1) % 2 == 1) white += w * d;
            }

            w = r.y2 - j2 * d;
            w = max(w, 0ll);
            white += id / 2 * w * d;
            if (id % 2 == 1){
                if ((i1 + j2) % 2 == 1) white += w * d;
            }

            h = (i1 - 1) * d - r.x1 + 1, w = (j1 - 1) * d - r.y1 + 1;
            h = max(h, 0ll);
            w = max(w, 0ll);
            if ((i1 + j1) % 2 == 0) white += w * h;

            w = r.y2 - j2 * d;
            w = max(w, 0ll);
            if ((i1 + j2) % 2 == 0) white += w * h;

            h = r.x2 - i2 * d;
            h = max(h, 0ll);
            if ((i2 + j2) % 2 == 0) white += w * h;

            h = r.x2 - i2 * d, w = (j1 - 1) * d - r.y1 + 1;
            h = max(h, 0ll);
            w = max(w, 0ll);
            if ((i2 + j1) % 2 == 0) white += w * h;

            ll black = (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1) - white;
            cost += white;
            cost -= black;

            debug(white, black);
        }

        ans = min(ans, cost);
        debug(d, cost);
    }

    cout << ans;
}

int main() {
    startTime = clock();
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
//    cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;

    return 0;
}

/*

6 8
1 2 1 2
2 1 2 1
3 3 3 3
3 4 3 4
3 6 3 6
4 3 4 3
4 4 4 4
5 5 5 5

4 1
4 1 4 4

 */

Compilation message

chessboard.cpp: In function 'void solve(int)':
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
chessboard.cpp:73:13: note: in expansion of macro 'debug'
   73 |             debug(i1, j1, i2, j2, id, jd);
      |             ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
chessboard.cpp:138:13: note: in expansion of macro 'debug'
  138 |             debug(white, black);
      |             ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
chessboard.cpp:142:9: note: in expansion of macro 'debug'
  142 |         debug(d, cost);
      |         ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
chessboard.cpp:153:13: note: in expansion of macro 'debug'
  153 |             debug(i1, j1, i2, j2, id, jd);
      |             ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
chessboard.cpp:218:13: note: in expansion of macro 'debug'
  218 |             debug(white, black);
      |             ^~~~~
chessboard.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
chessboard.cpp:222:9: note: in expansion of macro 'debug'
  222 |         debug(d, cost);
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 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 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 3168 KB Output is correct
2 Correct 7 ms 1092 KB Output is correct
3 Correct 15 ms 2420 KB Output is correct
4 Correct 16 ms 2492 KB Output is correct
5 Correct 20 ms 3008 KB Output is correct
6 Correct 14 ms 2148 KB Output is correct
7 Correct 3 ms 712 KB Output is correct
8 Correct 13 ms 2176 KB Output is correct
9 Correct 34 ms 4152 KB Output is correct
10 Correct 18 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 3168 KB Output is correct
2 Correct 7 ms 1092 KB Output is correct
3 Correct 15 ms 2420 KB Output is correct
4 Correct 16 ms 2492 KB Output is correct
5 Correct 20 ms 3008 KB Output is correct
6 Correct 14 ms 2148 KB Output is correct
7 Correct 3 ms 712 KB Output is correct
8 Correct 13 ms 2176 KB Output is correct
9 Correct 34 ms 4152 KB Output is correct
10 Correct 18 ms 2892 KB Output is correct
11 Incorrect 1 ms 204 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 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 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 23 ms 3168 KB Output is correct
10 Correct 7 ms 1092 KB Output is correct
11 Correct 15 ms 2420 KB Output is correct
12 Correct 16 ms 2492 KB Output is correct
13 Correct 20 ms 3008 KB Output is correct
14 Correct 14 ms 2148 KB Output is correct
15 Correct 3 ms 712 KB Output is correct
16 Correct 13 ms 2176 KB Output is correct
17 Correct 34 ms 4152 KB Output is correct
18 Correct 18 ms 2892 KB Output is correct
19 Incorrect 1 ms 204 KB Output isn't correct
20 Halted 0 ms 0 KB -