This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 07.06.2023 15:56:53 *
*************************************/
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
static char buf[250 << 20];
void* operator new(size_t s) {
static size_t i = sizeof buf;
assert(s < i);
return (void*)&buf[i -= s];
}
void operator delete(void*) {}
long long gcd2(long long X, long long Y) {
long long tmp;
while (X != Y && Y != 0) {
tmp = X;
X = Y;
Y = tmp % Y;
}
return X;
}
struct Node {
int l, r;
long long val;
Node *left, *right;
Node(int _l, int _r) {
l = _l; r = _r;
val = 0;
left = right = nullptr;
}
void update(int p, long long v) {
if (r - l == 1) {
val = v;
return;
}
int m = l + r >> 1;
Node *&child = p < m ? left : right;
if (child == nullptr) child = new Node(p, p + 1);
else if (child->r <= p || child->l > p) {
int x = l, y = r;
int a = min(child->l, p);
int b = max(child->r, p + 1);
while (true) {
int m = x + y >> 1;
if (a >= m) x = m;
else if (b <= m) y = m;
else break;
}
m = x + y >> 1;
Node *nxt = new Node(x, y);
if (p < m) nxt->right = child;
else nxt->left = child;
child = nxt;
}
child->update(p, v);
val = gcd2(left == nullptr ? 0 : left->val, right == nullptr ? 0 : right->val);
}
long long get(int u, int v) {
if (l >= v || r <= u) return 0;
if (u <= l && r <= v) return val;
return gcd2(left == nullptr ? 0 : left->get(u, v), right == nullptr ? 0 : right->get(u, v));
}
};
struct Node2D {
int l, r;
Node *val;
Node2D *left, *right;
Node2D(int _l, int _r, int _n) {
l = _l; r = _r;
val = new Node(0, _n);
left = right = nullptr;
}
void update(int p, int k, long long v) {
if (r - l == 1) {
val->update(k, v);
return;
}
int m = l + r >> 1;
Node2D *&child = p < m ? left : right;
if (child == nullptr) child = new Node2D(p, p + 1, val->r);
else if (child->r <= p || child->l > p) {
int x = l, y = r;
int a = min(child->l, p);
int b = max(child->r, p + 1);
while (true) {
int m = x + y >> 1;
if (a >= m) x = m;
else if (b <= m) y = m;
else break;
}
m = x + y >> 1;
Node2D *nxt = new Node2D(x, y, val->r);
if (p < m) nxt->right = child;
else nxt->left = child;
*nxt->val = *child->val;
child = nxt;
}
child->update(p, k, v);
val->update(k, gcd2(left == nullptr ? 0 : left->val->get(k, k + 1), right == nullptr ? 0 : right->val->get(k, k + 1)));
}
long long get(int u, int v, int x, int y) {
if (l >= v || r <= u) return 0;
if (u <= l && r <= v) return val->get(x, y);
return gcd2(left == nullptr ? 0 : left->get(u, v, x, y), right == nullptr ? 0 : right->get(u, v, x, y));
}
} *root;
void init(int R, int C) {
root = new Node2D(0, R, C);
}
void update(int P, int Q, long long K) {
root->update(P, Q, K);
}
long long calculate(int P, int Q, int U, int V) {
return root->get(P, U + 1, Q, V + 1);
}
#ifdef LOCAL
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#define fail(s, x...) do { \
fprintf(stderr, s "\n", ## x); \
exit(1); \
} while(0)
#define MAX_SIZE 1000000000
#define MAX_N 272000
int main() {
freopen("game.out", "w", stdout);
int R, C, N;
int P, Q, U, V;
long long K;
int i, type;
int res;
FILE *f = fopen("game.in", "r");
if (!f)
fail("Failed to open input file.");
res = fscanf(f, "%d", &R);
res = fscanf(f, "%d", &C);
res = fscanf(f, "%d", &N);
init(R, C);
for (i = 0; i < N; i++) {
res = fscanf(f, "%d", &type);
if (type == 1) {
res = fscanf(f, "%d%d%lld", &P, &Q, &K);
update(P, Q, K);
} else if (type == 2) {
res = fscanf(f, "%d%d%d%d", &P, &Q, &U, &V);
printf("%lld\n", calculate(P, Q, U, V));
} else
fail("Invalid action type in input file.");
}
fclose(f);
cerr << "end game\n";
return 0;
}
#endif
Compilation message (stderr)
game.cpp: In member function 'void Node::update(int, long long int)':
game.cpp:78:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | int m = l + r >> 1;
| ~~^~~
game.cpp:86:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int m = x + y >> 1;
| ~~^~~
game.cpp:91:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | m = x + y >> 1;
| ~~^~~
game.cpp: In member function 'void Node2D::update(int, int, long long int)':
game.cpp:121:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
121 | int m = l + r >> 1;
| ~~^~~
game.cpp:129:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
129 | int m = x + y >> 1;
| ~~^~~
game.cpp:134:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
134 | m = x + y >> 1;
| ~~^~~
# | 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... |