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 "game.h"
#include <bits/stdc++.h>
using namespace std;
long long gcd2(long long X, long long Y) {
return __gcd(X, Y);
}
struct node{
long long a = 0;
node *px1 = 0, *px2 = 0, *py1 = 0, *py2 = 0;
};
int R, C;
long long get(node *v){
if(!v) return 0;
return v->a;
}
node root;
void update_y(int y, long long K, node *v, int y1 = 0, int y2 = C - 1){
if(y1 == y2){
v->a = K;
}else{
int ym = (y1 + y2) / 2;
if(y <= ym){
if(!v->py1) v->py1 = new node();
update_y(y, K, v->py1, y1, ym);
}else{
if(!v->py2) v->py2 = new node();
update_y(y, K, v->py2, ym + 1, y2);
}
v->a = gcd2(get(v->py1), get(v->py2));
}
}
long long calc_y(int qy1, int qy2, node *v, int y1 = 0, int y2 = C - 1){
if(!v || qy1 > y2 || y1 > qy2) return 0;
if(qy1 <= y1 && y2 <= qy2) return v->a;
int ym = (y1 + y2) / 2;
long long a1 = 0, a2 = 0;
if(v->py1) a1 = calc_y(qy1, qy2, v->py1, y1, ym);
if(v->py2) a2 = calc_y(qy1, qy2, v->py2, ym + 1, y2);
return gcd2(a1, a2);
}
void update_x(int x, int y, long long K, node *v, int x1 = 0, int x2 = R - 1){
if(x1 == x2){
update_y(y, K, v);
return;
}
int xm = (x1 + x2) / 2;
if(x <= xm){
if(!v->px1) v->px1 = new node();
update_x(x, y, K, v->px1, x1, xm);
}else{
if(!v->px2) v->px2 = new node();
update_x(x, y, K, v->px2, xm + 1, x2);
}
long long A = gcd2(calc_y(y, y, v->px1), calc_y(y, y, v->px2));
update_y(y, A, v);
}
long long calc_x(int qx1, int qx2, int qy1, int qy2, node *v, int x1 = 0, int x2 = R - 1){
if(!v || qx1 > x2 || x1 > qx2) return 0;
if(qx1 <= x1 && x2 <= qx2) return calc_y(qy1, qy2, v);
int xm = (x1 + x2) / 2;
long long a1 = 0, a2 = 0;
if(v->px1) a1 = calc_x(qx1, qx2, qy1, qy2, v->px1, x1, xm);
if(v->px2) a2 = calc_x(qx1, qx2, qy1, qy2, v->px2, xm + 1, x2);
return gcd2(a1, a2);
}
void init(int R, int C) {
root = node(); ::R = R, ::C = C;
}
void update(int P, int Q, long long K) {
update_x(P, Q, K, &root);
}
long long calculate(int P, int Q, int U, int V) {
return calc_x(P, U, Q, V, &root);
}
# | 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... |