Submission #489111

# Submission time Handle Problem Language Result Execution time Memory
489111 2021-11-21T07:54:32 Z CYMario Game (IOI13_game) C++17
100 / 100
6851 ms 73220 KB
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef long long ll;
const int N = 300010;
//interaction header
#include "game.h"

ll rd()
{
    ll k = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        k = (k << 1) + (k << 3) + (c ^ 48);
        c = getchar();
    }
    return f > 0 ? k : -k;
}
void wr(ll x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        wr(x / 10);
    putchar(x % 10 + '0');
}
 
ll gcd(ll a, ll b)
{
    if (a < 0)
        a = -a;
    if (b < 0)
        b = -a;
    if (a == 0)
        return b;
    if (b == 0)
        return a;
    int r = 0;
    while (!((a & 1) || (b & 1)))
        a >>= 1, b >>= 1, r++;
 
    ll ret = 0;
    while (1)
    {
        while (!(a & 1))
            a >>= 1;
        while (!(b & 1))
            b >>= 1;
        if (a > b)
            a = a - b;
        else
            b = b - a;
        if (0 == a)
        {
            ret = b << r;
            break;
        }
        if (0 == b)
        {
            ret = a << r;
            break;
        }
    }
    return ret;
}
 
struct node
{
    node *l, *r;
    int pos, key, mn, mx;
    ll val, g;
    node(int position, ll value)
    {
        l = r = nullptr;
        mn = mx = pos = position;
        key = rand();
        val = g = value;
    }
    void pull()
    {
        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);
    }
};
// memory O(n)
struct treap
{
    node *root;
    treap()
    {
        root = nullptr;
    }
    void split(node *t, int pos, node *&l, node *&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->pull();
    }
    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->pull();
            return l;
        }
        r->l = merge(l, r->l);
        r->pull();
        return r;
    }
    bool find(int pos)
    {
        node *t = root;
        while (t)
        {
            if (t->pos == pos)
                return true;
            if (t->pos > pos)
                t = t->l;
            else
                t = t->r;
        }
        return false;
    }
    void upd(node *t, int pos, ll val)
    {
        if (t->pos == pos)
        {
            t->val = val;
            t->pull();
            return;
        }
        if (t->pos > pos)
            upd(t->l, pos, val);
        else
            upd(t->r, pos, val);
        t->pull();
    }
    void insert(int pos, ll val)
    { // set a_pos = val
        if (find(pos))
            upd(root, pos, val);
        else
        {
            node *l, *r;
            split(root, pos, l, r);
            root = merge(merge(l, new node(pos, val)), r);
        }
    }
    ll query(node *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 l, int r)
    { // gcd of a_i such that l <= i <= r
        if (!root)
            return 0;
        return query(root, l, r);
    }
    void print(node *t)
    {
        if (!t)
            return;
        print(t->l);
        wr(t->val), putchar('\n');
        print(t->r);
    }
};
// Dynamic 2D Query Tree From Shahjalal Shohag
// total memory along with treap = nlogn
struct ST
{
    ST *l, *r;
    treap t;
    int b, e;
    ST()
    {
        l = r = nullptr;
    }
    ST(int st, int en)
    {
        l = r = nullptr;
        b = st, e = en;
    }
    void fix(int pos)
    {
        ll val = 0;
        if (l)
            val = gcd(val, l->t.query(pos, pos));
        if (r)
            val = gcd(val, r->t.query(pos, pos));
        t.insert(pos, val);
    }
    void upd(int x, int y, ll val)
    { // set a[x][y] = val
        if (e < x || x < b)
            return;
        if (b == e)
        {
            t.insert(y, val);
            return;
        }
        if (b != e)
        {
            if (x <= (b + e) / 2)
            {
                if (!l)
                    l = new ST(b, (b + e) / 2);
                l->upd(x, y, val);
            }
            else
            {
                if (!r)
                    r = new ST((b + e) / 2 + 1, e);
                r->upd(x, y, val);
            }
        }
        fix(y);
    }
    // gcd of a[x][y] such that i <= x <= j && st <= y <= en
    ll query(int i, int j, int st, int en)
    { 
        if (e < i || j < b)
            return 0;
        if (i <= b && e <= j)
            return t.query(st, en);
        ll ans = 0;
        if (l)
            ans = gcd(ans, l->query(i, j, st, en));
        if (r)
            ans = gcd(ans, r->query(i, j, st, en));
        return ans;
    }
};
ST t;
 
void init(int R, int C)
{
    srand(time(NULL));
    t = ST(0, R - 1);
}
 
void update(int P, int Q, long long K)
{
    t.upd(P, Q, K);
}
 
long long calculate(int P, int Q, int U, int V)
{
    return t.query(P, U, Q, V);
}
 
void test_interaction()
{
    int n = rd(), m = rd();
    init(n, m);
    int q = rd();
    while(q--)
    {
        int op = rd();
        //op 1 : update single point
        if (op == 1)
        {
            int x = rd(), y = rd(); ll w = rd();
            update(x, y, w);
        }
        //op 2 : 2D gcd query
        else 
        {
            int xl = rd(), yl = rd(), xr = rd(), yr = rd();
            wr(calculate(xl, xr, yl, yr)), putchar('\n');
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 284 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 740 ms 11384 KB Output is correct
5 Correct 355 ms 11216 KB Output is correct
6 Correct 1463 ms 8728 KB Output is correct
7 Correct 1742 ms 8408 KB Output is correct
8 Correct 743 ms 7432 KB Output is correct
9 Correct 1640 ms 8540 KB Output is correct
10 Correct 1995 ms 8120 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 3 ms 280 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 2 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 252 KB Output is correct
12 Correct 983 ms 13324 KB Output is correct
13 Correct 3023 ms 7656 KB Output is correct
14 Correct 289 ms 5584 KB Output is correct
15 Correct 3326 ms 8960 KB Output is correct
16 Correct 638 ms 11444 KB Output is correct
17 Correct 1561 ms 9656 KB Output is correct
18 Correct 3021 ms 12772 KB Output is correct
19 Correct 2463 ms 12928 KB Output is correct
20 Correct 2906 ms 12464 KB Output is correct
21 Correct 0 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 2 ms 280 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 1 ms 280 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 725 ms 11464 KB Output is correct
13 Correct 329 ms 11184 KB Output is correct
14 Correct 1435 ms 8672 KB Output is correct
15 Correct 1693 ms 8440 KB Output is correct
16 Correct 735 ms 7468 KB Output is correct
17 Correct 1580 ms 8428 KB Output is correct
18 Correct 1936 ms 8052 KB Output is correct
19 Correct 999 ms 13388 KB Output is correct
20 Correct 2719 ms 7812 KB Output is correct
21 Correct 263 ms 5580 KB Output is correct
22 Correct 3187 ms 9072 KB Output is correct
23 Correct 570 ms 11312 KB Output is correct
24 Correct 1545 ms 9680 KB Output is correct
25 Correct 2941 ms 12976 KB Output is correct
26 Correct 2455 ms 13152 KB Output is correct
27 Correct 2911 ms 12280 KB Output is correct
28 Correct 680 ms 38980 KB Output is correct
29 Correct 1549 ms 41756 KB Output is correct
30 Correct 3783 ms 29748 KB Output is correct
31 Correct 3077 ms 24584 KB Output is correct
32 Correct 356 ms 10116 KB Output is correct
33 Correct 579 ms 10672 KB Output is correct
34 Correct 851 ms 35576 KB Output is correct
35 Correct 1957 ms 25400 KB Output is correct
36 Correct 4368 ms 39528 KB Output is correct
37 Correct 3282 ms 39704 KB Output is correct
38 Correct 4067 ms 39104 KB Output is correct
39 Correct 2717 ms 32972 KB Output is correct
40 Correct 0 ms 284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 2 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 280 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 2 ms 288 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 280 KB Output is correct
12 Correct 789 ms 11316 KB Output is correct
13 Correct 349 ms 11104 KB Output is correct
14 Correct 1663 ms 8600 KB Output is correct
15 Correct 1836 ms 8424 KB Output is correct
16 Correct 784 ms 7372 KB Output is correct
17 Correct 1731 ms 8488 KB Output is correct
18 Correct 2058 ms 8128 KB Output is correct
19 Correct 1006 ms 13240 KB Output is correct
20 Correct 3236 ms 7652 KB Output is correct
21 Correct 271 ms 5576 KB Output is correct
22 Correct 3425 ms 9040 KB Output is correct
23 Correct 600 ms 11372 KB Output is correct
24 Correct 1735 ms 9724 KB Output is correct
25 Correct 3190 ms 12704 KB Output is correct
26 Correct 2661 ms 12900 KB Output is correct
27 Correct 3071 ms 12380 KB Output is correct
28 Correct 754 ms 39008 KB Output is correct
29 Correct 1762 ms 41612 KB Output is correct
30 Correct 3888 ms 29712 KB Output is correct
31 Correct 3379 ms 24520 KB Output is correct
32 Correct 375 ms 10116 KB Output is correct
33 Correct 651 ms 10604 KB Output is correct
34 Correct 1028 ms 35444 KB Output is correct
35 Correct 2041 ms 25360 KB Output is correct
36 Correct 4115 ms 39524 KB Output is correct
37 Correct 3351 ms 39964 KB Output is correct
38 Correct 4276 ms 39124 KB Output is correct
39 Correct 1263 ms 71232 KB Output is correct
40 Correct 3248 ms 73220 KB Output is correct
41 Correct 6642 ms 55132 KB Output is correct
42 Correct 5855 ms 43468 KB Output is correct
43 Correct 2232 ms 67960 KB Output is correct
44 Correct 577 ms 10492 KB Output is correct
45 Correct 2789 ms 32912 KB Output is correct
46 Correct 6471 ms 72020 KB Output is correct
47 Correct 6402 ms 72092 KB Output is correct
48 Correct 6851 ms 71600 KB Output is correct
49 Correct 0 ms 208 KB Output is correct