This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "game.h"
using namespace std;
int N, M;
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;
}
long long coord(int x, int y) {
return 1LL * (y - 1) * N + x;
}
long long pos, pos1, pos2;
struct Treap {
Treap* left;
Treap* right;
int prio;
long long pos, val, gcd;
};
Treap* emptyTreap = new Treap {NULL, NULL, 0, 0LL, 0LL, 0LL};
void recompute(Treap* root) {
if (root != emptyTreap)
root->gcd = gcd2(gcd2(root->left->gcd, root->right->gcd), root->val);
}
pair<Treap*, Treap*> split(Treap* root, long long pos) {
if (root == emptyTreap)
return {emptyTreap, emptyTreap};
pair<Treap*, Treap*> ans;
if (root->pos <= pos) {
ans.first = root;
pair<Treap*, Treap*>aux = split(root->right, pos);
ans.second = aux.second;
ans.first->right = aux.first;
recompute(ans.first);
} else {
ans.second = root;
pair<Treap*, Treap*>aux = split(root->left, pos);
ans.first = aux.first;
ans.second->left = aux.second;
recompute(ans.second);
}
return ans;
}
Treap* join(Treap* a, Treap* b) {
if (a == emptyTreap)
return b;
if (b == emptyTreap)
return a;
if (a->prio < b->prio) {
a->right = join(a->right, b);
recompute(a);
return a;
} else {
b->left = join(a, b->left);
recompute(b);
return b;
}
}
Treap* TreapUpdate(Treap* root, long long val) {
pair<Treap*, Treap*>aux1 = split(root, pos - 1);
pair<Treap*, Treap*>aux2 = split(aux1.second, pos);
Treap* newTreap = new Treap {emptyTreap, emptyTreap, rand(), pos, val, val};
return join(join(aux1.first, newTreap), aux2.second);
}
long long TreapQuery(Treap* root) {
pair<Treap*, Treap*>aux1 = split(root, pos1 - 1);
pair<Treap*, Treap*>aux2 = split(aux1.second, pos2);
long long answer = 0;
if (aux2.first != emptyTreap)
answer = aux2.first->gcd;
root = join(join(aux1.first, aux2.first), aux2.second);
return answer;
}
struct SegTree {
Treap* treap;
SegTree* left;
SegTree* right;
};
SegTree* emptyTree = new SegTree {emptyTreap, NULL, NULL};
SegTree* Root;
SegTree* SegTreeUpdate(SegTree* root, int l, int r, int P, int Q, long long K) {
if (root == emptyTree)
root = new SegTree {emptyTreap, emptyTree, emptyTree};
root->treap = TreapUpdate(root->treap, K);
if (l == r)
return root;
int mid = (l + r) / 2;
if (P <= mid)
root->left = SegTreeUpdate(root->left, l, mid, P, Q, K);
else
root->right = SegTreeUpdate(root->right, mid + 1, r, P, Q, K);
return root;
}
long long SegTreeQuery(SegTree* root, int l, int r, int P, int Q, int U, int V) {
if (U < l)
return 0;
if (r < P)
return 0;
if (root == emptyTree)
return 0;
if (P <= l && r <= U)
return TreapQuery(root->treap);
int mid = (l + r) / 2;
return gcd2(SegTreeQuery(root->left, l, mid, P, Q, U, V), SegTreeQuery(root->right, mid + 1, r, P, Q, U, V));
}
void init(int R, int C) {
srand(time(NULL));
Root = emptyTree;
N = R;
M = C;
}
void update(int P, int Q, long long K) {
pos = coord(P + 1, Q + 1);
Root = SegTreeUpdate(Root, 1, N, P + 1, Q + 1, K);
}
long long calculate(int P, int Q, int U, int V) {
pos1 = coord(P + 1, Q + 1);
pos2 = coord(U + 1, V + 1);
return SegTreeQuery(Root, 1, N, P + 1, Q + 1, U + 1, V + 1);
}
Compilation message (stderr)
grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
int res;
^~~
# | 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... |