Submission #1090811

# Submission time Handle Problem Language Result Execution time Memory
1090811 2024-09-18T19:39:24 Z vjudge1 Game (IOI13_game) C++17
100 / 100
2125 ms 62832 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 {
    int 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 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 344 KB Output is correct
12 Correct 0 ms 600 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 390 ms 7140 KB Output is correct
5 Correct 163 ms 7224 KB Output is correct
6 Correct 445 ms 4176 KB Output is correct
7 Correct 512 ms 3912 KB Output is correct
8 Correct 304 ms 3148 KB Output is correct
9 Correct 493 ms 3924 KB Output is correct
10 Correct 486 ms 3588 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 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 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 540 ms 9268 KB Output is correct
13 Correct 796 ms 3664 KB Output is correct
14 Correct 149 ms 848 KB Output is correct
15 Correct 836 ms 4692 KB Output is correct
16 Correct 168 ms 7252 KB Output is correct
17 Correct 642 ms 4944 KB Output is correct
18 Correct 1135 ms 7572 KB Output is correct
19 Correct 970 ms 7712 KB Output is correct
20 Correct 965 ms 6992 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory 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 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 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 365 ms 6980 KB Output is correct
13 Correct 163 ms 7252 KB Output is correct
14 Correct 442 ms 4196 KB Output is correct
15 Correct 512 ms 3756 KB Output is correct
16 Correct 288 ms 3152 KB Output is correct
17 Correct 499 ms 3928 KB Output is correct
18 Correct 494 ms 3412 KB Output is correct
19 Correct 536 ms 9196 KB Output is correct
20 Correct 764 ms 3668 KB Output is correct
21 Correct 148 ms 884 KB Output is correct
22 Correct 845 ms 4576 KB Output is correct
23 Correct 168 ms 7208 KB Output is correct
24 Correct 654 ms 4944 KB Output is correct
25 Correct 1149 ms 7508 KB Output is correct
26 Correct 963 ms 7756 KB Output is correct
27 Correct 970 ms 7048 KB Output is correct
28 Correct 274 ms 28496 KB Output is correct
29 Correct 920 ms 31568 KB Output is correct
30 Correct 1050 ms 22872 KB Output is correct
31 Correct 933 ms 17492 KB Output is correct
32 Correct 196 ms 1104 KB Output is correct
33 Correct 298 ms 1364 KB Output is correct
34 Correct 258 ms 28752 KB Output is correct
35 Correct 782 ms 15484 KB Output is correct
36 Correct 1545 ms 29008 KB Output is correct
37 Correct 1277 ms 29264 KB Output is correct
38 Correct 1325 ms 28500 KB Output is correct
39 Correct 1056 ms 22728 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory 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 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 386 ms 6996 KB Output is correct
13 Correct 162 ms 7248 KB Output is correct
14 Correct 442 ms 4396 KB Output is correct
15 Correct 512 ms 3924 KB Output is correct
16 Correct 294 ms 3148 KB Output is correct
17 Correct 481 ms 3924 KB Output is correct
18 Correct 495 ms 3412 KB Output is correct
19 Correct 547 ms 9296 KB Output is correct
20 Correct 789 ms 4052 KB Output is correct
21 Correct 159 ms 848 KB Output is correct
22 Correct 838 ms 4684 KB Output is correct
23 Correct 171 ms 7248 KB Output is correct
24 Correct 650 ms 4916 KB Output is correct
25 Correct 1172 ms 7536 KB Output is correct
26 Correct 959 ms 8020 KB Output is correct
27 Correct 987 ms 7256 KB Output is correct
28 Correct 281 ms 28496 KB Output is correct
29 Correct 889 ms 31672 KB Output is correct
30 Correct 1061 ms 22868 KB Output is correct
31 Correct 936 ms 17660 KB Output is correct
32 Correct 203 ms 1108 KB Output is correct
33 Correct 325 ms 1536 KB Output is correct
34 Correct 241 ms 29028 KB Output is correct
35 Correct 818 ms 15600 KB Output is correct
36 Correct 1592 ms 29104 KB Output is correct
37 Correct 1259 ms 29300 KB Output is correct
38 Correct 1318 ms 28496 KB Output is correct
39 Correct 412 ms 60688 KB Output is correct
40 Correct 1716 ms 62832 KB Output is correct
41 Correct 1653 ms 47888 KB Output is correct
42 Correct 1438 ms 36140 KB Output is correct
43 Correct 483 ms 60752 KB Output is correct
44 Correct 302 ms 1172 KB Output is correct
45 Correct 1027 ms 22696 KB Output is correct
46 Correct 2125 ms 61148 KB Output is correct
47 Correct 2080 ms 61164 KB Output is correct
48 Correct 2074 ms 60744 KB Output is correct
49 Correct 0 ms 348 KB Output is correct