Submission #1090810

# Submission time Handle Problem Language Result Execution time Memory
1090810 2024-09-18T19:35:52 Z vjudge1 Game (IOI13_game) C++17
100 / 100
2207 ms 73492 KB
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct Node {
    ll pos, key, mn, mx;
    ll val, g;
    Node *l, *r;

    Node(int posi, ll vali) {
        l = r = nullptr;
        key = rng();
        mn = mx = pos = posi;
        val = g = vali;
    }

    void update() {
        g = val;
        if(l) g = gcd(g, l->g);
        if(r) g = gcd(g, r->g);
        mn = (l ? l->mn : mn);
        mx = (r ? r->mx : mx);
    }
};

struct Treap {
    Node *root;

    Treap() { root = nullptr; }

    void split(Node *u, int x, Node *&l, Node *&r) {
        if(u == nullptr) {
            l = r = nullptr;
            return ;
        }

        if(u->pos < x) {
            split(u->r, x, l, r);
            u->r = l;
            l = u;
        } else {
            split(u->l, x, l, r);
            u->l = r;
            r = u;
        }

        u->update();
    }

    Node* merge(Node *l, Node *r) {
        if(!l || !r) return (l ? l : r);
        if(l->key < r->key) {
            l->r = merge(l->r, r);
            l->update();
            return l;
        } else {
            r->l = merge(l, r->l);
            r->update();
            return r;
        }
    }

    bool find(int id) {
        Node *u = root;
        while(u != nullptr) {
            if(u->pos == id) return 1;
            if(u->pos > id) {
                if(u->l == nullptr) return 0;
                u = u->l;
            }
            if(u->pos < id) {
                if(u->r == nullptr) return 0;
                u = u->r;
            }
        }
        return 0;
    }

    void update(Node *u, int p, ll v) {
        if(u->pos == p) {
            u->val = v;
            u->update();
            return ;
        }
        if(u->pos > p) update(u->l, p, v);
        else update(u->r, p, v);
        u->update();
    }

    void insert(int p, ll v) {
        if(find(p)) {
            update(root, p, v);
            return ;
        }
        Node *l, *r;
        split(root, p, l, r);
        root = merge(merge(l, new Node(p, v)), r);
    }

    ll query(Node *u, int tl, int tr) {
        if(u->mx < tl || tr < u->mn) return 0;
        if(tl <= u->mn && u->mx <= tr) return u->g;
        ll ans = (tl <= u->pos && u->pos <= tr ? u->val : 0);
        if(u->l) ans = gcd(ans, query(u->l, tl, tr));
        if(u->r) ans = gcd(ans, query(u->r, tl, tr));
        return ans;
    }

    ll query(int tl, int tr) { return (root ? query(root, tl, tr) : 0); }
};

struct SegTree {
    SegTree *l, *r;
    Treap t;
    int L, R;

    SegTree() { l = r = nullptr; }
    SegTree(int a, int b) : L(a), R(b), l(nullptr), r(nullptr) {}

    int tm() { return (L + R) / 2; }
    void add_l() { if(!l) l = new SegTree(L, tm()); }
    void add_r() { if(!r) r = new SegTree(tm()+1, R); }

    void merge(int p) {
        ll ans = 0;
        if(l) ans = gcd(ans, l->t.query(p, p));
        if(r) ans = gcd(ans, r->t.query(p, p));
        t.insert(p, ans);
    }

    void update(int x, int y, ll val) {
        if(x < L || R < x) return ;
        if(L == R) {
            t.insert(y, val);
            return ;
        }

        if(x <= tm()) {
            add_l();
            l->update(x, y, val);
        } else {
            add_r();
            r->update(x, y, val);
        }
        merge(y);
    }

    ll query(int x1, int x2, int y1, int y2) {
        if(x2 < L || R < x1) return 0;
        if(x1 <= L && R <= x2) return t.query(y1, y2);

        ll ans = 0;
        if(l) ans = gcd(ans, l->query(x1, x2, y1, y2));
        if(r) ans = gcd(ans, r->query(x1, x2, y1, y2));
        return ans;
    }
};

SegTree tree;

void init(int R, int C) {
    tree = SegTree(0, R-1);
}

void update(int P, int Q, ll K) {
    tree.update(P, Q, K);
}

ll calculate(int P, int Q, int U, int V) {
    return tree.query(P, U, Q, V);
}

Compilation message

game.cpp: In constructor 'SegTree::SegTree(int, int)':
game.cpp:128:12: warning: 'SegTree::R' will be initialized after [-Wreorder]
  128 |     int L, R;
      |            ^
game.cpp:126:14: warning:   'SegTree* SegTree::l' [-Wreorder]
  126 |     SegTree *l, *r;
      |              ^
game.cpp:131:5: warning:   when initialized here [-Wreorder]
  131 |     SegTree(int a, int b) : L(a), R(b), l(nullptr), r(nullptr) {}
      |     ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 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 0 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 367 ms 7572 KB Output is correct
5 Correct 183 ms 8020 KB Output is correct
6 Correct 468 ms 4688 KB Output is correct
7 Correct 519 ms 4416 KB Output is correct
8 Correct 302 ms 3668 KB Output is correct
9 Correct 517 ms 4480 KB Output is correct
10 Correct 501 ms 4436 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 1 ms 344 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 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 576 ms 10400 KB Output is correct
13 Correct 810 ms 4432 KB Output is correct
14 Correct 154 ms 1060 KB Output is correct
15 Correct 852 ms 5440 KB Output is correct
16 Correct 172 ms 8772 KB Output is correct
17 Correct 664 ms 5712 KB Output is correct
18 Correct 1229 ms 8960 KB Output is correct
19 Correct 1055 ms 9056 KB Output is correct
20 Correct 1024 ms 8672 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory 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 0 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 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 387 ms 7652 KB Output is correct
13 Correct 163 ms 7924 KB Output is correct
14 Correct 449 ms 4692 KB Output is correct
15 Correct 536 ms 4436 KB Output is correct
16 Correct 326 ms 3588 KB Output is correct
17 Correct 526 ms 4944 KB Output is correct
18 Correct 490 ms 4176 KB Output is correct
19 Correct 534 ms 10580 KB Output is correct
20 Correct 765 ms 4528 KB Output is correct
21 Correct 150 ms 852 KB Output is correct
22 Correct 866 ms 5620 KB Output is correct
23 Correct 177 ms 8816 KB Output is correct
24 Correct 651 ms 5712 KB Output is correct
25 Correct 1179 ms 9048 KB Output is correct
26 Correct 1024 ms 9296 KB Output is correct
27 Correct 1008 ms 8504 KB Output is correct
28 Correct 298 ms 33372 KB Output is correct
29 Correct 1030 ms 36544 KB Output is correct
30 Correct 1047 ms 27216 KB Output is correct
31 Correct 997 ms 20948 KB Output is correct
32 Correct 208 ms 1104 KB Output is correct
33 Correct 301 ms 1616 KB Output is correct
34 Correct 262 ms 33620 KB Output is correct
35 Correct 858 ms 17860 KB Output is correct
36 Correct 1639 ms 33992 KB Output is correct
37 Correct 1292 ms 34104 KB Output is correct
38 Correct 1343 ms 33356 KB Output is correct
39 Correct 1099 ms 26356 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 1 ms 348 KB Output is correct
12 Correct 408 ms 7640 KB Output is correct
13 Correct 178 ms 7880 KB Output is correct
14 Correct 455 ms 4692 KB Output is correct
15 Correct 528 ms 4468 KB Output is correct
16 Correct 310 ms 3668 KB Output is correct
17 Correct 510 ms 4700 KB Output is correct
18 Correct 501 ms 4180 KB Output is correct
19 Correct 547 ms 10456 KB Output is correct
20 Correct 839 ms 4400 KB Output is correct
21 Correct 144 ms 1104 KB Output is correct
22 Correct 836 ms 5456 KB Output is correct
23 Correct 176 ms 8788 KB Output is correct
24 Correct 651 ms 5780 KB Output is correct
25 Correct 1168 ms 9036 KB Output is correct
26 Correct 1014 ms 9180 KB Output is correct
27 Correct 971 ms 8540 KB Output is correct
28 Correct 288 ms 33300 KB Output is correct
29 Correct 923 ms 36372 KB Output is correct
30 Correct 1094 ms 27212 KB Output is correct
31 Correct 979 ms 20820 KB Output is correct
32 Correct 204 ms 1104 KB Output is correct
33 Correct 297 ms 1620 KB Output is correct
34 Correct 249 ms 33536 KB Output is correct
35 Correct 803 ms 17912 KB Output is correct
36 Correct 1674 ms 33872 KB Output is correct
37 Correct 1349 ms 33960 KB Output is correct
38 Correct 1384 ms 33508 KB Output is correct
39 Correct 418 ms 71320 KB Output is correct
40 Correct 1733 ms 73492 KB Output is correct
41 Correct 1594 ms 57304 KB Output is correct
42 Correct 1374 ms 43336 KB Output is correct
43 Correct 491 ms 71508 KB Output is correct
44 Correct 289 ms 1232 KB Output is correct
45 Correct 1037 ms 26360 KB Output is correct
46 Correct 2168 ms 71796 KB Output is correct
47 Correct 2157 ms 71680 KB Output is correct
48 Correct 2207 ms 71248 KB Output is correct
49 Correct 0 ms 348 KB Output is correct