Submission #153914

# Submission time Handle Problem Language Result Execution time Memory
153914 2019-09-17T13:03:21 Z andrew Chessboard (IZhO18_chessboard) C++17
100 / 100
1481 ms 7416 KB
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define sset ordered_set
#define sqr(x) (x)*(x)
#define pw(x) (1ll << x)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
const ll MAXN = 1123456;
const ll N = 2e5;
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> void vout(T s){cout << s << endl;exit(0);}

struct rectangle{
    ll x1, y1, x2, y2;
};

vector <rectangle> b;

vector <ll> pref, pref1;

ll n, k;

ll f(ll len){
    ll kol = n / len;
    ll res = 0;
    ll S = 0, TrueS = 0;
    if(k){
        for(auto i : b){
            S += (i.x2 - i.x1 + 1) * (i.y2 - i.y1 + 1);
            ll old = TrueS;

            ll l = i.x1 / len + (i.x1 % len > 0), r = i.x2 / len + (i.x2 % len > 0);
            if(l == r){
                if(l % 2){
                    TrueS += (i.x2 - i.x1 + 1) * (pref[i.y2] - pref[i.y1 - 1]);
                }
                else{
                    TrueS += (i.x2 - i.x1 + 1) * (pref1[i.y2] - pref1[i.y1 - 1]);
                }
            }else{
                if(l % 2){
                    TrueS += (l * len + 1 - i.x1) * (pref[i.y2] - pref[i.y1 - 1]);
                }else{
                    TrueS += (l * len + 1 - i.x1) * (pref1[i.y2] - pref1[i.y1 - 1]);
                }

                if(r % 2){
                    TrueS += (i.x2 - len * (r - 1)) * (pref[i.y2] - pref[i.y1 - 1]);
                }else{
                    TrueS += (i.x2 - len * (r - 1)) * (pref1[i.y2] - pref1[i.y1 - 1]);
                }

                if(l + 1 < r){
                    ll ft = l + 1;
                    ll Kol = r - l - 1;
                    if(ft % 2){
                        TrueS += len * ((pref[i.y2] - pref[i.y1 - 1]) * ((Kol + 1) / 2) + (pref1[i.y2] - pref1[i.y1 - 1]) * (Kol / 2));
                    }else{
                        TrueS += len * ((pref1[i.y2] - pref1[i.y1 - 1]) * ((Kol + 1) / 2) + (pref[i.y2] - pref[i.y1 - 1]) * (Kol / 2));
                    }
                }
            }

        }
    }
    res += S - TrueS;
    res += len * pref[n] * ((kol + 1) / 2) + len * pref1[n] * (kol / 2) - TrueS;
    return res;
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;

    pref.resize(n + 1);
    pref1.resize(n + 1);

    b.resize(k);

    for(int i = 0; i < k; i++)cin >> b[i].x1 >> b[i].y1 >> b[i].x2 >> b[i].y2;

    ll ans = 1e18;

    for(int i = 1; i < n; i++)if(n % i == 0){

        //firstComb


        for(int j = 1; j <= n; j++)pref[j] = 0;
        for(int j = 1; j <= n; j++)pref1[j] = 1;

        ll uk = 1;
        while(uk <= n){
            for(int j = 1; j <= i; j++)pref[uk + j - 1] = 1;
            uk += 2 * i;
        }

        uk = 1;
        while(uk <= n){
            for(int j = 1; j <= i; j++)pref1[uk + j - 1] = 0;
            uk += 2 * i;
        }

        for(int j = 1; j <= n; j++)pref[j] += pref[j - 1];
        for(int j = 1; j <= n; j++)pref1[j] += pref1[j - 1];

        ans = min(ans, f(i));

        //secondComb

        for(int j = 1; j <= n; j++)pref[j] = 1;
        for(int j = 1; j <= n; j++)pref1[j] = 0;

        uk = 1;
        while(uk <= n){
            for(int j = 1; j <= i; j++)pref[uk + j - 1] = 0;
            uk += 2 * i;
        }

        uk = 1;
        while(uk <= n){
            for(int j = 1; j <= i; j++)pref1[uk + j - 1] = 1;
            uk += 2 * i;
        }

        for(int j = 1; j <= n; j++)pref[j] += pref[j - 1];
        for(int j = 1; j <= n; j++)pref1[j] += pref1[j - 1];

        ans = min(ans, f(i));
    }

    cout << ans << "\n";

    return 0;
}

Compilation message

chessboard.cpp: In function 'll f(ll)':
chessboard.cpp:50:16: warning: unused variable 'old' [-Wunused-variable]
             ll old = TrueS;
                ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 4728 KB Output is correct
2 Correct 12 ms 1448 KB Output is correct
3 Correct 29 ms 3832 KB Output is correct
4 Correct 25 ms 2724 KB Output is correct
5 Correct 34 ms 4344 KB Output is correct
6 Correct 23 ms 3064 KB Output is correct
7 Correct 7 ms 1144 KB Output is correct
8 Correct 22 ms 3064 KB Output is correct
9 Correct 52 ms 5764 KB Output is correct
10 Correct 30 ms 3704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 3 ms 420 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 3 ms 420 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 19 ms 1656 KB Output is correct
17 Correct 42 ms 4348 KB Output is correct
18 Correct 67 ms 5028 KB Output is correct
19 Correct 214 ms 4600 KB Output is correct
20 Correct 238 ms 4984 KB Output is correct
21 Correct 39 ms 4216 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 41 ms 2424 KB Output is correct
24 Correct 56 ms 4600 KB Output is correct
25 Correct 10 ms 760 KB Output is correct
26 Correct 38 ms 3064 KB Output is correct
27 Correct 53 ms 3576 KB Output is correct
28 Correct 60 ms 4856 KB Output is correct
29 Correct 19 ms 1912 KB Output is correct
30 Correct 5 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 4728 KB Output is correct
2 Correct 12 ms 1448 KB Output is correct
3 Correct 29 ms 3832 KB Output is correct
4 Correct 25 ms 2724 KB Output is correct
5 Correct 34 ms 4344 KB Output is correct
6 Correct 23 ms 3064 KB Output is correct
7 Correct 7 ms 1144 KB Output is correct
8 Correct 22 ms 3064 KB Output is correct
9 Correct 52 ms 5764 KB Output is correct
10 Correct 30 ms 3704 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 380 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 420 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 19 ms 1656 KB Output is correct
27 Correct 42 ms 4348 KB Output is correct
28 Correct 67 ms 5028 KB Output is correct
29 Correct 214 ms 4600 KB Output is correct
30 Correct 238 ms 4984 KB Output is correct
31 Correct 39 ms 4216 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 41 ms 2424 KB Output is correct
34 Correct 56 ms 4600 KB Output is correct
35 Correct 10 ms 760 KB Output is correct
36 Correct 38 ms 3064 KB Output is correct
37 Correct 53 ms 3576 KB Output is correct
38 Correct 60 ms 4856 KB Output is correct
39 Correct 19 ms 1912 KB Output is correct
40 Correct 5 ms 504 KB Output is correct
41 Correct 242 ms 6548 KB Output is correct
42 Correct 80 ms 7036 KB Output is correct
43 Correct 143 ms 6520 KB Output is correct
44 Correct 72 ms 7032 KB Output is correct
45 Correct 59 ms 7304 KB Output is correct
46 Correct 242 ms 7096 KB Output is correct
47 Correct 52 ms 6724 KB Output is correct
48 Correct 100 ms 6756 KB Output is correct
49 Correct 65 ms 6520 KB Output is correct
50 Correct 1088 ms 6720 KB Output is correct
51 Correct 1161 ms 7148 KB Output is correct
52 Correct 1079 ms 6672 KB Output is correct
53 Correct 1130 ms 7032 KB Output is correct
54 Correct 1052 ms 6648 KB Output is correct
55 Correct 1199 ms 7240 KB Output is correct
56 Correct 1041 ms 6452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 37 ms 4728 KB Output is correct
10 Correct 12 ms 1448 KB Output is correct
11 Correct 29 ms 3832 KB Output is correct
12 Correct 25 ms 2724 KB Output is correct
13 Correct 34 ms 4344 KB Output is correct
14 Correct 23 ms 3064 KB Output is correct
15 Correct 7 ms 1144 KB Output is correct
16 Correct 22 ms 3064 KB Output is correct
17 Correct 52 ms 5764 KB Output is correct
18 Correct 30 ms 3704 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 380 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 420 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 19 ms 1656 KB Output is correct
35 Correct 42 ms 4348 KB Output is correct
36 Correct 67 ms 5028 KB Output is correct
37 Correct 214 ms 4600 KB Output is correct
38 Correct 238 ms 4984 KB Output is correct
39 Correct 39 ms 4216 KB Output is correct
40 Correct 3 ms 376 KB Output is correct
41 Correct 41 ms 2424 KB Output is correct
42 Correct 56 ms 4600 KB Output is correct
43 Correct 10 ms 760 KB Output is correct
44 Correct 38 ms 3064 KB Output is correct
45 Correct 53 ms 3576 KB Output is correct
46 Correct 60 ms 4856 KB Output is correct
47 Correct 19 ms 1912 KB Output is correct
48 Correct 5 ms 504 KB Output is correct
49 Correct 242 ms 6548 KB Output is correct
50 Correct 80 ms 7036 KB Output is correct
51 Correct 143 ms 6520 KB Output is correct
52 Correct 72 ms 7032 KB Output is correct
53 Correct 59 ms 7304 KB Output is correct
54 Correct 242 ms 7096 KB Output is correct
55 Correct 52 ms 6724 KB Output is correct
56 Correct 100 ms 6756 KB Output is correct
57 Correct 65 ms 6520 KB Output is correct
58 Correct 1088 ms 6720 KB Output is correct
59 Correct 1161 ms 7148 KB Output is correct
60 Correct 1079 ms 6672 KB Output is correct
61 Correct 1130 ms 7032 KB Output is correct
62 Correct 1052 ms 6648 KB Output is correct
63 Correct 1199 ms 7240 KB Output is correct
64 Correct 1041 ms 6452 KB Output is correct
65 Correct 3 ms 504 KB Output is correct
66 Correct 3 ms 380 KB Output is correct
67 Correct 1250 ms 6776 KB Output is correct
68 Correct 1206 ms 6776 KB Output is correct
69 Correct 970 ms 6200 KB Output is correct
70 Correct 1040 ms 6904 KB Output is correct
71 Correct 1059 ms 6648 KB Output is correct
72 Correct 1027 ms 6524 KB Output is correct
73 Correct 1061 ms 6392 KB Output is correct
74 Correct 1287 ms 6788 KB Output is correct
75 Correct 1481 ms 6520 KB Output is correct
76 Correct 1141 ms 6896 KB Output is correct
77 Correct 189 ms 7416 KB Output is correct
78 Correct 74 ms 6904 KB Output is correct
79 Correct 135 ms 6648 KB Output is correct
80 Correct 142 ms 6880 KB Output is correct
81 Correct 143 ms 6580 KB Output is correct
82 Correct 116 ms 7032 KB Output is correct
83 Correct 89 ms 6796 KB Output is correct
84 Correct 694 ms 7308 KB Output is correct
85 Correct 1274 ms 7116 KB Output is correct
86 Correct 79 ms 1656 KB Output is correct
87 Correct 87 ms 1784 KB Output is correct
88 Correct 1200 ms 7208 KB Output is correct
89 Correct 280 ms 2808 KB Output is correct
90 Correct 80 ms 1656 KB Output is correct