Submission #154331

# Submission time Handle Problem Language Result Execution time Memory
154331 2019-09-20T18:38:02 Z dandrozavr Chessboard (IZhO18_chessboard) C++14
100 / 100
1314 ms 19236 KB
/*
Uruchamiamy samolot zwiadowczy ( + 500% do wzlamaniej )

/▄/  /█/  /◐/   /▐/   /▌/ /▀/ /░/ /🔥/   choose  own style!

***IT'S OUR LONG WAY TO THE OIILLLL***

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░▌░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░◐◐◐█████████▀▀▀▀▀▀🔥░░░░░░░░███░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░▌░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░█▌░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▄▄▀██████████████████████████████████████████████████
░░░░░░░░░░░░░░░░░░░░░░░░░░▄▄▄████▄████████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████
░░░░░░░░░░░░░░░░░░░░░░░░░░░▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█████████▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░◐◐◐█████████▀▀▀▀▀▀🔥░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████████░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░████████░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░██████░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█████░░░░░░░░░░░░░░░
*/


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>
using namespace std;



#define pb push_back
#define ll long long
#define ld long doubl
#define fi first
#define se second
#define eb emplace_back
#define pii pair < int , int >
#define pipii pair< int, pair < int , int > >
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());

//#include <ext/pb_ds/detail/standard_policies.hpp>'
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;template <typename T> using ordered_set = tree <T, null_type, less< T >, rb_tree_tag,tree_order_statistics_node_update>;
namespace fastio {template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}template<class T1, class T2> ostream& operator << (ostream& os, const pair<T1, T2>& p) { return os << p.first << " " << p.second; }template <class T> ostream &operator<<(ostream &os, set<T> const& con) { for (auto &u : con) os << u << " "; return os; }void pr() {}template <typename T, typename... args> void pr(T x, args... tail) { cout << x << " "; pr(tail...);}}using namespace fastio;

const int N = 3e5 + 5;
const int MA = 1e6 + 1;

const int X[4] = {0, 0, 1, -1};
const int Y[4] = {-1, 1, 0, 0};

vector < int > g[N];
ll n, k;
vector < pair < pii , pii > >  q(N);
int x1, yy1, x2, y2;
int kk[4];

void update(pair < pii , pii > q, int len)
{
    x1 = q.fi.fi;
    x2 = q.se.fi;
    yy1 = q.fi.se;
    y2 = q.se.se;

    kk[0] = x1 % len;
    if (kk[0])
        kk[0] = len - kk[0];
    kk[0] = min(kk[0], x2 - x1 + 1);

    kk[1] = (x2 + 1) % len;
    kk[1] = min(kk[1], x2 - x1 + 1);

    kk[2] = yy1 % len;
    if (kk[2])
        kk[2] = len - kk[2];
    kk[2] = min(kk[2], y2 - yy1 + 1);

    kk[3] = (y2 + 1) % len;
    kk[3] = min(kk[3], y2 - yy1 + 1);
}


ll solve(int len)
{
    ll all = n * n;
    ll ans = 0;
    int llen = len * 2;

    vector < pair < pii , pii > >  z = q;

    for (int i = 0; i < n; ++i)
        if (i % llen < len)
            ans += (n / len / 2 + (n / len) % 2) * 1ll * len; else
            ans += (n / len / 2) * 1ll * len;
    for (int i = 0; i < k; ++i)
    {
        //kk[4]
        update(q[i], len);
//        cout<<ans<<endl;
//        cout<<kk[0]<<" "<<kk[1]<<" "<<kk[2]<<" "<<kk[3]<<endl;
        if (kk[2])
        {
            ll otv = 0;
            if (((x1 % llen < len) + (yy1 % llen < len)) % 2 == 0)
                otv -= kk[0]; else otv += kk[0];
            if (((x1 / len) != (x2 / len)) || (!kk[0]))
            if (((x2 % llen < len) + (yy1 % llen < len)) % 2 == 0)
                otv -= kk[1]; else otv += kk[1];
            ll dis = x2 - x1 + 1 - kk[0] - kk[1];
            ll bx = dis / len / 2 + (dis / len) % 2;
            ll mx = dis / len / 2;

            if ((((x1 + kk[0]) % llen < len) + (yy1 % llen < len)) % 2 == 0)
               otv -= len * bx,
               otv += len * mx; else
               otv += len * bx,
               otv -= len * mx;
            ans += otv * kk[2];
            q[i].fi.se += kk[2];
        }

        if (q[i].fi.fi > q[i].se.fi || q[i].fi.se > q[i].se.se) continue;
        update(q[i], len);

        if (kk[0])
        {
            ll otv = 0;
            if (((x1 % llen < len) + (y2 % llen < len)) % 2 == 0)
                otv -= kk[3]; else otv += kk[3];
            ll dis = y2 - yy1 + 1 - kk[3];
            ll bx = dis / len / 2 + (dis / len) % 2;
            ll mx = dis / len / 2;
            if (((x1 % llen < len) + (yy1 % llen < len)) % 2 == 0)
               otv -= len * bx,
               otv += len * mx; else
               otv += len * bx,
               otv -= len * mx;
            ans += otv * kk[0];
            q[i].fi.fi += kk[0];
        }

        if (q[i].fi.fi > q[i].se.fi || q[i].fi.se > q[i].se.se) continue;
        update(q[i], len);

        if (kk[3])
        {
            ll otv = 0;
            if (((x2 % llen < len) + (y2 % llen < len)) % 2 == 0)
                otv -= kk[1]; else otv += kk[1];
            ll dis = x2 - x1 + 1 - kk[1];
            ll bx = dis / len / 2 + (dis / len) % 2;
            ll mx = dis / len / 2;
            if (((x1 % llen < len) + (y2 % llen < len)) % 2 == 0)
               otv -= len * bx,
               otv += len * mx; else
               otv += len * bx,
               otv -= len * mx;
            ans += otv * kk[3];
            q[i].se.se -= kk[3];
        }

        if (q[i].fi.fi > q[i].se.fi || q[i].fi.se > q[i].se.se) continue;
        update(q[i], len);
        if (kk[1])
        {
            ll otv = 0;
            ll dis = y2 - yy1 + 1;
            ll bx = dis / len / 2 + (dis / len) % 2;
            ll mx = dis / len / 2;
            if (((x2 % llen < len) + (yy1 % llen < len)) % 2 == 0)
               otv -= len * bx,
               otv += len * mx; else
               otv += len * bx,
               otv -= len * mx;
//            cout<<otv<<endl;
            ans += otv * kk[1];
            q[i].se.fi -= kk[1];
        }

        if (q[i].fi.fi > q[i].se.fi || q[i].fi.se > q[i].se.se) continue;
        update(q[i], len);

        //right
        ll dx = q[i].se.fi - q[i].fi.fi + 1;
        ll dy = q[i].se.se - q[i].fi.se + 1;
        ll by = dy / len / 2 + (dy / len) % 2;
        ll my = dy / len / 2;
        ll bx = dx / len / 2 + (dx / len) % 2;
        ll mx = dx / len / 2;
        ll alll = dx * dy;
        int nac = (q[i].fi.fi % llen < len) + (q[i].fi.se % llen < len);
//        cout<<len<<" "<<nac<<" "<<dx<<" "<<dy<<" "<<mx<<" "<<my<<" "<<bx<<" "<<by<<" "<<ans<<endl;

        if (nac % 2 == 0)
        {
            ll ch = (bx * by + mx * my) * 1ll * len * len;
            ans -= ch;
            ans += (alll - ch);
        } else
        {
            ll ch = (mx * by + bx * my) * 1ll * len * len;
            ans -= ch;
            ans += (alll - ch);
        }
    }
//    cout<<ans<<endl;
//    cout<<all<<endl//;
//    cout<<endl;
    q = z;
    return min(ans, all - ans);
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef Estb_probitie
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    cin >> n >> k;

    for (int i = 0; i < k; ++i)
    {
        int x1, x2, yy1, y2;
        cin >> x1 >> yy1 >> x2 >> y2;
        --x1, --x2, --yy1, --y2;
        q[i] = {{x1, yy1}, {x2, y2}};
    }
    ll ans = solve(1);
    for (int i = 2; i * i <= n; ++i)
    if (n % i == 0)
        {
            ans = min(ans, solve(i));
            ans = min(ans, solve(n / i));
        }

    cout << ans;

}

Compilation message

chessboard.cpp: In function 'long long int solve(int)':
chessboard.cpp:113:16: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
             if (((x1 / len) != (x2 / len)) || (!kk[0]))
                ^
# Verdict Execution time Memory Grader output
1 Correct 18 ms 16760 KB Output is correct
2 Correct 30 ms 16796 KB Output is correct
3 Correct 25 ms 16800 KB Output is correct
4 Correct 26 ms 16928 KB Output is correct
5 Correct 19 ms 16760 KB Output is correct
6 Correct 33 ms 16796 KB Output is correct
7 Correct 23 ms 16796 KB Output is correct
8 Correct 23 ms 16836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 18300 KB Output is correct
2 Correct 28 ms 17144 KB Output is correct
3 Correct 43 ms 17784 KB Output is correct
4 Correct 46 ms 17656 KB Output is correct
5 Correct 52 ms 18168 KB Output is correct
6 Correct 38 ms 17656 KB Output is correct
7 Correct 22 ms 16888 KB Output is correct
8 Correct 38 ms 17656 KB Output is correct
9 Correct 69 ms 18936 KB Output is correct
10 Correct 47 ms 17912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 16796 KB Output is correct
2 Correct 27 ms 16796 KB Output is correct
3 Correct 30 ms 16800 KB Output is correct
4 Correct 31 ms 16788 KB Output is correct
5 Correct 24 ms 16800 KB Output is correct
6 Correct 25 ms 16800 KB Output is correct
7 Correct 23 ms 16796 KB Output is correct
8 Correct 29 ms 16800 KB Output is correct
9 Correct 23 ms 16800 KB Output is correct
10 Correct 18 ms 16760 KB Output is correct
11 Correct 24 ms 16796 KB Output is correct
12 Correct 18 ms 16760 KB Output is correct
13 Correct 28 ms 16888 KB Output is correct
14 Correct 27 ms 16796 KB Output is correct
15 Correct 24 ms 16784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 16796 KB Output is correct
2 Correct 27 ms 16796 KB Output is correct
3 Correct 30 ms 16800 KB Output is correct
4 Correct 31 ms 16788 KB Output is correct
5 Correct 24 ms 16800 KB Output is correct
6 Correct 25 ms 16800 KB Output is correct
7 Correct 23 ms 16796 KB Output is correct
8 Correct 29 ms 16800 KB Output is correct
9 Correct 23 ms 16800 KB Output is correct
10 Correct 18 ms 16760 KB Output is correct
11 Correct 24 ms 16796 KB Output is correct
12 Correct 18 ms 16760 KB Output is correct
13 Correct 28 ms 16888 KB Output is correct
14 Correct 27 ms 16796 KB Output is correct
15 Correct 24 ms 16784 KB Output is correct
16 Correct 40 ms 17272 KB Output is correct
17 Correct 59 ms 18168 KB Output is correct
18 Correct 86 ms 18436 KB Output is correct
19 Correct 261 ms 18208 KB Output is correct
20 Correct 279 ms 18424 KB Output is correct
21 Correct 58 ms 18168 KB Output is correct
22 Correct 38 ms 16796 KB Output is correct
23 Correct 68 ms 17440 KB Output is correct
24 Correct 79 ms 18296 KB Output is correct
25 Correct 35 ms 17016 KB Output is correct
26 Correct 59 ms 17824 KB Output is correct
27 Correct 79 ms 17952 KB Output is correct
28 Correct 85 ms 18332 KB Output is correct
29 Correct 34 ms 17272 KB Output is correct
30 Correct 25 ms 16800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 18300 KB Output is correct
2 Correct 28 ms 17144 KB Output is correct
3 Correct 43 ms 17784 KB Output is correct
4 Correct 46 ms 17656 KB Output is correct
5 Correct 52 ms 18168 KB Output is correct
6 Correct 38 ms 17656 KB Output is correct
7 Correct 22 ms 16888 KB Output is correct
8 Correct 38 ms 17656 KB Output is correct
9 Correct 69 ms 18936 KB Output is correct
10 Correct 47 ms 17912 KB Output is correct
11 Correct 28 ms 16796 KB Output is correct
12 Correct 27 ms 16796 KB Output is correct
13 Correct 30 ms 16800 KB Output is correct
14 Correct 31 ms 16788 KB Output is correct
15 Correct 24 ms 16800 KB Output is correct
16 Correct 25 ms 16800 KB Output is correct
17 Correct 23 ms 16796 KB Output is correct
18 Correct 29 ms 16800 KB Output is correct
19 Correct 23 ms 16800 KB Output is correct
20 Correct 18 ms 16760 KB Output is correct
21 Correct 24 ms 16796 KB Output is correct
22 Correct 18 ms 16760 KB Output is correct
23 Correct 28 ms 16888 KB Output is correct
24 Correct 27 ms 16796 KB Output is correct
25 Correct 24 ms 16784 KB Output is correct
26 Correct 40 ms 17272 KB Output is correct
27 Correct 59 ms 18168 KB Output is correct
28 Correct 86 ms 18436 KB Output is correct
29 Correct 261 ms 18208 KB Output is correct
30 Correct 279 ms 18424 KB Output is correct
31 Correct 58 ms 18168 KB Output is correct
32 Correct 38 ms 16796 KB Output is correct
33 Correct 68 ms 17440 KB Output is correct
34 Correct 79 ms 18296 KB Output is correct
35 Correct 35 ms 17016 KB Output is correct
36 Correct 59 ms 17824 KB Output is correct
37 Correct 79 ms 17952 KB Output is correct
38 Correct 85 ms 18332 KB Output is correct
39 Correct 34 ms 17272 KB Output is correct
40 Correct 25 ms 16800 KB Output is correct
41 Correct 218 ms 18920 KB Output is correct
42 Correct 90 ms 19100 KB Output is correct
43 Correct 140 ms 18936 KB Output is correct
44 Correct 88 ms 18976 KB Output is correct
45 Correct 74 ms 19192 KB Output is correct
46 Correct 247 ms 19072 KB Output is correct
47 Correct 66 ms 18808 KB Output is correct
48 Correct 115 ms 18912 KB Output is correct
49 Correct 82 ms 18844 KB Output is correct
50 Correct 1039 ms 18980 KB Output is correct
51 Correct 1094 ms 19192 KB Output is correct
52 Correct 1037 ms 19064 KB Output is correct
53 Correct 1084 ms 19104 KB Output is correct
54 Correct 1035 ms 18936 KB Output is correct
55 Correct 1167 ms 19192 KB Output is correct
56 Correct 986 ms 18848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 16760 KB Output is correct
2 Correct 30 ms 16796 KB Output is correct
3 Correct 25 ms 16800 KB Output is correct
4 Correct 26 ms 16928 KB Output is correct
5 Correct 19 ms 16760 KB Output is correct
6 Correct 33 ms 16796 KB Output is correct
7 Correct 23 ms 16796 KB Output is correct
8 Correct 23 ms 16836 KB Output is correct
9 Correct 54 ms 18300 KB Output is correct
10 Correct 28 ms 17144 KB Output is correct
11 Correct 43 ms 17784 KB Output is correct
12 Correct 46 ms 17656 KB Output is correct
13 Correct 52 ms 18168 KB Output is correct
14 Correct 38 ms 17656 KB Output is correct
15 Correct 22 ms 16888 KB Output is correct
16 Correct 38 ms 17656 KB Output is correct
17 Correct 69 ms 18936 KB Output is correct
18 Correct 47 ms 17912 KB Output is correct
19 Correct 28 ms 16796 KB Output is correct
20 Correct 27 ms 16796 KB Output is correct
21 Correct 30 ms 16800 KB Output is correct
22 Correct 31 ms 16788 KB Output is correct
23 Correct 24 ms 16800 KB Output is correct
24 Correct 25 ms 16800 KB Output is correct
25 Correct 23 ms 16796 KB Output is correct
26 Correct 29 ms 16800 KB Output is correct
27 Correct 23 ms 16800 KB Output is correct
28 Correct 18 ms 16760 KB Output is correct
29 Correct 24 ms 16796 KB Output is correct
30 Correct 18 ms 16760 KB Output is correct
31 Correct 28 ms 16888 KB Output is correct
32 Correct 27 ms 16796 KB Output is correct
33 Correct 24 ms 16784 KB Output is correct
34 Correct 40 ms 17272 KB Output is correct
35 Correct 59 ms 18168 KB Output is correct
36 Correct 86 ms 18436 KB Output is correct
37 Correct 261 ms 18208 KB Output is correct
38 Correct 279 ms 18424 KB Output is correct
39 Correct 58 ms 18168 KB Output is correct
40 Correct 38 ms 16796 KB Output is correct
41 Correct 68 ms 17440 KB Output is correct
42 Correct 79 ms 18296 KB Output is correct
43 Correct 35 ms 17016 KB Output is correct
44 Correct 59 ms 17824 KB Output is correct
45 Correct 79 ms 17952 KB Output is correct
46 Correct 85 ms 18332 KB Output is correct
47 Correct 34 ms 17272 KB Output is correct
48 Correct 25 ms 16800 KB Output is correct
49 Correct 218 ms 18920 KB Output is correct
50 Correct 90 ms 19100 KB Output is correct
51 Correct 140 ms 18936 KB Output is correct
52 Correct 88 ms 18976 KB Output is correct
53 Correct 74 ms 19192 KB Output is correct
54 Correct 247 ms 19072 KB Output is correct
55 Correct 66 ms 18808 KB Output is correct
56 Correct 115 ms 18912 KB Output is correct
57 Correct 82 ms 18844 KB Output is correct
58 Correct 1039 ms 18980 KB Output is correct
59 Correct 1094 ms 19192 KB Output is correct
60 Correct 1037 ms 19064 KB Output is correct
61 Correct 1084 ms 19104 KB Output is correct
62 Correct 1035 ms 18936 KB Output is correct
63 Correct 1167 ms 19192 KB Output is correct
64 Correct 986 ms 18848 KB Output is correct
65 Correct 23 ms 16860 KB Output is correct
66 Correct 24 ms 16796 KB Output is correct
67 Correct 1221 ms 19064 KB Output is correct
68 Correct 1230 ms 19036 KB Output is correct
69 Correct 1078 ms 18720 KB Output is correct
70 Correct 1127 ms 19064 KB Output is correct
71 Correct 1206 ms 18976 KB Output is correct
72 Correct 1137 ms 18936 KB Output is correct
73 Correct 1168 ms 18848 KB Output is correct
74 Correct 1219 ms 18976 KB Output is correct
75 Correct 1149 ms 18972 KB Output is correct
76 Correct 1211 ms 19076 KB Output is correct
77 Correct 211 ms 19192 KB Output is correct
78 Correct 95 ms 18936 KB Output is correct
79 Correct 169 ms 18980 KB Output is correct
80 Correct 164 ms 19064 KB Output is correct
81 Correct 157 ms 18720 KB Output is correct
82 Correct 144 ms 18972 KB Output is correct
83 Correct 121 ms 18848 KB Output is correct
84 Correct 758 ms 19172 KB Output is correct
85 Correct 1314 ms 19236 KB Output is correct
86 Correct 212 ms 16800 KB Output is correct
87 Correct 203 ms 16800 KB Output is correct
88 Correct 1279 ms 19216 KB Output is correct
89 Correct 416 ms 17312 KB Output is correct
90 Correct 208 ms 16888 KB Output is correct