#include "game.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
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 SegTree {
const ll NEUTRAL = 0;
struct Node {
ll v;
Node* l, * r;
int skip_pos;
Node(ll a, Node* b, Node* c) : v(a), l(b), r(c) {skip_pos = -1;}
};
int size;
Node* root, *null;
void init(int n) {
size = 1;
while (size < n)
size *= 2;
null = new Node(0, nullptr, nullptr);
null->l = null->r = null;
root = null;
}
Node* set(int i, ll v, Node* x, int lx, int rx) {
if (x == null)
x = new Node(0, null, null);
if (lx == rx - 1) {
x->v = v;
return x;
}
int mid = (lx + rx) / 2;
if (x->skip_pos == -1 || x->skip_pos == i) {
x->skip_pos = i;
x->v = v;
return x;
}
else if (x->skip_pos != -2) {
if (x->skip_pos < mid) {
x->l = set(x->skip_pos, x->v, x->l, lx, mid);
}
else {
x->r = set(x->skip_pos, x->v, x->r, mid, rx);
}
x->skip_pos = -2;
}
if (i < mid) {
x->l = set(i, v, x->l, lx, mid);
}
else {
x->r = set(i, v, x->r, mid, rx);
}
x->v = gcd2(x->l->v, x->r->v);
return x;
}
void set(int i, ll v) {
root = set(i, v, root, 0, size);
}
ll calc(int l, int r, Node* x, int lx, int rx) {
if (l >= rx || lx >= r || x == null) {
return NEUTRAL;
}
if (x->skip_pos >= 0) {
int p = x->skip_pos;
if (p >= l && p < r)
return x->v;
else
return NEUTRAL;
}
if (l <= lx && rx <= r) {
ll ret = x->v;
return ret;
}
int mid = (lx + rx) / 2;
ll c1 = calc(l, r, x->l, lx, mid);
ll c2 = calc(l, r, x->r, mid, rx);
return gcd2(c1, c2);
}
ll calc(int l, int r) {
return calc(l, r, root, 0, size);
}
};
struct Tree2D {
const ll NEUTRAL = 0;
struct Node {
SegTree col;
Node *l, * r;
Node(int sz, Node* a, Node* b) : l(a), r(b) {col.init(sz);}
};
Node *root, *null;
int row_size;
int col_size;
void init(int n, int m) {
row_size = 1;
while (row_size < n)
row_size *= 2;
col_size = m;
null = new Node(col_size, nullptr, nullptr);
null->l = null->r = null;
root = null;
}
ll calc(int i_l, int i_r, int j_l, int j_r, Node* x, int lx, int rx) {
if (i_l >= rx || lx >= i_r || x == null)
return NEUTRAL;
if (i_l <= lx && rx <= i_r) {
return x->col.calc(j_l, j_r);
}
int mid = (lx + rx) / 2;
ll c1 = calc(i_l, i_r, j_l, j_r, x->l, lx, mid);
ll c2 = calc(i_l, i_r, j_l, j_r, x->r, mid, rx);
return gcd2(c1, c2);
}
ll calc(int i_l, int i_r, int j_l, int j_r) {
return calc(i_l, i_r, j_l, j_r, root, 0, row_size);
}
Node* set(int i, int j, ll v, Node* x, int lx, int rx) {
if (x == null)
x = new Node(col_size, null, null);
if (lx == rx - 1) {
x->col.set(j, v);
return x;
}
int mid = (lx + rx) / 2;
if (i < mid) {
x->l = set(i, j, v, x->l, lx, mid);
}
else {
x->r = set(i, j, v, x->r, mid, rx);
}
ll new_v = gcd2(x->l->col.calc(j, j + 1), x->r->col.calc(j, j + 1));
x->col.set(j, new_v);
return x;
}
void set(int i, int j, ll v) {
root = set(i, j, v, root, 0, row_size);
}
};
Tree2D board;
void init(int R, int C) {
board.init(R, C);
}
void update(int P, int Q, long long K) {
board.set(P, Q, K);
}
long long calculate(int P, int Q, int U, int V) {
return board.calc(P, U + 1, Q, V + 1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
318 ms |
9720 KB |
Output is correct |
5 |
Correct |
247 ms |
9944 KB |
Output is correct |
6 |
Correct |
312 ms |
6732 KB |
Output is correct |
7 |
Correct |
355 ms |
6588 KB |
Output is correct |
8 |
Correct |
235 ms |
4464 KB |
Output is correct |
9 |
Correct |
327 ms |
6520 KB |
Output is correct |
10 |
Correct |
302 ms |
6196 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
560 ms |
12584 KB |
Output is correct |
13 |
Correct |
961 ms |
6020 KB |
Output is correct |
14 |
Correct |
230 ms |
972 KB |
Output is correct |
15 |
Correct |
1112 ms |
7760 KB |
Output is correct |
16 |
Correct |
240 ms |
11536 KB |
Output is correct |
17 |
Correct |
509 ms |
7196 KB |
Output is correct |
18 |
Correct |
847 ms |
11872 KB |
Output is correct |
19 |
Correct |
702 ms |
11908 KB |
Output is correct |
20 |
Correct |
629 ms |
11392 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
394 ms |
9720 KB |
Output is correct |
13 |
Correct |
221 ms |
10004 KB |
Output is correct |
14 |
Correct |
306 ms |
6736 KB |
Output is correct |
15 |
Correct |
365 ms |
6520 KB |
Output is correct |
16 |
Correct |
276 ms |
4488 KB |
Output is correct |
17 |
Correct |
323 ms |
6600 KB |
Output is correct |
18 |
Correct |
299 ms |
6120 KB |
Output is correct |
19 |
Correct |
559 ms |
12572 KB |
Output is correct |
20 |
Correct |
975 ms |
5976 KB |
Output is correct |
21 |
Correct |
226 ms |
932 KB |
Output is correct |
22 |
Correct |
1125 ms |
7984 KB |
Output is correct |
23 |
Correct |
219 ms |
11560 KB |
Output is correct |
24 |
Correct |
506 ms |
7120 KB |
Output is correct |
25 |
Correct |
819 ms |
11880 KB |
Output is correct |
26 |
Correct |
698 ms |
12080 KB |
Output is correct |
27 |
Correct |
640 ms |
11456 KB |
Output is correct |
28 |
Correct |
391 ms |
62284 KB |
Output is correct |
29 |
Correct |
925 ms |
57320 KB |
Output is correct |
30 |
Correct |
2218 ms |
51236 KB |
Output is correct |
31 |
Correct |
2111 ms |
40624 KB |
Output is correct |
32 |
Correct |
445 ms |
10540 KB |
Output is correct |
33 |
Correct |
563 ms |
14232 KB |
Output is correct |
34 |
Correct |
393 ms |
51128 KB |
Output is correct |
35 |
Correct |
647 ms |
32992 KB |
Output is correct |
36 |
Correct |
1303 ms |
55244 KB |
Output is correct |
37 |
Correct |
1016 ms |
55540 KB |
Output is correct |
38 |
Correct |
959 ms |
54804 KB |
Output is correct |
39 |
Correct |
838 ms |
44920 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
224 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
311 ms |
9652 KB |
Output is correct |
13 |
Correct |
235 ms |
10032 KB |
Output is correct |
14 |
Correct |
305 ms |
6776 KB |
Output is correct |
15 |
Correct |
346 ms |
6416 KB |
Output is correct |
16 |
Correct |
234 ms |
4456 KB |
Output is correct |
17 |
Correct |
326 ms |
6512 KB |
Output is correct |
18 |
Correct |
307 ms |
6192 KB |
Output is correct |
19 |
Correct |
553 ms |
12588 KB |
Output is correct |
20 |
Correct |
957 ms |
5960 KB |
Output is correct |
21 |
Correct |
218 ms |
976 KB |
Output is correct |
22 |
Correct |
1062 ms |
7688 KB |
Output is correct |
23 |
Correct |
247 ms |
11736 KB |
Output is correct |
24 |
Correct |
515 ms |
7208 KB |
Output is correct |
25 |
Correct |
847 ms |
11900 KB |
Output is correct |
26 |
Correct |
738 ms |
11936 KB |
Output is correct |
27 |
Correct |
650 ms |
11364 KB |
Output is correct |
28 |
Correct |
415 ms |
62248 KB |
Output is correct |
29 |
Correct |
895 ms |
57340 KB |
Output is correct |
30 |
Correct |
2381 ms |
51168 KB |
Output is correct |
31 |
Correct |
2146 ms |
40580 KB |
Output is correct |
32 |
Correct |
423 ms |
10572 KB |
Output is correct |
33 |
Correct |
601 ms |
14268 KB |
Output is correct |
34 |
Correct |
375 ms |
51108 KB |
Output is correct |
35 |
Correct |
658 ms |
32900 KB |
Output is correct |
36 |
Correct |
1270 ms |
55236 KB |
Output is correct |
37 |
Correct |
1036 ms |
55456 KB |
Output is correct |
38 |
Correct |
1008 ms |
54860 KB |
Output is correct |
39 |
Correct |
575 ms |
124160 KB |
Output is correct |
40 |
Correct |
1402 ms |
107668 KB |
Output is correct |
41 |
Correct |
3013 ms |
101880 KB |
Output is correct |
42 |
Correct |
2847 ms |
78584 KB |
Output is correct |
43 |
Correct |
646 ms |
102504 KB |
Output is correct |
44 |
Correct |
585 ms |
10924 KB |
Output is correct |
45 |
Correct |
837 ms |
44844 KB |
Output is correct |
46 |
Correct |
1829 ms |
106532 KB |
Output is correct |
47 |
Correct |
1790 ms |
106692 KB |
Output is correct |
48 |
Correct |
1782 ms |
106260 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |