이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "game.h"
using ll = long long;
template <class T> using Vec = std::vector<T>;
ll gcd2(ll X, ll Y) {
ll tmp;
while (X != Y && Y != 0) {
tmp = X;
X = Y;
Y = tmp % Y;
}
return X;
}
int Row, Column;
struct SegGCD {
struct Node {
ll gcd;
int left, right;
std::array<int, 2> ch;
Node(const int left, const int right): gcd(0), left(left), right(right), ch({-1, -1}) {}
};
Vec<Node> node;
SegGCD() {
node.emplace_back(0, Column);
}
void fetch(const int u) {
const int l = node[u].ch[0];
const int r = node[u].ch[1];
node[u].gcd = gcd2(l == -1 ? 0 : node[l].gcd, r == -1 ? 0 : node[r].gcd);
}
void assign(const int x, const ll v) {
int u = 0;
Vec<int> path;
while (node[u].right - node[u].left > 1) {
path.push_back(u);
const int mid = (node[u].left + node[u].right) / 2;
if (x < mid) {
if (node[u].ch[0] == -1) {
node.emplace_back(node[u].left, mid);
node[u].ch[0] = (int) node.size() - 1;
}
u = node[u].ch[0];
}
else {
if (node[u].ch[1] == -1) {
node.emplace_back(mid, node[u].right);
node[u].ch[1] = (int) node.size() - 1;
}
u = node[u].ch[1];
}
}
node[u].gcd = v;
while (!path.empty()) {
fetch(path.back());
path.pop_back();
}
}
void fold(const int u, const int l, const int r, ll& base) {
if (u == -1 or node[u].right <= l or r <= node[u].left) {
return;
}
if (l <= node[u].left and node[u].right <= r) {
base = gcd2(base, node[u].gcd);
return;
}
fold(node[u].ch[0], l, r, base);
fold(node[u].ch[1], l, r, base);
}
};
struct Seg2D {
struct Node {
SegGCD seg;
int left, right;
std::array<int, 2> ch;
Node(const int left, const int right) : seg(), left(left), right(right), ch({-1, -1}) {}
};
Vec<Node> node;
Seg2D() {
node.emplace_back(0, Row);
}
void assign(const int x, const int y, const ll v) {
int u = 0;;
while (node[u].right - node[u].left > 1) {
node[u].seg.assign(y, v);
const int mid = (node[u].left + node[u].right) / 2;
if (x < mid) {
if (node[u].ch[0] == -1) {
node.emplace_back(node[u].left, mid);
node[u].ch[0] = (int) node.size() - 1;
}
u = node[u].ch[0];
}
else {
if (node[u].ch[1] == -1) {
node.emplace_back(mid, node[u].right);
node[u].ch[1] = (int) node.size() - 1;
}
u = node[u].ch[1];
}
}
node[u].seg.assign(y, v);
}
void fold(const int u, const int l, const int r, const int l2, const int r2, ll& base) {
if (u == -1 or node[u].right <= l or r <= node[u].left) {
return;
}
if (l <= node[u].left and node[u].right <= r) {
node[u].seg.fold(0, l2, r2, base);
return;
}
fold(node[u].ch[0], l, r, l2, r2, base);
fold(node[u].ch[1], l, r, l2, r2, base);
}
};
Seg2D seg;
void init(int R, int C) {
Row = R;
Column = C;
seg = Seg2D();
}
void update(int P, int Q, ll K) {
seg.assign(P, Q, K);
}
ll calculate(int P, int Q, int U, int V) {
ll base = 0;
seg.fold(0, P, U + 1, Q, V + 1, base);
return base;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |