답안 #155231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
155231 2019-09-27T07:47:38 Z Dasha_Gnedko Chessboard (IZhO18_chessboard) C++14
47 / 100
2000 ms 5752 KB
#include <bits/stdc++.h>

//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define int long long

using namespace std;

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

const int N = 1e6 + 100;
const int M = 21890;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const int P = 1336;
const ld eps = 0.000000001;
const int inf = 1e18 + 7;

mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());

int l1[N], r1[N], l2[N], r2[N];
int m;

pair <int, pair <int, int> > get(int r, int x, int y, int fl)
{
    int gx = x / r, gy = y / r;
    if (x % r) gx++;
    if (y % r) gy++;

    bool f;

    if (fl == 0)
    {
        if (gx % 2)
        {
            if (gy % 2) f = 0;
            else f = 1;
        }
        else if (gy % 2) f = 1;
        else f = 0;
    }
    else
    {
        if (gx % 2)
        {
            if (gy % 2) f = 1;
            else f = 0;
        }
        else if (gy % 2) f = 0;
        else f = 1;
    }

    int bx = gx * r - x + 1;
    int by = gy * r - y + 1;

    return {f, {bx, by}};

}

int solve(int r, int x1, int y1, int x2, int y2, int fl)
{
    int gx1 = x1 / r + 1, gx2 = x2 / r;
    if (x1 % r != 1) gx1++;
    if (x1 % r == 0) gx1--;

    int gy1 = y1 / r + 1, gy2 = y2 / r;
    if (y1 % r != 1) gy1++;
    if (y1 % r == 0) gy1--;

    if (r == 1)
    {
        gx1 = x1; gx2 = x2; gy1 = y1; gy2 = y2;
    }


    if (gx1 > gx2 || gy1 > gy2)
    {

//    cout << "! " << gx1 << " " << gx2 << "     " << gy1 << " " << gy2 << endl;
        if (gx1 > gx2 && gy1 > gy2)
        {
            pair < int, pair <int, int> > p = get(r, x1, y1, fl);
//            cout << x1 << " " << y1 << " " << p.S.F << " " << p.S.S << endl;
            if (x1 + p.S.F > x2 && y1 + p.S.S > y2)
            {
//                cout << x1 << " " << y1 << " " << p.F << endl;
                if (p.F == 0) return (x2 - x1 + 1) * (y2 - y1 + 1);
                else return 0;
            }

            if (x1 + p.S.F > x2)
            {
                int S = (x2 - x1 + 1) * p.S.S;
                if (p.F == 0) return S;
                else return (x2 - x1 + 1) * (y2 - y1 + 1) - S;
            }

            if (y1 + p.S.S > y2)
            {
                int S = (y2 - y1 + 1) * p.S.F;
                if (p.F == 0) return S;
                else return (x2 - x1 + 1) * (y2 - y1 + 1) - S;
            }

            int v1 = p.S.F * p.S.S + (x2 - x1 + 1 - p.S.F) * (y2 - y1 + 1 - p.S.S);
            int v2 = (x2 - x1 + 1) * (y2 - y1 + 1) - v1;

//            cout << "!!! " << v1 << " " << v2 << endl;

            if (p.F == 0) return v1;
            else return v2;

        }

        return 0;


    }

    int stx = gx1 * r - r + 1;
    int ex = gx2 * r;

    int sty = gy1 * r - r + 1;
    int ey = gy2 * r;

    int kolx = (gx2 - gx1 + 1 + 1) / 2;
    int koly = (gy2 - gy1 + 1 + 1) / 2;

    int S = kolx * koly * r * r;

    kolx = (gx2 - gx1 + 1) / 2;
    koly = (gy2 - gy1 + 1) / 2;

    S += kolx * koly * r * r;

    pair < int, pair <int, int> > p = get(r, stx, sty, fl);

//    cout << p.F << " " << S << endl;

    if (p.F == 0) return S;
    else return (ex - stx + 1) * (ey - sty + 1) - S;

}

int get_ans(int r, int n)
{
    int S = n * n;

    n /= r;

    int kx = (n + 1) / 2;
    int k1 = kx * kx;
    kx = n / 2;
    k1 += kx * kx;
    k1 *= r * r;

    int k2 = S - k1;


    int v1 = 0, v2 = 0;

    for(int i = 0; i < m; i++)
    {
        int pl = (l2[i] - l1[i] + 1) * (r2[i] - r1[i] + 1);
        int c1 = solve(r, l1[i], r1[i], l2[i], r2[i], 1);
        int c2 = solve(r, l1[i], r1[i], l2[i], r2[i], 0);

//        cout << r << " " << solve(r, l1[i], r1[i], l2[i], r2[i], 1) << endl;
        v1 += c1;
        k1 -= (pl - c1);

        v2 += c2;
        k2 -= (pl - c2);
    }

//    cout << r << " " << v1 << " " << k1 << " " << v2 << " " << k2 << endl;

    return min(v1 + k1, v2 + k2);

}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n >> m;

    for(int i = 0; i < m; i++)
    {
        cin >> l1[i] >> r1[i] >> l2[i] >> r2[i];
//        cout << solve(2, l1[i], r1[i], l2[i], r2[i], 1) << endl;
    }

//    cout << get_ans(2, n);
//
//    return 0;

    int d = 1;
    int ans = inf;
    while (d * d <= n)
    {
        if (n % d == 0)
        {
            ans = min(ans, get_ans(d, n));
            if (d != 1) ans = min(ans, get_ans(n / d, n));
        }
        d++;
    }

    cout << ans;

}
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 2268 KB Output is correct
2 Correct 11 ms 1272 KB Output is correct
3 Correct 25 ms 2552 KB Output is correct
4 Correct 27 ms 2676 KB Output is correct
5 Correct 34 ms 3320 KB Output is correct
6 Correct 22 ms 1812 KB Output is correct
7 Correct 7 ms 888 KB Output is correct
8 Correct 23 ms 2296 KB Output is correct
9 Correct 55 ms 4828 KB Output is correct
10 Correct 34 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 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 4 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 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 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 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 4 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 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 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 25 ms 1272 KB Output is correct
17 Correct 44 ms 3064 KB Output is correct
18 Correct 86 ms 3448 KB Output is correct
19 Correct 445 ms 3184 KB Output is correct
20 Correct 497 ms 3576 KB Output is correct
21 Correct 42 ms 2936 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 67 ms 1656 KB Output is correct
24 Correct 81 ms 3192 KB Output is correct
25 Correct 15 ms 632 KB Output is correct
26 Correct 50 ms 2168 KB Output is correct
27 Correct 85 ms 2576 KB Output is correct
28 Correct 85 ms 3448 KB Output is correct
29 Correct 19 ms 1400 KB Output is correct
30 Correct 5 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 2268 KB Output is correct
2 Correct 11 ms 1272 KB Output is correct
3 Correct 25 ms 2552 KB Output is correct
4 Correct 27 ms 2676 KB Output is correct
5 Correct 34 ms 3320 KB Output is correct
6 Correct 22 ms 1812 KB Output is correct
7 Correct 7 ms 888 KB Output is correct
8 Correct 23 ms 2296 KB Output is correct
9 Correct 55 ms 4828 KB Output is correct
10 Correct 34 ms 3064 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 4 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 3 ms 348 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 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 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 25 ms 1272 KB Output is correct
27 Correct 44 ms 3064 KB Output is correct
28 Correct 86 ms 3448 KB Output is correct
29 Correct 445 ms 3184 KB Output is correct
30 Correct 497 ms 3576 KB Output is correct
31 Correct 42 ms 2936 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 67 ms 1656 KB Output is correct
34 Correct 81 ms 3192 KB Output is correct
35 Correct 15 ms 632 KB Output is correct
36 Correct 50 ms 2168 KB Output is correct
37 Correct 85 ms 2576 KB Output is correct
38 Correct 85 ms 3448 KB Output is correct
39 Correct 19 ms 1400 KB Output is correct
40 Correct 5 ms 504 KB Output is correct
41 Correct 367 ms 4984 KB Output is correct
42 Correct 88 ms 5624 KB Output is correct
43 Correct 195 ms 4984 KB Output is correct
44 Correct 85 ms 5496 KB Output is correct
45 Correct 57 ms 5752 KB Output is correct
46 Correct 410 ms 5496 KB Output is correct
47 Correct 51 ms 5240 KB Output is correct
48 Correct 142 ms 5112 KB Output is correct
49 Correct 79 ms 4984 KB Output is correct
50 Execution timed out 2037 ms 5368 KB Time limit exceeded
51 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 2268 KB Output is correct
10 Correct 11 ms 1272 KB Output is correct
11 Correct 25 ms 2552 KB Output is correct
12 Correct 27 ms 2676 KB Output is correct
13 Correct 34 ms 3320 KB Output is correct
14 Correct 22 ms 1812 KB Output is correct
15 Correct 7 ms 888 KB Output is correct
16 Correct 23 ms 2296 KB Output is correct
17 Correct 55 ms 4828 KB Output is correct
18 Correct 34 ms 3064 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 4 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 348 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 3 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 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 25 ms 1272 KB Output is correct
35 Correct 44 ms 3064 KB Output is correct
36 Correct 86 ms 3448 KB Output is correct
37 Correct 445 ms 3184 KB Output is correct
38 Correct 497 ms 3576 KB Output is correct
39 Correct 42 ms 2936 KB Output is correct
40 Correct 3 ms 376 KB Output is correct
41 Correct 67 ms 1656 KB Output is correct
42 Correct 81 ms 3192 KB Output is correct
43 Correct 15 ms 632 KB Output is correct
44 Correct 50 ms 2168 KB Output is correct
45 Correct 85 ms 2576 KB Output is correct
46 Correct 85 ms 3448 KB Output is correct
47 Correct 19 ms 1400 KB Output is correct
48 Correct 5 ms 504 KB Output is correct
49 Correct 367 ms 4984 KB Output is correct
50 Correct 88 ms 5624 KB Output is correct
51 Correct 195 ms 4984 KB Output is correct
52 Correct 85 ms 5496 KB Output is correct
53 Correct 57 ms 5752 KB Output is correct
54 Correct 410 ms 5496 KB Output is correct
55 Correct 51 ms 5240 KB Output is correct
56 Correct 142 ms 5112 KB Output is correct
57 Correct 79 ms 4984 KB Output is correct
58 Execution timed out 2037 ms 5368 KB Time limit exceeded
59 Halted 0 ms 0 KB -