답안 #863377

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863377 2023-10-20T06:22:56 Z deepaung 게임 (IOI13_game) C++14
100 / 100
5509 ms 78264 KB
#include <bits/stdc++.h>
#include "game.h"

#define f first
#define s second
#define pll pair<ll, ll>
#define ll long long

using namespace std;

mt19937 rnd(time(NULL));

ll R, C;

ll gcd2(ll X, ll Y) {
    ll tmp;
    while (X != Y && Y != 0) {
        tmp = X;
        X = Y;
        Y = tmp % Y;
    }
    return X;
}

struct Treap {
    struct node {
        ll val, gcd, sz, pri;
        pll pos;
        node *l, *r;

        node(ll val=0, pll pos={0, 0}, node *l=NULL, node *r=NULL) : 
            val(val), gcd(val), sz(1), pri(rnd()), pos(pos), l(l), r(r) {}
    };

    node *root;

    Treap() : root(NULL) {}

    void recalc(node *cur) {
        if (cur == NULL) return;

        cur->sz = 1;
        cur->gcd = cur->val;
        if (cur->l != NULL)
            cur->sz += cur->l->sz, cur->gcd = gcd2(cur->gcd, cur->l->gcd);
        if (cur->r != NULL)
            cur->sz += cur->r->sz, cur->gcd = gcd2(cur->gcd, cur->r->gcd);
    }

    void split(node *cur, node *&left, node *&right, pll pos) {
        if (cur == NULL) {
            left = right = NULL;
            return;
        }

        if (cur->pos <= pos) {
            split(cur->r, cur->r, right, pos);
            left = cur;
        } else {
            split(cur->l, left, cur->l, pos);
            right = cur;
        }

        recalc(cur);
    }

    void merge(node *&cur, node *left, node *right) {
        if (left == NULL || right == NULL) {
            cur = (left != NULL ? left : right);
            return;
        }

        if (left->pri > right->pri) {
            merge(left->r, left->r, right);
            cur = left;
        } else {
            merge(right->l, left, right->l);
            cur = right;
        }

        recalc(cur);
    }

    void update(ll x, ll y, ll val) {
        node *left, *mid, *right;
        split(root, left, mid, {y, x-1});
        split(mid, mid, right, {y, x});

        merge(root, left, new node(val, {y, x}));
        merge(root, root, right);
    }

    ll gcd_range(ll yl, ll yr) {
        node *left, *mid, *right;
        split(root, left, mid, {yl, -1});
        split(mid, mid, right, {yr+1, -1});

        ll ans = (mid != NULL ? mid->gcd : 0);

        merge(root, left, mid);
        merge(root, root, right);

        return ans;
    }

};

struct Segtree {
    struct node {
        Treap treap;
        node *l, *r;

        node() : 
            treap(Treap()), l(NULL), r(NULL) {}
    };

    node *root;

    void update(ll x, ll y, ll val, node *&cur, ll l, ll r) {
        if (cur == NULL) cur = new node();

        cur->treap.update(x, y, val);
        if (l == r) return;

        ll mid = (l + r) >> 1;
        if (x <= mid) update(x, y, val, cur->l, l, mid);
        else update(x, y, val, cur->r, mid+1, r);
    }

    ll gcd_range(ll xl, ll yl, ll xr, ll yr, node *cur, ll l, ll r) {
        if (cur == NULL) return 0;
        if (xr < l || r < xl) return 0;

        if (xl <= l && r <= xr) {
            return cur->treap.gcd_range(yl, yr);
        }

        ll mid = (l + r) >> 1;
        ll resl = gcd_range(xl, yl, xr, yr, cur->l, l, mid);
        ll resr = gcd_range(xl, yl, xr, yr, cur->r, mid+1, r);

        return gcd2(resl, resr);

        // if (xl == l && r == xr) {
        //     return cur->treap.gcd_range(yl, yr);
        // }

        // ll mid = (l + r) >> 1;
        // if (xr <= mid) {
        //     return gcd_range(xl, yl, xr, yr, cur->l, l, mid);
        // } else if (xl >= mid + 1) {
        //     return gcd_range(xl, yl, xr, yr, cur->r, mid+1, r);
        // } else {
        //     return gcd2(
        //         gcd_range(xl, yl, mid, yr, cur->l, l, mid),
        //         gcd_range(mid+1, yl, xr, yr, cur->r, mid+1, r)
        //     );
        // }
    }

} segtree;

void init(int R, int C) {
    ::R = R;
    ::C = C;
}

void update(int x, int y, ll val) {
    x++, y++;
    segtree.update(x, y, val, segtree.root, 1, R);
}

ll calculate(int xl, int yl, int xr, int yr) {
    xl++, yl++, xr++, yr++;
    return segtree.gcd_range(xl, yl, xr, yr, segtree.root, 1, R);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 579 ms 7872 KB Output is correct
5 Correct 242 ms 8064 KB Output is correct
6 Correct 933 ms 5128 KB Output is correct
7 Correct 1071 ms 9272 KB Output is correct
8 Correct 737 ms 7704 KB Output is correct
9 Correct 1049 ms 9276 KB Output is correct
10 Correct 903 ms 9044 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 544 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 947 ms 12988 KB Output is correct
13 Correct 2344 ms 9860 KB Output is correct
14 Correct 475 ms 15072 KB Output is correct
15 Correct 2442 ms 14988 KB Output is correct
16 Correct 312 ms 14932 KB Output is correct
17 Correct 1557 ms 11176 KB Output is correct
18 Correct 2814 ms 16088 KB Output is correct
19 Correct 2364 ms 16632 KB Output is correct
20 Correct 2322 ms 15852 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 605 ms 8044 KB Output is correct
13 Correct 260 ms 12112 KB Output is correct
14 Correct 942 ms 9492 KB Output is correct
15 Correct 1087 ms 9384 KB Output is correct
16 Correct 736 ms 8016 KB Output is correct
17 Correct 1029 ms 9556 KB Output is correct
18 Correct 921 ms 8816 KB Output is correct
19 Correct 917 ms 17352 KB Output is correct
20 Correct 2334 ms 13712 KB Output is correct
21 Correct 480 ms 14820 KB Output is correct
22 Correct 2529 ms 14844 KB Output is correct
23 Correct 336 ms 14752 KB Output is correct
24 Correct 1563 ms 11152 KB Output is correct
25 Correct 2872 ms 16288 KB Output is correct
26 Correct 2286 ms 16328 KB Output is correct
27 Correct 2321 ms 15640 KB Output is correct
28 Correct 767 ms 41204 KB Output is correct
29 Correct 1420 ms 44300 KB Output is correct
30 Correct 3388 ms 35112 KB Output is correct
31 Correct 2826 ms 34596 KB Output is correct
32 Correct 650 ms 34380 KB Output is correct
33 Correct 961 ms 34360 KB Output is correct
34 Correct 360 ms 37456 KB Output is correct
35 Correct 1770 ms 26420 KB Output is correct
36 Correct 3301 ms 41624 KB Output is correct
37 Correct 2577 ms 42016 KB Output is correct
38 Correct 2823 ms 41404 KB Output is correct
39 Correct 2247 ms 34504 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 564 ms 7692 KB Output is correct
13 Correct 257 ms 7972 KB Output is correct
14 Correct 928 ms 4940 KB Output is correct
15 Correct 1109 ms 9292 KB Output is correct
16 Correct 722 ms 7728 KB Output is correct
17 Correct 1068 ms 9264 KB Output is correct
18 Correct 921 ms 8812 KB Output is correct
19 Correct 953 ms 17232 KB Output is correct
20 Correct 2327 ms 13552 KB Output is correct
21 Correct 482 ms 15036 KB Output is correct
22 Correct 2430 ms 14944 KB Output is correct
23 Correct 303 ms 14860 KB Output is correct
24 Correct 1539 ms 11016 KB Output is correct
25 Correct 2778 ms 16468 KB Output is correct
26 Correct 2310 ms 16412 KB Output is correct
27 Correct 2250 ms 15872 KB Output is correct
28 Correct 763 ms 41168 KB Output is correct
29 Correct 1394 ms 44200 KB Output is correct
30 Correct 3442 ms 35204 KB Output is correct
31 Correct 3048 ms 34424 KB Output is correct
32 Correct 679 ms 34464 KB Output is correct
33 Correct 946 ms 34368 KB Output is correct
34 Correct 332 ms 37460 KB Output is correct
35 Correct 1725 ms 26340 KB Output is correct
36 Correct 3169 ms 41860 KB Output is correct
37 Correct 2566 ms 41868 KB Output is correct
38 Correct 2601 ms 41352 KB Output is correct
39 Correct 1074 ms 76116 KB Output is correct
40 Correct 2348 ms 78264 KB Output is correct
41 Correct 5509 ms 67888 KB Output is correct
42 Correct 4889 ms 66140 KB Output is correct
43 Correct 584 ms 73040 KB Output is correct
44 Correct 1204 ms 63796 KB Output is correct
45 Correct 2179 ms 34840 KB Output is correct
46 Correct 4126 ms 77164 KB Output is correct
47 Correct 4046 ms 77228 KB Output is correct
48 Correct 3946 ms 77484 KB Output is correct
49 Correct 0 ms 348 KB Output is correct