Submission #609085

# Submission time Handle Problem Language Result Execution time Memory
609085 2022-07-27T12:07:52 Z pakhomovee Chessboard (IZhO18_chessboard) C++17
100 / 100
802 ms 5820 KB
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <set>
#include <numeric>
#include <queue>
#include <functional>

using namespace std;

#define int long long

struct rect {
    int x1, y1, x2, y2;
    rect() {}
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<rect> r(k);
    int S = 0;
    for (int i = 0; i < k; ++i) {
        cin >> r[i].x1 >> r[i].y1 >> r[i].x2 >> r[i].y2;
        --r[i].x1; --r[i].y1; --r[i].x2; --r[i].y2;
        S += (r[i].x2 - r[i].x1 + 1) * (r[i].y2 - r[i].y1 + 1);
    }
    auto whiteCount = [&] (rect r, int len, int parity) {
        if (r.x1 / len == r.x2 / len && r.y1 / len == r.y2 / len) {
            int par = (r.x1 / len + r.y1 / len) % 2;
            if (par == parity) return (r.x2 - r.x1 + 1) * (r.y2 - r.y1 + 1);
            return 0ll;
        } else if (r.x1 / len == r.x2 / len) {
            int firstFullColumn = (r.y1 + len - 1) / len;
            int lastFullColumn = r.y2 / len;
            int res = 0;
            if ((r.x1 / len + r.y1 / len) % 2 == parity) {
                res += (firstFullColumn * len - r.y1) * (r.x2 - r.x1 + 1);
            }
            if ((r.x1 / len + lastFullColumn) % 2 == parity) {
                res += (r.y2 - lastFullColumn * len + 1) * (r.x2 - r.x1 + 1);
            }
            int par = (r.x1 / len + firstFullColumn) % 2 == parity;
            res += (lastFullColumn - firstFullColumn + par) / 2 * (r.x2 - r.x1 + 1) * len;
            return res;
        } else if (r.y1 / len == r.y2 / len) {
            swap(r.x1, r.y1);
            swap(r.x2, r.y2);
            int firstFullColumn = (r.y1 + len - 1) / len;
            int lastFullColumn = r.y2 / len;
            int res = 0;
            if ((r.x1 / len + r.y1 / len) % 2 == parity) {
                res += (firstFullColumn * len - r.y1) * (r.x2 - r.x1 + 1);
            }
            if ((r.x1 / len + lastFullColumn) % 2 == parity) {
                res += (r.y2 - lastFullColumn * len + 1) * (r.x2 - r.x1 + 1);
            }
            int par = (r.x1 / len + firstFullColumn) % 2 == parity;
            res += (lastFullColumn - firstFullColumn + par) / 2 * (r.x2 - r.x1 + 1) * len;
            return res;
        }
        int firstFullColumn = (r.y1 + len - 1) / len;
        int lastFullColumn = r.y2 / len;
        int firstFullRow = (r.x1 + len - 1) / len;
        int lastFullRow = r.x2 / len;
        int fullBlockPar = (firstFullRow + firstFullColumn) % 2;
        int res = ((lastFullColumn - firstFullColumn) * (lastFullRow - firstFullRow) + (fullBlockPar == parity)) / 2 * len * len;
        //  add corners
        int corner1 = 0, corner2 = 0, corner3 = 0, corner4 = 0;
        int up, down, lt, rt;
        up = firstFullRow * len - r.x1;
        down = r.x2 - lastFullRow * len + 1;
        lt = firstFullColumn * len - r.y1;
        rt = r.y2 - lastFullColumn * len + 1;
        if ((firstFullRow + firstFullColumn) % 2 == parity) {
            corner1 = up * lt;
        }
        if ((firstFullRow + lastFullColumn + 1) % 2 == parity) {
            corner2 = up * rt;
        }
        if ((lastFullRow + lastFullColumn) % 2 == parity) {
            corner3 = down * rt;
        }
        if ((lastFullRow + firstFullColumn + 1) % 2 == parity) {
            corner4 = down * lt;
        }
        res += corner1 + corner2 + corner3 + corner4;
        //  add borders
        int ltb = 0, rtb = 0, upb = 0, downb = 0;
        int ltp = (firstFullRow + firstFullColumn + 1) % 2 == parity;
        int rtp = (firstFullRow + lastFullColumn) % 2 == parity;
        int upp = (firstFullRow + 1 + firstFullColumn) % 2 == parity;
        int downp = (lastFullRow + firstFullColumn) % 2 == parity;
        ltb = (lastFullRow - firstFullRow + ltp) / 2 * lt * len;
        rtb = (lastFullRow - firstFullRow + rtp) / 2 * rt * len;
        upb = (lastFullColumn - firstFullColumn + upp) / 2 * up * len;
        downb = (lastFullColumn - firstFullColumn + downp) / 2 * down * len;
        res += ltb + rtb + upb + downb;
        return res;
    };
    int ans = n * n;
    for (int len = 1; len < n; ++len) {
        if (n % len) continue;
        if (1) {
            int cntSide = n / len;
            int black = (cntSide * cntSide + 1) / 2 * len * len;
            int wrongWhite = 0;
            for (rect q : r) {
                wrongWhite += whiteCount(q, len, 1);
            }
            ans = min(ans, wrongWhite * 2 + black - S);
        }
        if (1) {
            int cntSide = n / len;
            int black = (cntSide * cntSide) / 2 * len * len;
            int wrongWhite = 0;
            for (rect q : r) {
                wrongWhite += whiteCount(q, len, 0);
            }
            ans = min(ans, wrongWhite * 2 + black - S);
        }
    }
    cout << ans;
}
/*
 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2260 KB Output is correct
2 Correct 7 ms 724 KB Output is correct
3 Correct 15 ms 1456 KB Output is correct
4 Correct 15 ms 1668 KB Output is correct
5 Correct 19 ms 2004 KB Output is correct
6 Correct 20 ms 1364 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 13 ms 1308 KB Output is correct
9 Correct 32 ms 3080 KB Output is correct
10 Correct 17 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 12 ms 1108 KB Output is correct
17 Correct 25 ms 2868 KB Output is correct
18 Correct 39 ms 3384 KB Output is correct
19 Correct 142 ms 3028 KB Output is correct
20 Correct 157 ms 3412 KB Output is correct
21 Correct 27 ms 2764 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 26 ms 1620 KB Output is correct
24 Correct 36 ms 3028 KB Output is correct
25 Correct 5 ms 468 KB Output is correct
26 Correct 22 ms 1980 KB Output is correct
27 Correct 33 ms 2372 KB Output is correct
28 Correct 35 ms 3284 KB Output is correct
29 Correct 10 ms 1348 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 2260 KB Output is correct
2 Correct 7 ms 724 KB Output is correct
3 Correct 15 ms 1456 KB Output is correct
4 Correct 15 ms 1668 KB Output is correct
5 Correct 19 ms 2004 KB Output is correct
6 Correct 20 ms 1364 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 13 ms 1308 KB Output is correct
9 Correct 32 ms 3080 KB Output is correct
10 Correct 17 ms 1876 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 12 ms 1108 KB Output is correct
27 Correct 25 ms 2868 KB Output is correct
28 Correct 39 ms 3384 KB Output is correct
29 Correct 142 ms 3028 KB Output is correct
30 Correct 157 ms 3412 KB Output is correct
31 Correct 27 ms 2764 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 26 ms 1620 KB Output is correct
34 Correct 36 ms 3028 KB Output is correct
35 Correct 5 ms 468 KB Output is correct
36 Correct 22 ms 1980 KB Output is correct
37 Correct 33 ms 2372 KB Output is correct
38 Correct 35 ms 3284 KB Output is correct
39 Correct 10 ms 1348 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 136 ms 2908 KB Output is correct
42 Correct 48 ms 3204 KB Output is correct
43 Correct 91 ms 2892 KB Output is correct
44 Correct 41 ms 3080 KB Output is correct
45 Correct 47 ms 3320 KB Output is correct
46 Correct 150 ms 3156 KB Output is correct
47 Correct 30 ms 2912 KB Output is correct
48 Correct 58 ms 3040 KB Output is correct
49 Correct 39 ms 2892 KB Output is correct
50 Correct 632 ms 3164 KB Output is correct
51 Correct 666 ms 3248 KB Output is correct
52 Correct 643 ms 3116 KB Output is correct
53 Correct 706 ms 3236 KB Output is correct
54 Correct 634 ms 3024 KB Output is correct
55 Correct 766 ms 3380 KB Output is correct
56 Correct 646 ms 2992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 23 ms 2260 KB Output is correct
10 Correct 7 ms 724 KB Output is correct
11 Correct 15 ms 1456 KB Output is correct
12 Correct 15 ms 1668 KB Output is correct
13 Correct 19 ms 2004 KB Output is correct
14 Correct 20 ms 1364 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 13 ms 1308 KB Output is correct
17 Correct 32 ms 3080 KB Output is correct
18 Correct 17 ms 1876 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 12 ms 1108 KB Output is correct
35 Correct 25 ms 2868 KB Output is correct
36 Correct 39 ms 3384 KB Output is correct
37 Correct 142 ms 3028 KB Output is correct
38 Correct 157 ms 3412 KB Output is correct
39 Correct 27 ms 2764 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 26 ms 1620 KB Output is correct
42 Correct 36 ms 3028 KB Output is correct
43 Correct 5 ms 468 KB Output is correct
44 Correct 22 ms 1980 KB Output is correct
45 Correct 33 ms 2372 KB Output is correct
46 Correct 35 ms 3284 KB Output is correct
47 Correct 10 ms 1348 KB Output is correct
48 Correct 2 ms 340 KB Output is correct
49 Correct 136 ms 2908 KB Output is correct
50 Correct 48 ms 3204 KB Output is correct
51 Correct 91 ms 2892 KB Output is correct
52 Correct 41 ms 3080 KB Output is correct
53 Correct 47 ms 3320 KB Output is correct
54 Correct 150 ms 3156 KB Output is correct
55 Correct 30 ms 2912 KB Output is correct
56 Correct 58 ms 3040 KB Output is correct
57 Correct 39 ms 2892 KB Output is correct
58 Correct 632 ms 3164 KB Output is correct
59 Correct 666 ms 3248 KB Output is correct
60 Correct 643 ms 3116 KB Output is correct
61 Correct 706 ms 3236 KB Output is correct
62 Correct 634 ms 3024 KB Output is correct
63 Correct 766 ms 3380 KB Output is correct
64 Correct 646 ms 2992 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 0 ms 212 KB Output is correct
67 Correct 802 ms 3176 KB Output is correct
68 Correct 786 ms 5448 KB Output is correct
69 Correct 667 ms 4688 KB Output is correct
70 Correct 701 ms 5236 KB Output is correct
71 Correct 677 ms 5208 KB Output is correct
72 Correct 681 ms 5096 KB Output is correct
73 Correct 649 ms 4936 KB Output is correct
74 Correct 719 ms 5356 KB Output is correct
75 Correct 740 ms 5068 KB Output is correct
76 Correct 721 ms 5452 KB Output is correct
77 Correct 129 ms 5708 KB Output is correct
78 Correct 47 ms 5184 KB Output is correct
79 Correct 90 ms 4940 KB Output is correct
80 Correct 97 ms 5196 KB Output is correct
81 Correct 99 ms 4796 KB Output is correct
82 Correct 75 ms 5472 KB Output is correct
83 Correct 57 ms 5068 KB Output is correct
84 Correct 462 ms 5628 KB Output is correct
85 Correct 756 ms 5820 KB Output is correct
86 Correct 2 ms 212 KB Output is correct
87 Correct 1 ms 320 KB Output is correct
88 Correct 743 ms 5788 KB Output is correct
89 Correct 162 ms 1364 KB Output is correct
90 Correct 1 ms 324 KB Output is correct