Submission #113542

# Submission time Handle Problem Language Result Execution time Memory
113542 2019-05-26T07:26:36 Z FutymyClone Chessboard (IZhO18_chessboard) C++14
100 / 100
389 ms 4464 KB
#include <bits/stdc++.h>

using namespace std;

struct Rect {
    int x, y, x2, y2;
    Rect (int x = 0, int y = 0, int x2 = 0, int y2 = 0): x(x), y(y), x2(x2), y2(y2) {}
};

int n, k;
vector <Rect> vec;
long long ans = 1e18;

int round_up (int n, int r) {
    return n % r == 0 ? n + 1 : n + r - n % r + 1;
}

int round_down (int n, int r) {
    return n % r == 0 ? n - r : n - n % r;
}

bool is_black (int x, int y, int len) {
    int cellx = (x % len == 0 ? x / len : x / len + 1) % 2;
    int celly = (y % len == 0 ? y / len : y / len + 1) % 2;
    return cellx == celly;
}

int getcellx (int x, int len) {
    return x % len == 0 ? x / len : x / len + 1;
}

int getcelly (int y, int len) {
    return y % len == 0 ? y / len : y / len + 1;
}

long long solve (int len) {
    int nsquare = n / len; //Number of square in each row and each column
    long long res = (1LL * nsquare * nsquare + 1) / 2 * len * len; //initialize result as number of needed black squares
    //if (len == 2) cout << res << "\n";
    for (auto rect: vec) {
        int x = rect.x, y = rect.y, x2 = rect.x2, y2 = rect.y2;
        int upx = round_up(x, len), upy = round_up(y, len), downx = round_down(x2, len), downy = round_down(y2, len);
        //if (len == 2) cout << upx << " " << upy << " " << downx << " " << downy << "\n";
        if (downx >= upx && downy >= upy) {
            long long sq = 1LL * (downx - upx + 1) * (downy - upy + 1) / len / len;
            long long nblack = is_black(x, y, len) ? (sq + 1) / 2 : sq / 2;
            long long nwhite = sq - nblack;
            res -= nblack * len * len, res += nwhite * len * len;
        }
        //if (len == 2) cout << res << "\n";
        if (downx >= upx) {
            long long sq = 1LL * (downx - upx + 1) / len;
            long long nblack = is_black(x, y, len) ? sq / 2 : (sq + 1) / 2;
            long long nwhite = sq - nblack;
            res -= nblack * len * (min(upy - 1, y2) - y + 1);
            res += nwhite * len * (min(upy - 1, y2) - y + 1);
            if (getcelly(y, len) != getcelly(y2, len)) {
                nblack = is_black(x, y2, len) ? sq / 2 : (sq + 1) / 2;
                nwhite = sq - nblack;
                res -= nblack * len * (y2 - max(y, downy + 1) + 1);
                res += nwhite * len * (y2 - max(y, downy + 1) + 1);
            }
        }
        //if (len == 2) cout << res << "\n";
        if (downy >= upy) {
            long long sq = 1LL * (downy - upy + 1) / len;
            long long nblack = is_black(x, y, len) ? sq / 2 : (sq + 1) / 2;
            long long nwhite = sq - nblack;
            res -= nblack * len * (min(upx - 1, x2) - x + 1);
            res += nwhite * len * (min(upx - 1, x2) - x + 1);
            if (getcellx(x, len) != getcellx(x2, len)) {
                nblack = is_black(x2, y, len) ? sq / 2 : (sq + 1) / 2;
                nwhite = sq - nblack;
                res -= nblack * len * (x2 - max(x, downx + 1) + 1);
                res += nwhite * len * (x2 - max(x, downx + 1) + 1);
            }
        }

        if (getcellx(x, len) == getcellx(x2, len) && getcelly(y, len) == getcelly(y2, len)) {
            if (is_black(x, y, len)) res -= 1LL * (x2 - x + 1) * (y2 - y + 1);
            else res += 1LL * (x2 - x + 1) * (y2 - y + 1);
        }
        else if (getcellx(x, len) == getcellx(x2, len)) {
            if (is_black(x, y, len)) res -= 1LL * (x2 - x + 1) * (upy - y);
            else res += 1LL * (x2 - x + 1) * (upy - y);

            if (is_black(x, y2, len)) res -= 1LL * (x2 - x + 1) * (y2 - downy);
            else res += 1LL * (x2 - x + 1) * (y2 - downy);
        }
        else if (getcelly(y, len) == getcelly(y2, len)) {
            if (is_black(x, y, len)) res -= 1LL * (upx - x) * (y2 - y + 1);
            else res += 1LL * (upx - x) * (y2 - y + 1);

            if (is_black(x2, y, len)) res -= 1LL * (x2 - downx) * (y2 - y + 1);
            else res += 1LL * (x2 - downx) * (y2 - y + 1);
        }
        else {
            if (is_black(x, y, len)) res -= 1LL * (upx - x) * (upy - y);
            else res += 1LL * (upx - x) * (upy - y);

            if (is_black(x2, y, len)) res -= 1LL * (x2 - downx) * (upy - y);
            else res += 1LL * (x2 - downx) * (upy - y);

            if (is_black(x, y2, len)) res -= 1LL * (upx - x) * (y2 - downy);
            else res += 1LL * (upx - x) * (y2 - downy);

            if (is_black(x2, y2, len)) res -= 1LL * (x2 - downx) * (y2 - downy);
            else res += 1LL * (x2 - downx) * (y2 - downy);
        }
    }

    return res;
}

long long calc() {
    long long res = (1LL * n * n + 1) / 2;
    for (auto rect: vec) {
        int x = rect.x, y = rect.y, x2 = rect.x2, y2 = rect.y2;
        long long sq = 1LL * (x2 - x + 1) * (y2 - y + 1);
        long long nblack = is_black(x, y, 1) ? (sq + 1) / 2 : sq / 2;
        long long nwhite = sq - nblack;
        res -= nblack; res += nwhite;
    }

    return res;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= k; i++) {
        int x, y, x2, y2;
        cin >> x >> y >> x2 >> y2;
        vec.push_back(Rect(x, y, x2, y2));
    }

    long long res = calc();
    ans = min(ans, min(res, 1LL * n * n - res));
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            long long temp = solve(i);
            ans = min(ans, min(temp, 1LL * n * n - temp));
            temp = solve(n / i);
            ans = min(ans, min(temp, 1LL * n * n - temp));
        }
    }

    cout << ans;
    return 0;
}
/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1532 KB Output is correct
2 Correct 8 ms 1024 KB Output is correct
3 Correct 17 ms 1660 KB Output is correct
4 Correct 17 ms 1532 KB Output is correct
5 Correct 21 ms 1532 KB Output is correct
6 Correct 15 ms 1532 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 16 ms 1532 KB Output is correct
9 Correct 34 ms 2552 KB Output is correct
10 Correct 20 ms 1532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 12 ms 1024 KB Output is correct
17 Correct 30 ms 2524 KB Output is correct
18 Correct 37 ms 2552 KB Output is correct
19 Correct 82 ms 2552 KB Output is correct
20 Correct 94 ms 2572 KB Output is correct
21 Correct 27 ms 2540 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 22 ms 1532 KB Output is correct
24 Correct 38 ms 2532 KB Output is correct
25 Correct 6 ms 768 KB Output is correct
26 Correct 22 ms 1532 KB Output is correct
27 Correct 30 ms 2544 KB Output is correct
28 Correct 36 ms 2516 KB Output is correct
29 Correct 13 ms 1532 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1532 KB Output is correct
2 Correct 8 ms 1024 KB Output is correct
3 Correct 17 ms 1660 KB Output is correct
4 Correct 17 ms 1532 KB Output is correct
5 Correct 21 ms 1532 KB Output is correct
6 Correct 15 ms 1532 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 16 ms 1532 KB Output is correct
9 Correct 34 ms 2552 KB Output is correct
10 Correct 20 ms 1532 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 12 ms 1024 KB Output is correct
27 Correct 30 ms 2524 KB Output is correct
28 Correct 37 ms 2552 KB Output is correct
29 Correct 82 ms 2552 KB Output is correct
30 Correct 94 ms 2572 KB Output is correct
31 Correct 27 ms 2540 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
33 Correct 22 ms 1532 KB Output is correct
34 Correct 38 ms 2532 KB Output is correct
35 Correct 6 ms 768 KB Output is correct
36 Correct 22 ms 1532 KB Output is correct
37 Correct 30 ms 2544 KB Output is correct
38 Correct 36 ms 2516 KB Output is correct
39 Correct 13 ms 1532 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 71 ms 2552 KB Output is correct
42 Correct 39 ms 2552 KB Output is correct
43 Correct 49 ms 2572 KB Output is correct
44 Correct 38 ms 2544 KB Output is correct
45 Correct 40 ms 2516 KB Output is correct
46 Correct 81 ms 2544 KB Output is correct
47 Correct 37 ms 2728 KB Output is correct
48 Correct 45 ms 2484 KB Output is correct
49 Correct 35 ms 2544 KB Output is correct
50 Correct 273 ms 2544 KB Output is correct
51 Correct 290 ms 2544 KB Output is correct
52 Correct 272 ms 2548 KB Output is correct
53 Correct 288 ms 2524 KB Output is correct
54 Correct 266 ms 2544 KB Output is correct
55 Correct 297 ms 2552 KB Output is correct
56 Correct 258 ms 2544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 25 ms 1532 KB Output is correct
10 Correct 8 ms 1024 KB Output is correct
11 Correct 17 ms 1660 KB Output is correct
12 Correct 17 ms 1532 KB Output is correct
13 Correct 21 ms 1532 KB Output is correct
14 Correct 15 ms 1532 KB Output is correct
15 Correct 5 ms 640 KB Output is correct
16 Correct 16 ms 1532 KB Output is correct
17 Correct 34 ms 2552 KB Output is correct
18 Correct 20 ms 1532 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 12 ms 1024 KB Output is correct
35 Correct 30 ms 2524 KB Output is correct
36 Correct 37 ms 2552 KB Output is correct
37 Correct 82 ms 2552 KB Output is correct
38 Correct 94 ms 2572 KB Output is correct
39 Correct 27 ms 2540 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 22 ms 1532 KB Output is correct
42 Correct 38 ms 2532 KB Output is correct
43 Correct 6 ms 768 KB Output is correct
44 Correct 22 ms 1532 KB Output is correct
45 Correct 30 ms 2544 KB Output is correct
46 Correct 36 ms 2516 KB Output is correct
47 Correct 13 ms 1532 KB Output is correct
48 Correct 3 ms 384 KB Output is correct
49 Correct 71 ms 2552 KB Output is correct
50 Correct 39 ms 2552 KB Output is correct
51 Correct 49 ms 2572 KB Output is correct
52 Correct 38 ms 2544 KB Output is correct
53 Correct 40 ms 2516 KB Output is correct
54 Correct 81 ms 2544 KB Output is correct
55 Correct 37 ms 2728 KB Output is correct
56 Correct 45 ms 2484 KB Output is correct
57 Correct 35 ms 2544 KB Output is correct
58 Correct 273 ms 2544 KB Output is correct
59 Correct 290 ms 2544 KB Output is correct
60 Correct 272 ms 2548 KB Output is correct
61 Correct 288 ms 2524 KB Output is correct
62 Correct 266 ms 2544 KB Output is correct
63 Correct 297 ms 2552 KB Output is correct
64 Correct 258 ms 2544 KB Output is correct
65 Correct 2 ms 384 KB Output is correct
66 Correct 2 ms 384 KB Output is correct
67 Correct 370 ms 2544 KB Output is correct
68 Correct 376 ms 4336 KB Output is correct
69 Correct 307 ms 4336 KB Output is correct
70 Correct 323 ms 4336 KB Output is correct
71 Correct 351 ms 4376 KB Output is correct
72 Correct 322 ms 4344 KB Output is correct
73 Correct 333 ms 4308 KB Output is correct
74 Correct 358 ms 4336 KB Output is correct
75 Correct 358 ms 4336 KB Output is correct
76 Correct 352 ms 4336 KB Output is correct
77 Correct 78 ms 4456 KB Output is correct
78 Correct 39 ms 4216 KB Output is correct
79 Correct 62 ms 4336 KB Output is correct
80 Correct 62 ms 4336 KB Output is correct
81 Correct 58 ms 4336 KB Output is correct
82 Correct 60 ms 4336 KB Output is correct
83 Correct 52 ms 4344 KB Output is correct
84 Correct 239 ms 4464 KB Output is correct
85 Correct 389 ms 4328 KB Output is correct
86 Correct 3 ms 384 KB Output is correct
87 Correct 2 ms 384 KB Output is correct
88 Correct 365 ms 4456 KB Output is correct
89 Correct 75 ms 1536 KB Output is correct
90 Correct 2 ms 384 KB Output is correct