Submission #485460

# Submission time Handle Problem Language Result Execution time Memory
485460 2021-11-07T22:40:59 Z SirCovidThe19th Chessboard (IZhO18_chessboard) C++17
100 / 100
1089 ms 4264 KB
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, x, y) for (int i = x; i < y; i++)
#define ll long long

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

ll n, k, ans = 1e18; vector<rect> A;

void solve(ll sz, int T){
    ll bl = (n / sz) * (n / sz) / 2;
    if ((n / sz) % 2) bl += T;
    ll res = sz * sz * bl;
    
    auto eval = [&](int x1, int y1, int x2, int y2, ll area){
        
        int dX = x2 - x1 + 1;
        int dY = y2 - y1 + 1;
        if (!dX or !dY) return 0LL;
        
        ll L = (dY - 1) / sz + 1; //ceil div to take care of smoll square
        ll H = (dX - 1) / sz + 1; 
        ll black = L * H / 2;

        //odd x odd dimension will have one extra of white/black
        int a = (x1 / sz) % 2, b = (y1 / sz) % 2;
        if (L % 2 and H % 2) black += (a ^ b ^ T); //T = 1 means start is black
        ll white = L * H - black;

        return black * area - white * area; //color operations saved
    };

    for (auto [x1, y1, x2, y2] : A){
        
        ll VU = sz - (x1 % sz);
        ll VD = (x2 % sz) + 1;

        ll HL = sz - (y1 % sz);
        ll HR = (y2 % sz) + 1;

        //small dimension cases
        if (VU + VD > x2 - x1 + 1) VU = x2 - x1 + 1, VD = 0;
        if (HL + HR > y2 - y1 + 1) HL = y2 - y1 + 1, HR = 0;
        
        //inner rect
        res -= eval(x1 + VU, y1 + HL, x2 - VD, y2 - HR, (ll)sz * sz); 

        //corner rec
        res -= eval(x1, y1, x1 + VU - 1, y1 + HL - 1, HL * VU); //top left
        res -= eval(x2 - VD + 1, y1, x2, y1 + HL - 1, VD * HL); //bottom left
        res -= eval(x1, y2 - HR + 1, x1 + VU - 1, y2, HR * VU); //top right
        res -= eval(x2 - VD + 1, y2 - HR + 1, x2, y2, VD * HR); //bottom right

        //side rec
        res -= eval(x1 + VU, y1, x2 - VD, y1 + HL - 1, sz * HL); //left
        res -= eval(x1, y1 + HL, x1 + VU - 1, y2 - HR, sz * VU); //up
        res -= eval(x1 + VU, y2 - HR + 1, x2 - VD, y2, sz * HR); //right
        res -= eval(x2 - VD + 1, y1 + HL, x2, y2 - HR, sz * VD); //down
    }
    ans = min(ans, res);
}

int main(){
    cin >> n >> k; A.resize(k);
    for (auto &i : A){
        cin >> i.x1 >> i.y1 >> i.x2 >> i.y2;
        i.x1--; i.y1--; i.x2--; i.y2--; //0 indexed makes mod ops easier
    }
    FOR(i, 2, n + 1) if (!(n % i)) solve(n / i, 0), solve(n / i, 1);
    cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 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 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 2668 KB Output is correct
2 Correct 16 ms 808 KB Output is correct
3 Correct 43 ms 1796 KB Output is correct
4 Correct 42 ms 1760 KB Output is correct
5 Correct 59 ms 2432 KB Output is correct
6 Correct 37 ms 1592 KB Output is correct
7 Correct 8 ms 564 KB Output is correct
8 Correct 36 ms 1604 KB Output is correct
9 Correct 116 ms 3732 KB Output is correct
10 Correct 58 ms 2248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 26 ms 1096 KB Output is correct
17 Correct 70 ms 2876 KB Output is correct
18 Correct 95 ms 3396 KB Output is correct
19 Correct 209 ms 3012 KB Output is correct
20 Correct 230 ms 3360 KB Output is correct
21 Correct 73 ms 2756 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 51 ms 1588 KB Output is correct
24 Correct 84 ms 3096 KB Output is correct
25 Correct 10 ms 588 KB Output is correct
26 Correct 54 ms 2080 KB Output is correct
27 Correct 74 ms 2372 KB Output is correct
28 Correct 91 ms 3268 KB Output is correct
29 Correct 28 ms 1328 KB Output is correct
30 Correct 4 ms 304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 2668 KB Output is correct
2 Correct 16 ms 808 KB Output is correct
3 Correct 43 ms 1796 KB Output is correct
4 Correct 42 ms 1760 KB Output is correct
5 Correct 59 ms 2432 KB Output is correct
6 Correct 37 ms 1592 KB Output is correct
7 Correct 8 ms 564 KB Output is correct
8 Correct 36 ms 1604 KB Output is correct
9 Correct 116 ms 3732 KB Output is correct
10 Correct 58 ms 2248 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 0 ms 292 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 26 ms 1096 KB Output is correct
27 Correct 70 ms 2876 KB Output is correct
28 Correct 95 ms 3396 KB Output is correct
29 Correct 209 ms 3012 KB Output is correct
30 Correct 230 ms 3360 KB Output is correct
31 Correct 73 ms 2756 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 51 ms 1588 KB Output is correct
34 Correct 84 ms 3096 KB Output is correct
35 Correct 10 ms 588 KB Output is correct
36 Correct 54 ms 2080 KB Output is correct
37 Correct 74 ms 2372 KB Output is correct
38 Correct 91 ms 3268 KB Output is correct
39 Correct 28 ms 1328 KB Output is correct
40 Correct 4 ms 304 KB Output is correct
41 Correct 202 ms 3484 KB Output is correct
42 Correct 115 ms 4012 KB Output is correct
43 Correct 139 ms 3624 KB Output is correct
44 Correct 106 ms 3876 KB Output is correct
45 Correct 102 ms 4120 KB Output is correct
46 Correct 227 ms 3912 KB Output is correct
47 Correct 91 ms 3672 KB Output is correct
48 Correct 126 ms 3692 KB Output is correct
49 Correct 98 ms 3524 KB Output is correct
50 Correct 799 ms 3876 KB Output is correct
51 Correct 851 ms 4036 KB Output is correct
52 Correct 794 ms 3840 KB Output is correct
53 Correct 853 ms 4088 KB Output is correct
54 Correct 787 ms 3908 KB Output is correct
55 Correct 887 ms 4264 KB Output is correct
56 Correct 760 ms 3780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 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 0 ms 204 KB Output is correct
6 Correct 1 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 71 ms 2668 KB Output is correct
10 Correct 16 ms 808 KB Output is correct
11 Correct 43 ms 1796 KB Output is correct
12 Correct 42 ms 1760 KB Output is correct
13 Correct 59 ms 2432 KB Output is correct
14 Correct 37 ms 1592 KB Output is correct
15 Correct 8 ms 564 KB Output is correct
16 Correct 36 ms 1604 KB Output is correct
17 Correct 116 ms 3732 KB Output is correct
18 Correct 58 ms 2248 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 0 ms 292 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 296 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 292 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 26 ms 1096 KB Output is correct
35 Correct 70 ms 2876 KB Output is correct
36 Correct 95 ms 3396 KB Output is correct
37 Correct 209 ms 3012 KB Output is correct
38 Correct 230 ms 3360 KB Output is correct
39 Correct 73 ms 2756 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 51 ms 1588 KB Output is correct
42 Correct 84 ms 3096 KB Output is correct
43 Correct 10 ms 588 KB Output is correct
44 Correct 54 ms 2080 KB Output is correct
45 Correct 74 ms 2372 KB Output is correct
46 Correct 91 ms 3268 KB Output is correct
47 Correct 28 ms 1328 KB Output is correct
48 Correct 4 ms 304 KB Output is correct
49 Correct 202 ms 3484 KB Output is correct
50 Correct 115 ms 4012 KB Output is correct
51 Correct 139 ms 3624 KB Output is correct
52 Correct 106 ms 3876 KB Output is correct
53 Correct 102 ms 4120 KB Output is correct
54 Correct 227 ms 3912 KB Output is correct
55 Correct 91 ms 3672 KB Output is correct
56 Correct 126 ms 3692 KB Output is correct
57 Correct 98 ms 3524 KB Output is correct
58 Correct 799 ms 3876 KB Output is correct
59 Correct 851 ms 4036 KB Output is correct
60 Correct 794 ms 3840 KB Output is correct
61 Correct 853 ms 4088 KB Output is correct
62 Correct 787 ms 3908 KB Output is correct
63 Correct 887 ms 4264 KB Output is correct
64 Correct 760 ms 3780 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 1050 ms 3984 KB Output is correct
68 Correct 1024 ms 4032 KB Output is correct
69 Correct 888 ms 3516 KB Output is correct
70 Correct 938 ms 3860 KB Output is correct
71 Correct 957 ms 3848 KB Output is correct
72 Correct 942 ms 3780 KB Output is correct
73 Correct 943 ms 3648 KB Output is correct
74 Correct 1024 ms 4040 KB Output is correct
75 Correct 956 ms 3780 KB Output is correct
76 Correct 1031 ms 4012 KB Output is correct
77 Correct 236 ms 4164 KB Output is correct
78 Correct 116 ms 3748 KB Output is correct
79 Correct 172 ms 3620 KB Output is correct
80 Correct 181 ms 3828 KB Output is correct
81 Correct 180 ms 3524 KB Output is correct
82 Correct 155 ms 3936 KB Output is correct
83 Correct 139 ms 3664 KB Output is correct
84 Correct 656 ms 4108 KB Output is correct
85 Correct 1066 ms 4164 KB Output is correct
86 Correct 2 ms 204 KB Output is correct
87 Correct 1 ms 204 KB Output is correct
88 Correct 1089 ms 4144 KB Output is correct
89 Correct 231 ms 1020 KB Output is correct
90 Correct 1 ms 288 KB Output is correct