#include "game.h"
#include <cstring>
#include <vector>
#include <algorithm>
#define W 5
long long **mat;
long long ****rmq;
struct myc {
int x;
long long y;
};
std::vector < myc > lin[2000], col[100000];
int nrlin, nrcol, lg2[100001];
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;
}
void init(int R, int C) {
mat = new long long*[R];
for (int i = 0; i < R; i++) {
mat[i] = new long long[C];
for (int j = 0; j < C; j++)
mat[i][j] = 0;
}
nrlin = R / W;
nrcol = C / W;
int n = std::max(nrlin, nrcol);
for (int i = 2; i <= n; i++)
if ((1 << (lg2[i - 1] + 1)) == i)
lg2[i] = lg2[i - 1] + 1;
else
lg2[i] = lg2[i - 1];
rmq = new long long***[nrlin];
for (int i = 0; i < nrlin; i++) {
rmq[i] = new long long**[nrcol];
for (int j = 0; j < nrcol; j++) {
rmq[i][j] = new long long*[lg2[nrlin] + 1];
for (int a = 0; a < lg2[nrlin] + 1; a++) {
rmq[i][j][a] = new long long[lg2[nrcol] + 1];
for (int b = 0; b < lg2[nrcol] + 1; b++)
rmq[i][j][a][b] = 0;
}
}
}
}
void update(int P, int Q, long long K) {
lin[P].push_back({Q, K});
col[Q].push_back({P, K});
mat[P][Q] = K;
P /= W;
Q /= W;
if (P < nrlin && Q < nrcol) {
rmq[P][Q][0][0] = 0;
for (int i = W * P; i < W * P + W; i++)
for (int j = W * Q; j < W * Q + W; j++)
rmq[P][Q][0][0] = gcd2(rmq[P][Q][0][0], mat[i][j]);
for (int i = P; i < nrlin; i++) {
for (int j = Q; j < nrcol; j++) {
for (int b = 1; (1 << b) <= j + 1; b++)
rmq[i][j][0][b] = gcd2(rmq[i][j][0][b - 1], rmq[i][j - (1 << (b - 1))][0][b - 1]);
for (int a = 1; (1 << a) <= i + 1; a++)
for (int b = 0; (1 << b) <= j + 1; b++)
rmq[i][j][a][b] = gcd2(rmq[i][j][a - 1][b], rmq[i - (1 << (a - 1))][j][a - 1][b]);
}
}
}
}
long long calculate(int p, int q, int u, int v) {
long long ans = 0;
int P = p / W + bool(p % W), Q = q / W + bool(q % W), U = u / W - 1 + bool(u % W == W - 1), V = v / W - 1 + bool(v % W == W - 1);
if (P > U)
for (int i = p; i <= u; i++)
for (auto &x : lin[i])
if (q <= x.x && x.x <= v)
ans = gcd2(x.y, ans);
else {
if (p % W)
for (int i = p; i % W; i++)
for (auto &x : lin[i])
if (q <= x.x && x.x <= v)
ans = gcd2(x.y, ans);
if (u % W != W - 1)
for (int i = u - u % W + 1; i <= u; i++)
for (auto &x : lin[i])
if (q <= x.x && x.x <= v)
ans = gcd2(x.y, ans);
}
if (Q > V)
for (int i = q; i <= v; i++)
for (auto &x : col[i])
if (p <= x.x && x.x <= u)
ans = gcd2(x.y, ans);
else {
if (q % W)
for (int i = q; i % W; i++)
for (auto &x : col[i])
if (p <= x.x && x.x <= u)
ans = gcd2(x.y, ans);
if (v % W != W - 1)
for (int i = v - v % W + 1; i <= v; i++)
for (auto &x : col[i])
if (p <= x.x && x.x <= u)
ans = gcd2(x.y, ans);
}
if (P <= U && Q <= V) {
int a = lg2[U - P + 1], b = lg2[V - Q + 1];
ans = gcd2(ans, gcd2(gcd2(gcd2(rmq[U][V][a][b], rmq[P + (1 << a) - 1][V][a][b]), rmq[U][Q + (1 << b) - 1][a][b]), rmq[P + (1 << a) - 1][Q + (1 << b) - 1][a][b]));
}
return ans;
}
Compilation message
grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
int res;
^~~
game.cpp: In function 'long long int calculate(int, int, int, int)':
game.cpp:82:8: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if (P > U)
^
game.cpp:99:8: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if (Q > V)
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |