Submission #1094112

# Submission time Handle Problem Language Result Execution time Memory
1094112 2024-09-28T12:47:04 Z ASIXER Game (IOI13_game) C++17
80 / 100
1987 ms 57168 KB
#include "game.h"
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;

struct YNode {
    int lb, rb;
    int x_root = -1;
    int lc = -1, rc = -1;
} y_tree[1000000];
struct XNode {
    int lb, rb;
    ll val = 0;
    int lc = -1, rc = -1;
} x_tree[1000000];

int r, c, q;
int y_cnt = 0, x_cnt = 0;

int init_x_node(int lb, int rb) {
    XNode& x_node = x_tree[x_cnt];
    x_node.lb = lb, x_node.rb = rb;
    return x_cnt++;
}
int init_y_node(int lb, int rb) {
    YNode& y_node = y_tree[y_cnt];
    y_node.lb = lb, y_node.rb = rb;
    y_node.x_root = init_x_node(1, c);
    return y_cnt++;
}

ll x_query(XNode& x_node, int xl, int xr) {
    // cout << "x query " << x_node.lb << " ~ " << x_node.rb << " (qrange: " << xl << " ~ " << xr << ")\n";
    if (xr < x_node.lb || x_node.rb < xl) return 0;
    else if (xl <= x_node.lb && x_node.rb <= xr) return x_node.val;
    ll ret = 0;
    if (x_node.lc != -1) ret = gcd(ret, x_query(x_tree[x_node.lc], xl, xr));
    if (x_node.rc != -1) ret = gcd(ret, x_query(x_tree[x_node.rc], xl, xr));
    return ret;
}
ll y_query(YNode& y_node, int yl, int yr, int xl, int xr) {
    // cout << "y query " << y_node.lb << " ~ " << y_node.rb << " (qr: " << yl << " ~ " << yr << ")\n";
    if (yr < y_node.lb || y_node.rb < yl) return 0;
    else if (yl <= y_node.lb && y_node.rb <= yr) return x_query(x_tree[y_node.x_root], xl, xr);
    ll ret = 0;
    if (y_node.lc != -1) ret = gcd(ret, y_query(y_tree[y_node.lc], yl, yr, xl, xr));
    if (y_node.rc != -1) ret = gcd(ret, y_query(y_tree[y_node.rc], yl, yr, xl, xr));
    return ret;
}

void x_update(XNode& x_node, int x, ll val) {
    if (x < x_node.lb || x_node.rb < x) return;
    if (x_node.lb == x_node.rb) {
        x_node.val = val;
        return;
    }
    int mid = (x_node.lb + x_node.rb) >> 1;
    if (x <= mid) {
        if (x_node.lc == -1) {
            x_node.lc = init_x_node(x, x);
            XNode& lcn = x_tree[x_node.lc];
            x_update(lcn, x, val);
        } else {
            XNode& lcn = x_tree[x_node.lc];
            if (lcn.lb <= x && x <= lcn.rb) x_update(lcn, x, val);
            else {
                int mmid = (x_node.lb + mid) >> 1;
                int new_lc_idx = init_x_node(x_node.lb, mid);
                XNode& new_lcn = x_tree[new_lc_idx];
                if (lcn.rb <= mmid) new_lcn.lc = x_node.lc;
                else new_lcn.rc = x_node.lc;
                x_node.lc = new_lc_idx;
                x_update(new_lcn, x, val);
            }
        }
    } else {
        if (x_node.rc == -1) {
            x_node.rc = init_x_node(x, x);
            XNode& rcn = x_tree[x_node.rc];
            x_update(rcn, x, val);
        } else {
            XNode& rcn = x_tree[x_node.rc];
            if (rcn.lb <= x && x <= rcn.rb) x_update(rcn, x, val);
            else {
                int mmid = (mid + 1 + x_node.rb) >> 1;
                int new_rc_idx = init_x_node(mid + 1, x_node.rb);
                XNode& new_rcn = x_tree[new_rc_idx];
                if (rcn.rb <= mmid) new_rcn.lc = x_node.rc;
                else new_rcn.rc = x_node.rc;
                x_node.rc = new_rc_idx;
                x_update(new_rcn, x, val);
            }
        }
    }

    ll new_val = 0;
    if (x_node.lc != -1) new_val = gcd(new_val, x_tree[x_node.lc].val);
    if (x_node.rc != -1) new_val = gcd(new_val, x_tree[x_node.rc].val);
    // cout << "x upd result: " << x_node.lb << " " << x_node.rb << ": " << new_val << "\n";
    x_node.val = new_val;
}

void y_update(YNode& y_node, int y, int x, ll val) {
    // cout << "y upd " << y_node.lb << " ~ " << y_node.rb << " (p: " << y << ")\n";
    if (y < y_node.lb || y_node.rb < y) return;
    if (y_node.lb == y_node.rb) return x_update(x_tree[y_node.x_root], x, val);
    int mid = (y_node.lb + y_node.rb) >> 1;
    if (y <= mid) {
        if (y_node.lc == -1) y_node.lc = init_y_node(y_node.lb, mid);
        y_update(y_tree[y_node.lc], y, x, val);
    } else {
        if (y_node.rc == -1) y_node.rc = init_y_node(mid + 1, y_node.rb);
        y_update(y_tree[y_node.rc], y, x, val);
    }

    ll new_val = 0;
    if (y_node.lc != -1) new_val = gcd(new_val, x_query(x_tree[y_tree[y_node.lc].x_root], x, x));
    if (y_node.rc != -1) new_val = gcd(new_val, x_query(x_tree[y_tree[y_node.rc].x_root], x, x));
    x_update(x_tree[y_node.x_root], x, new_val);
}

void init(int rr, int cc) { r = rr, c = cc, init_y_node(1, r); }

void update(int y, int x, ll k) { y_update(y_tree[0], y + 1, x + 1, k); }

ll calculate(int y1, int x1, int y2, int x2) { return y_query(y_tree[0], y1 + 1, y2 + 1, x1 + 1, x2 + 1); }
# Verdict Execution time Memory Grader output
1 Correct 19 ms 43356 KB Output is correct
2 Correct 17 ms 43392 KB Output is correct
3 Correct 17 ms 43356 KB Output is correct
4 Correct 16 ms 43352 KB Output is correct
5 Correct 16 ms 43388 KB Output is correct
6 Correct 17 ms 43356 KB Output is correct
7 Correct 17 ms 43304 KB Output is correct
8 Correct 16 ms 43356 KB Output is correct
9 Correct 17 ms 43356 KB Output is correct
10 Correct 16 ms 43484 KB Output is correct
11 Correct 15 ms 43348 KB Output is correct
12 Correct 16 ms 43464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 43352 KB Output is correct
2 Correct 18 ms 43460 KB Output is correct
3 Correct 19 ms 43356 KB Output is correct
4 Correct 330 ms 51712 KB Output is correct
5 Correct 220 ms 51704 KB Output is correct
6 Correct 296 ms 49028 KB Output is correct
7 Correct 322 ms 48720 KB Output is correct
8 Correct 228 ms 49232 KB Output is correct
9 Correct 325 ms 48768 KB Output is correct
10 Correct 285 ms 48464 KB Output is correct
11 Correct 16 ms 43356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 43456 KB Output is correct
2 Correct 16 ms 43356 KB Output is correct
3 Correct 17 ms 43612 KB Output is correct
4 Correct 16 ms 43356 KB Output is correct
5 Correct 16 ms 43480 KB Output is correct
6 Correct 16 ms 43356 KB Output is correct
7 Correct 16 ms 43356 KB Output is correct
8 Correct 15 ms 43356 KB Output is correct
9 Correct 15 ms 43356 KB Output is correct
10 Correct 15 ms 43244 KB Output is correct
11 Correct 15 ms 43356 KB Output is correct
12 Correct 536 ms 51528 KB Output is correct
13 Correct 879 ms 47956 KB Output is correct
14 Correct 196 ms 48464 KB Output is correct
15 Correct 1008 ms 48404 KB Output is correct
16 Correct 128 ms 48180 KB Output is correct
17 Correct 510 ms 49608 KB Output is correct
18 Correct 754 ms 49744 KB Output is correct
19 Correct 678 ms 49748 KB Output is correct
20 Correct 609 ms 49164 KB Output is correct
21 Correct 20 ms 43352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 43356 KB Output is correct
2 Correct 18 ms 43484 KB Output is correct
3 Correct 18 ms 43352 KB Output is correct
4 Correct 18 ms 43356 KB Output is correct
5 Correct 16 ms 43356 KB Output is correct
6 Correct 17 ms 43352 KB Output is correct
7 Correct 16 ms 43352 KB Output is correct
8 Correct 17 ms 43356 KB Output is correct
9 Correct 22 ms 43344 KB Output is correct
10 Correct 17 ms 43356 KB Output is correct
11 Correct 16 ms 43356 KB Output is correct
12 Correct 353 ms 51792 KB Output is correct
13 Correct 213 ms 51540 KB Output is correct
14 Correct 291 ms 48968 KB Output is correct
15 Correct 339 ms 48720 KB Output is correct
16 Correct 226 ms 49236 KB Output is correct
17 Correct 315 ms 48980 KB Output is correct
18 Correct 301 ms 48464 KB Output is correct
19 Correct 541 ms 51536 KB Output is correct
20 Correct 840 ms 48076 KB Output is correct
21 Correct 174 ms 48640 KB Output is correct
22 Correct 991 ms 48580 KB Output is correct
23 Correct 129 ms 48192 KB Output is correct
24 Correct 494 ms 49556 KB Output is correct
25 Correct 757 ms 49780 KB Output is correct
26 Correct 688 ms 49812 KB Output is correct
27 Correct 654 ms 49324 KB Output is correct
28 Correct 308 ms 54476 KB Output is correct
29 Correct 822 ms 57168 KB Output is correct
30 Correct 1970 ms 50888 KB Output is correct
31 Correct 1904 ms 51028 KB Output is correct
32 Correct 337 ms 53072 KB Output is correct
33 Correct 484 ms 53076 KB Output is correct
34 Correct 144 ms 50768 KB Output is correct
35 Correct 578 ms 54352 KB Output is correct
36 Correct 990 ms 54864 KB Output is correct
37 Correct 845 ms 55124 KB Output is correct
38 Correct 828 ms 54612 KB Output is correct
39 Correct 748 ms 54936 KB Output is correct
40 Correct 18 ms 43356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 43352 KB Output is correct
2 Correct 16 ms 43356 KB Output is correct
3 Correct 16 ms 43420 KB Output is correct
4 Correct 16 ms 43356 KB Output is correct
5 Correct 20 ms 43356 KB Output is correct
6 Correct 16 ms 43352 KB Output is correct
7 Correct 16 ms 43356 KB Output is correct
8 Correct 16 ms 43352 KB Output is correct
9 Correct 17 ms 43356 KB Output is correct
10 Correct 17 ms 43356 KB Output is correct
11 Correct 16 ms 43352 KB Output is correct
12 Correct 321 ms 51924 KB Output is correct
13 Correct 217 ms 51536 KB Output is correct
14 Correct 303 ms 48976 KB Output is correct
15 Correct 324 ms 48724 KB Output is correct
16 Correct 222 ms 49296 KB Output is correct
17 Correct 339 ms 49004 KB Output is correct
18 Correct 286 ms 48460 KB Output is correct
19 Correct 549 ms 51512 KB Output is correct
20 Correct 851 ms 48064 KB Output is correct
21 Correct 188 ms 48464 KB Output is correct
22 Correct 973 ms 48388 KB Output is correct
23 Correct 128 ms 48324 KB Output is correct
24 Correct 480 ms 49560 KB Output is correct
25 Correct 730 ms 49744 KB Output is correct
26 Correct 707 ms 49952 KB Output is correct
27 Correct 616 ms 49232 KB Output is correct
28 Correct 306 ms 54600 KB Output is correct
29 Correct 831 ms 57008 KB Output is correct
30 Correct 1987 ms 50772 KB Output is correct
31 Correct 1866 ms 50932 KB Output is correct
32 Correct 359 ms 53072 KB Output is correct
33 Correct 456 ms 53064 KB Output is correct
34 Correct 164 ms 50768 KB Output is correct
35 Correct 556 ms 54568 KB Output is correct
36 Correct 983 ms 54952 KB Output is correct
37 Correct 831 ms 55148 KB Output is correct
38 Correct 748 ms 54696 KB Output is correct
39 Incorrect 198 ms 54424 KB Output isn't correct
40 Halted 0 ms 0 KB -