제출 #722374

#제출 시각아이디문제언어결과실행 시간메모리
722374finn__Game (IOI13_game)C++17
0 / 100
2 ms340 KiB
#include <bits/stdc++.h>
#include "game.h"
using namespace std;

int const R = 1 << 30, C = 1 << 30;
//                     ^ really good programming language

struct NodeX
{
    unsigned xChildren, yRoot;
};

struct NodeY
{
    unsigned yChildren;
    long long g;
};

NodeX *root;

unsigned allocNode(size_t s) { return (unsigned)((char *)calloc(1, s) - (char *)root); }

char *getYRoot(void *z) { return ((char *)root + *((unsigned *)z + 1)); }

char *getLeftChild(void *z) { return ((char *)root + *(unsigned *)z); }

char *getRightChild(void *z) { return ((char *)root + *(unsigned *)z + 16); }

void updateY(int Q, long long K, NodeY *z, int C, int D)
{
    z->g = gcd(z->g, K);
    if (C == D)
        return;
    if (!z->yChildren)
        z->yChildren = allocNode(2 * sizeof(NodeY));
    if (Q <= (C + D) / 2)
        updateY(Q, K, (NodeY *)getLeftChild(z), C, (C + D) / 2);
    else
        updateY(Q, K, (NodeY *)getRightChild(z), (C + D) / 2 + 1, D);
}

void updateX(int P, int Q, long long K, NodeX *z, int A, int B, int C, int D)
{
    if (!z->yRoot)
        z->yRoot = allocNode(sizeof(NodeY));
    updateY(Q, K, (NodeY *)getYRoot(z), C, D);
    if (A == B)
        return;
    if (!z->xChildren)
        z->xChildren = allocNode(2 * sizeof(NodeX));
    if (P <= (A + B) / 2)
        updateX(P, Q, K, (NodeX *)getLeftChild(z), A, (A + B) / 2, C, D);
    else
        updateX(P, Q, K, (NodeX *)getRightChild(z), (A + B) / 2 + 1, B, C, D);
}

long long calculateY(int Q, int V, NodeY *z, int C, int D)
{
    if (D < Q || C > V)
        return 0;
    if (Q <= C && D <= V)
        return z->g;
    if (z->yChildren)
        return gcd(calculateY(Q, V, (NodeY *)getLeftChild(z), C, (C + D) / 2),
                   calculateY(Q, V, (NodeY *)getRightChild(z), (C + D) / 2 + 1, D));
    return 0;
}

long long calculateX(int P, int Q, int U, int V, NodeX *z, int A, int B, int C, int D)
{
    if (B < P || A > U)
        return 0;
    if (P <= A && B <= U)
        return z->yRoot ? calculateY(Q, V, (NodeY *)getYRoot(z), C, D) : 0;
    if (z->xChildren)
        return gcd(calculateX(P, Q, U, V, (NodeX *)getLeftChild(z), A, (A + B) / 2, C, D),
                   calculateX(P, Q, U, V, (NodeX *)getRightChild(z), (A + B) / 2 + 1, B, C, D));
    return 0;
}

void init(int R, int C) { root = (NodeX *)malloc(sizeof *root); }

void update(int P, int Q, long long K)
{
    updateX(P, Q, K, root, 0, R - 1, 0, C - 1);
}

long long calculate(int P, int Q, int U, int V)
{
    return calculateX(P, Q, U, V, root, 0, R - 1, 0, C - 1);
}
#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...