Submission #152647

# Submission time Handle Problem Language Result Execution time Memory
152647 2019-09-08T19:11:14 Z dolphingarlic Game (IOI13_game) C++17
100 / 100
6172 ms 73508 KB
#include "game.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); }
int rnd() { return ((rand() % (1 << 15)) << 16) + (rand() % (1 << 15)); }

struct TreapNode {
    TreapNode *l, *r;
    int pos, key, mn, mx;
    ll val, g;
    
    TreapNode(int position, ll value) {
        l = r = nullptr;
        mn = mx = pos = position;
        key = rnd();
        val = g = value;
    }

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

struct Treap {
    TreapNode *root;

    Treap() {
        root = nullptr;
        srand(rnd());
    }

    void split(TreapNode *t, int pos, TreapNode *&l, TreapNode *&r) {
        if (t == nullptr) {
            l = r = nullptr;
            return;
        }
        if (t->pos < pos) {
            split(t->r, pos, l, r);
            t->r = l;
            l = t;
        } else {
            split(t->l, pos, l, r);
            t->l = r;
            r = t;
        }
        t->update();
    }

    TreapNode* merge(TreapNode *l, TreapNode *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 pos) {
        TreapNode *t = root;
        while (t) {
            if (t->pos == pos) return true;
            if (t->pos > pos) t = t->l;
            else t = t->r;
        }
        return false;
    }

    void update(TreapNode *t, int pos, ll val) {
        if (t->pos == pos) {
            t->val = val;
            t->update();
            return;
        }
        if (t->pos > pos) update(t->l, pos, val);
        else update(t->r, pos, val);
        t->update();
    }

    void insert(int pos, ll val) {
        if (find(pos)) update(root, pos, val);
        else {
            TreapNode *l, *r;
            split(root, pos, l, r);
            root = merge(merge(l, new TreapNode(pos, val)), r);
        }
    }

    ll query(TreapNode *t, int st, int en) {
        if (t->mx < st || en < t->mn) return 0;
        if (st <= t->mn && t->mx <= en) return t->g;
        
        ll ans = (st <= t->pos && t->pos <= en ? t->val : 0);
        if (t->l) ans = gcd(ans, query(t->l, st, en));
        if (t->r) ans = gcd(ans, query(t->r, st, en));
        return ans;
    }
    ll query(int st, int en) {
        if (!root) return 0;
        return query(root, st, en);
    }
};

struct Segtree {
    Segtree *l, *r;
    Treap treap;
    int lo, hi;

    Segtree() { l = r = nullptr; }
    Segtree(int st, int en) {
        l = r = nullptr;
        lo = st, hi = en;
    }

    void new_left() {
        if (!l) l = new Segtree(lo, (lo + hi) / 2);
    }
    void new_right() {
        if (!r) r = new Segtree((lo + hi) / 2 + 1, hi);
    }
    void fix(int pos) {
        ll val = 0;
        if (l) val = gcd(val, l->treap.query(pos, pos));
        if (r) val = gcd(val, r->treap.query(pos, pos));
        treap.insert(pos, val);
    }

    void update(int x, int y, ll val) {
        if (hi < x || x < lo) return;
        if (lo == hi) {
            treap.insert(y, val);
            return;
        }

        if (x <= (lo + hi) / 2) {
            new_left();
            l->update(x, y, val);
        } else {
            new_right();
            r->update(x, y, val);
        }
        fix(y);
    }

    ll query(int t, int b, int st, int en) {
        if (hi < t || b < lo) return 0;
        if (t <= lo && hi <= b) return treap.query(st, en);

        ll ans = 0;
        if (l) ans = gcd(ans, l->query(t, b, st, en));
        if (r) ans = gcd(ans, r->query(t, b, st, en));
        return ans;
    }
};

Segtree segtree;

void init(int R, int C) {
    srand(12341234);
    segtree = Segtree(0, R - 1);
}

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

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

// int main() {
//     int R, C, N;
//     cin >> R >> C >> N;
//     init(R, C);
//     while (N--) {
//         int t;
//         cin >> t;
//         if (t == 1) {
//             int P, Q;
//             ll K;
//             cin >> P >> Q >> K;
//             update(P, Q, K);
//         } else {
//             int P, Q, U, V;
//             cout << calculate(P, Q, U, V) << '\n';
//         }
//     }
// }

Compilation message

grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
  int res;
      ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 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 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 1151 ms 11500 KB Output is correct
5 Correct 439 ms 11256 KB Output is correct
6 Correct 1075 ms 8892 KB Output is correct
7 Correct 1251 ms 8552 KB Output is correct
8 Correct 659 ms 7544 KB Output is correct
9 Correct 1256 ms 8724 KB Output is correct
10 Correct 1115 ms 8212 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 1603 ms 13572 KB Output is correct
13 Correct 2571 ms 7828 KB Output is correct
14 Correct 337 ms 5656 KB Output is correct
15 Correct 2844 ms 9372 KB Output is correct
16 Correct 445 ms 11460 KB Output is correct
17 Correct 1432 ms 9856 KB Output is correct
18 Correct 2553 ms 13060 KB Output is correct
19 Correct 2160 ms 13116 KB Output is correct
20 Correct 2192 ms 12532 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 380 KB Output is correct
3 Correct 4 ms 464 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 524 KB Output is correct
9 Correct 3 ms 356 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 1107 ms 11512 KB Output is correct
13 Correct 440 ms 11408 KB Output is correct
14 Correct 1012 ms 8816 KB Output is correct
15 Correct 1151 ms 8760 KB Output is correct
16 Correct 686 ms 7680 KB Output is correct
17 Correct 1186 ms 8732 KB Output is correct
18 Correct 1098 ms 8316 KB Output is correct
19 Correct 1646 ms 13596 KB Output is correct
20 Correct 2628 ms 7784 KB Output is correct
21 Correct 334 ms 5652 KB Output is correct
22 Correct 2804 ms 9268 KB Output is correct
23 Correct 479 ms 11536 KB Output is correct
24 Correct 1441 ms 9836 KB Output is correct
25 Correct 2663 ms 13000 KB Output is correct
26 Correct 2169 ms 12992 KB Output is correct
27 Correct 2424 ms 12792 KB Output is correct
28 Correct 984 ms 39200 KB Output is correct
29 Correct 2707 ms 41776 KB Output is correct
30 Correct 3264 ms 30012 KB Output is correct
31 Correct 2900 ms 24776 KB Output is correct
32 Correct 526 ms 10316 KB Output is correct
33 Correct 785 ms 10564 KB Output is correct
34 Correct 903 ms 35676 KB Output is correct
35 Correct 1879 ms 25316 KB Output is correct
36 Correct 3867 ms 39812 KB Output is correct
37 Correct 3437 ms 39916 KB Output is correct
38 Correct 3390 ms 39408 KB Output is correct
39 Correct 2564 ms 33052 KB Output is correct
40 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 4 ms 376 KB Output is correct
3 Correct 4 ms 380 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 3 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 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 388 KB Output is correct
12 Correct 1112 ms 11640 KB Output is correct
13 Correct 444 ms 11312 KB Output is correct
14 Correct 1043 ms 8868 KB Output is correct
15 Correct 1171 ms 8640 KB Output is correct
16 Correct 659 ms 7516 KB Output is correct
17 Correct 1119 ms 8676 KB Output is correct
18 Correct 1112 ms 8296 KB Output is correct
19 Correct 1615 ms 13620 KB Output is correct
20 Correct 2571 ms 7940 KB Output is correct
21 Correct 341 ms 5776 KB Output is correct
22 Correct 2790 ms 9240 KB Output is correct
23 Correct 440 ms 11436 KB Output is correct
24 Correct 1436 ms 9940 KB Output is correct
25 Correct 2614 ms 12948 KB Output is correct
26 Correct 2249 ms 13264 KB Output is correct
27 Correct 2318 ms 12552 KB Output is correct
28 Correct 1037 ms 39232 KB Output is correct
29 Correct 2671 ms 41736 KB Output is correct
30 Correct 3337 ms 29900 KB Output is correct
31 Correct 2950 ms 24580 KB Output is correct
32 Correct 547 ms 10316 KB Output is correct
33 Correct 795 ms 10672 KB Output is correct
34 Correct 970 ms 35728 KB Output is correct
35 Correct 1993 ms 25448 KB Output is correct
36 Correct 3910 ms 39720 KB Output is correct
37 Correct 3151 ms 39892 KB Output is correct
38 Correct 3364 ms 39496 KB Output is correct
39 Correct 1631 ms 71400 KB Output is correct
40 Correct 4768 ms 73508 KB Output is correct
41 Correct 5183 ms 55352 KB Output is correct
42 Correct 4621 ms 43736 KB Output is correct
43 Correct 2001 ms 68272 KB Output is correct
44 Correct 751 ms 10884 KB Output is correct
45 Correct 2639 ms 33192 KB Output is correct
46 Correct 5785 ms 72192 KB Output is correct
47 Correct 6172 ms 72312 KB Output is correct
48 Correct 5788 ms 71796 KB Output is correct
49 Correct 2 ms 256 KB Output is correct