답안 #930104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930104 2024-02-18T13:37:52 Z hoangsacura 게임 (IOI13_game) C++17
100 / 100
5693 ms 63652 KB
#include<bits/stdc++.h>
#include "game.h"
#define task ""
#define PI acos(-1)
#define fi first
#define sc second
#define ll long long
#define ld long double
#define rep(i, a, b, c) for (int i = a; i <= b; i += c)
#define ford(i, a, b, c) for (int i = a; i >= b; i -= c)
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
const int inf = 2e9, bsize = 350, maxn = 1e6 + 2, N = 2e5, mod = 998224353;
const long long llinf = 1e18;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef pair<II, II> IV;
int R, C;

struct Node {
    Node *left, *right, *par;
    int pos = 0;
    ll val = 0;
    ll GCD = 0;
};

typedef Node* tnode;
tnode nilt;

void upnode(tnode x) {
    x->GCD = __gcd(x->left->GCD, __gcd(x->right->GCD, x->val));
}

void link(tnode x, tnode y, bool left) {
    y->par = x;

    if (left)
        x->left = y;
    else
        x->right = y;
}

void uptree(tnode x) {
    tnode y = x->par;
    tnode z = y->par;

    if (y->left == x) {
        link(y, x->right, 1);
        link(x, y, 0);
    }
    else {
        link(y, x->left, 0);
        link(x, y, 1);
    }

    if (z != nilt) {
        if (z->left == y)
            link(z, x, 1);
        else
            link(z, x, 0);
    }
    else
        x->par = z;

    upnode(y);
    upnode(x);
}

void splay(tnode x) {
    if (x == nilt)
        return;

    while (x->par != nilt) {
        tnode y = x->par;
        tnode z = y->par;

        if (z != nilt) {
            if ((z->left == y) == (y->left == x))
                uptree(y);
            else
                uptree(x);
        }
        uptree(x);
    }
}

tnode Find(tnode &t, int pos) {
    if (t == nilt)
        return t;

    tnode res = nilt;
    tnode x = t;

    while (x != nilt) {
        t = x;

        if (pos < x->pos)
            x = x->left;
        else {
            res = x;
            x = x->right;
        }
    }

    splay(t);

    return res;
}

void split(tnode T, int pos, tnode &T1, tnode &T2) {
    T1 = Find(T, pos);

    if (T1 == nilt)
        T2 = T;
    else {
        splay(T1);
        T2 = T1->right;
        T1->right = T2->par = nilt;
        upnode(T1);
    }
}

tnode join(tnode T1, tnode T2) {
    if (T1 == nilt)
        return T2;

    while (T1->right != nilt)
        T1 = T1->right;
    splay(T1);

    if (T2 != nilt)
        link(T1, T2, 0);

    upnode(T1);

    return T1;
}

void ins(tnode &root, int pos, ll val) {
    if (root == nilt) {
        root = new Node();
        root->left = root->right = root->par = nilt;
        root->pos = pos;
        root->val = root->GCD = val;
        return;
    }

    tnode p = Find(root, pos);

    if (p == nilt) {
        tnode x = root;
        root = new Node();
        root->left = root->right = root->par = nilt;
        root->pos = pos;
        root->val = val;

        link(root, x, 0);

        upnode(root);

        return;
    }

    root = p;
    splay(root);

    if (p->pos < pos) {
        tnode x, y;
        x = root;
        y = x->right;
        y->par = x->right = nilt;
        upnode(x);

        root = new Node();
        root->left = root->right = root->par = nilt;
        root->pos = pos;
        root->val = val;

        if (x != nilt)
            link(root, x, 1);
        if (y != nilt)
            link(root, y, 0);

        upnode(root);

        return;
    }

    root->val = val;
    upnode(root);
}

ll take(tnode &root, int pos) {
    tnode p = Find(root, pos);

    if (p == nilt || p->pos != pos)
        return 0;

    return p->val;
}

struct IT {
    tnode x;
    int left = -1;
    int right = -1;
};
vector<IT> it;

void up(int r, int Q) {
    ll Left = (it[r].left == -1) ? 0 : take(it[it[r].left].x, Q);
    ll Right = (it[r].right == -1) ? 0 : take(it[it[r].right].x, Q);
    ins(it[r].x, Q, __gcd(Left, Right));
}

void update2D(int r, int lo, int hi, int P, int Q, ll k) {
    if (lo == hi) {
        ins(it[r].x, Q, k);
        return;
    }

    int mid = (lo + hi)/2;

    if (P <= mid) {
        if (it[r].left == -1) {
            it.emplace_back();
            it[r].left = it.size() - 1;
            it[it[r].left].x = nilt;
        }
        update2D(it[r].left, lo, mid, P, Q, k);
    }
    else {
        if (it[r].right == -1) {
            it.emplace_back();
            it[r].right = it.size() - 1;
            it[it[r].right].x = nilt;
        }
        update2D(it[r].right, mid + 1, hi, P, Q, k);
    }

    up(r, Q);
}

ll get(int r, int lo, int hi, int P, int Q, int U, int V) {
    if (hi < P || Q < lo || r == -1)
        return 0;

    if (P <= lo && hi <= Q) {
        if (it[r].x == nilt)
            return 0;
        tnode x, y, z;
        split(it[r].x, U - 1, x, y);
        split(y, V, y, z);

        ll ans = y->GCD;

        it[r].x = join(x, join(y, z));

        return ans;
    }

    int mid = (lo + hi)/2;

    return __gcd(get(it[r].left, lo, mid, P, Q, U, V), get(it[r].right, mid + 1, hi, P, Q, U, V));
}


void init(int _R, int _C) {
    nilt = new Node();
    it.emplace_back();
    it[0].x = nilt;

    R = _R;
    C = _C;
}

void update(int P, int Q, ll K) {
    update2D(0, 0, R - 1, P, Q, K);
}

ll calculate(int P, int Q, int U, int V) {
    return get(0, 0, R - 1, P, U, Q, V);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 0 ms 432 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 873 ms 11704 KB Output is correct
5 Correct 259 ms 11136 KB Output is correct
6 Correct 1376 ms 8700 KB Output is correct
7 Correct 1554 ms 8536 KB Output is correct
8 Correct 1020 ms 7576 KB Output is correct
9 Correct 1490 ms 8540 KB Output is correct
10 Correct 1339 ms 8128 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 348 KB Output is correct
5 Correct 1 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 432 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1103 ms 13336 KB Output is correct
13 Correct 3068 ms 7700 KB Output is correct
14 Correct 398 ms 5704 KB Output is correct
15 Correct 3343 ms 8892 KB Output is correct
16 Correct 290 ms 11256 KB Output is correct
17 Correct 2182 ms 10064 KB Output is correct
18 Correct 3646 ms 12816 KB Output is correct
19 Correct 3070 ms 12800 KB Output is correct
20 Correct 2847 ms 12212 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 348 KB Output is correct
5 Correct 1 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 1 ms 440 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 827 ms 11484 KB Output is correct
13 Correct 257 ms 11236 KB Output is correct
14 Correct 1372 ms 9024 KB Output is correct
15 Correct 1536 ms 8408 KB Output is correct
16 Correct 1030 ms 7504 KB Output is correct
17 Correct 1480 ms 8632 KB Output is correct
18 Correct 1329 ms 8564 KB Output is correct
19 Correct 1077 ms 13500 KB Output is correct
20 Correct 3056 ms 7844 KB Output is correct
21 Correct 383 ms 5660 KB Output is correct
22 Correct 3333 ms 9016 KB Output is correct
23 Correct 288 ms 11348 KB Output is correct
24 Correct 2223 ms 9860 KB Output is correct
25 Correct 3631 ms 12792 KB Output is correct
26 Correct 3145 ms 12884 KB Output is correct
27 Correct 2803 ms 12404 KB Output is correct
28 Correct 603 ms 33720 KB Output is correct
29 Correct 1577 ms 37612 KB Output is correct
30 Correct 4151 ms 26844 KB Output is correct
31 Correct 3733 ms 22280 KB Output is correct
32 Correct 477 ms 10132 KB Output is correct
33 Correct 889 ms 10656 KB Output is correct
34 Correct 417 ms 30648 KB Output is correct
35 Correct 2389 ms 22664 KB Output is correct
36 Correct 4343 ms 35020 KB Output is correct
37 Correct 3544 ms 34288 KB Output is correct
38 Correct 3357 ms 34876 KB Output is correct
39 Correct 3015 ms 30368 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 436 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 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 348 KB Output is correct
12 Correct 852 ms 11568 KB Output is correct
13 Correct 274 ms 11148 KB Output is correct
14 Correct 1356 ms 8788 KB Output is correct
15 Correct 1520 ms 8712 KB Output is correct
16 Correct 1014 ms 7804 KB Output is correct
17 Correct 1504 ms 8888 KB Output is correct
18 Correct 1330 ms 8080 KB Output is correct
19 Correct 1066 ms 13188 KB Output is correct
20 Correct 3073 ms 7748 KB Output is correct
21 Correct 384 ms 5460 KB Output is correct
22 Correct 3323 ms 8968 KB Output is correct
23 Correct 291 ms 11312 KB Output is correct
24 Correct 2220 ms 9616 KB Output is correct
25 Correct 3561 ms 12852 KB Output is correct
26 Correct 3115 ms 12964 KB Output is correct
27 Correct 2784 ms 11944 KB Output is correct
28 Correct 561 ms 34376 KB Output is correct
29 Correct 1571 ms 36232 KB Output is correct
30 Correct 4153 ms 27412 KB Output is correct
31 Correct 3662 ms 22340 KB Output is correct
32 Correct 483 ms 10216 KB Output is correct
33 Correct 844 ms 10704 KB Output is correct
34 Correct 420 ms 31068 KB Output is correct
35 Correct 2371 ms 22580 KB Output is correct
36 Correct 4148 ms 35132 KB Output is correct
37 Correct 3391 ms 34540 KB Output is correct
38 Correct 3314 ms 34808 KB Output is correct
39 Correct 671 ms 61560 KB Output is correct
40 Correct 2404 ms 63652 KB Output is correct
41 Correct 5693 ms 49100 KB Output is correct
42 Correct 5171 ms 38996 KB Output is correct
43 Correct 808 ms 57772 KB Output is correct
44 Correct 664 ms 10892 KB Output is correct
45 Correct 2911 ms 30624 KB Output is correct
46 Correct 5142 ms 62720 KB Output is correct
47 Correct 5135 ms 62772 KB Output is correct
48 Correct 4847 ms 61892 KB Output is correct
49 Correct 1 ms 348 KB Output is correct