Submission #518537

#TimeUsernameProblemLanguageResultExecution timeMemory
518537blueGame (IOI13_game)C++17
63 / 100
2266 ms256004 KiB
#include "game.h"
#include <vector>
#include <algorithm>
using namespace std;

using ll = long long;

const int INF = 1'000'000'000;

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



struct col_tree
{
    int l;
    int r;

    ll g = 0;

    col_tree* left = NULL;
    col_tree* right = NULL;
};

col_tree* new_col_tree(int L, int R)
{
    col_tree* res = new col_tree;
    res->l = L;
    res->r = R;
    return res;
}

void update(col_tree* T, int I, ll V)
{
    if(T->l == T->r)
    {
        T->g = V;
    }
    else
    {
        if(I <= (T->l+T->r)/2)
        {
            if(T->left == NULL) T->left = new_col_tree(T->l, (T->l+T->r)/2);
            update(T->left, I, V);
        }
        else
        {
            if(T->right == NULL) T->right = new_col_tree((T->l+T->r)/2+1, T->r);
            update(T->right, I, V);
        }

        ll lg = (T->left == NULL) ? 0 : T->left->g;
        ll rg = (T->right == NULL) ? 0 : T->right->g;

        T->g = gcd2(lg, rg);
    }
}

ll range_gcd(col_tree* T, int L, int R)
{
    if(T == NULL) return 0;
    else if(R < T->l || T->r < L) return 0;
    else if(L <= T->l && T->r <= R) return T->g;
    else return gcd2(range_gcd(T->left, L, R), range_gcd(T->right, L, R));
}











struct row_tree
{
    col_tree v{0, INF-1, 0, NULL, NULL};

    row_tree* left = NULL;
    row_tree* right = NULL;
};

row_tree* new_row_tree()
{
    return new row_tree;
}
                           //ro
void update(row_tree* T, int I, int J, ll V, int l, int r)
{
    if(l == r)
    {
        update(&T->v, J, V);
    }
    else
    {
        if(I <= (l+r)/2)
        {
            if(T->left == NULL) T->left = new_row_tree();
            update(T->left, I, J, V, l, (l+r)/2);
        }
        else
        {
            if(T->right == NULL) T->right = new_row_tree();
            update(T->right, I, J, V, (l+r)/2+1, r);
        }

        ll lg = (T->left == NULL) ? 0 : range_gcd(&T->left->v, J, J);
        ll rg = (T->right == NULL) ? 0 : range_gcd(&T->right->v, J, J);

        update(&T->v, J, gcd2(lg, rg));
    }
}

void update(row_tree* T, int I, int J, ll V)
{
    update(T, I, J, V, 0, INF-1);
}


ll grid_gcd(row_tree* T, int R1, int R2, int C1, int C2, int l, int r)
{
    if(T == NULL) return 0;
    else if(R2 < l || r < R1) return 0;
    else if(R1 <= l && r <= R2) return range_gcd(&T->v, C1, C2);
    else return gcd2(grid_gcd(T->left, R1, R2, C1, C2, l, (l+r)/2), grid_gcd(T->right, R1, R2, C1, C2, (l+r)/2+1, r));
}

ll grid_gcd(row_tree* T, int R1, int R2, int C1, int C2)
{
    return grid_gcd(T, R1, R2, C1, C2, 0, INF-1);
}



row_tree ST;

void init(int R, int C)
{
    ;
}

void update(int P, int Q, long long K)
{
    update(&ST, P, Q, K);
}

ll calculate(int P, int Q, int U, int V)
{
    return grid_gcd(&ST, P, U, Q, V);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...