#include "game.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll gcd(ll X, ll Y) {
ll tmp;
while (X != Y && Y != 0) {
tmp = X;
X = Y;
Y = tmp % Y;
}
return X;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Node {
int pos, key, mn, mx;
ll val, g;
Node *l, *r;
Node(int posi, ll vali) {
l = r = nullptr;
key = rng();
mn = mx = pos = posi;
val = g = vali;
}
void update() {
g = val;
if(l) g = gcd(g, l->g);
if(r) g = gcd(g, r->g);
mn = (l ? l->mn : mn);
mx = (r ? r->mx : mx);
}
};
struct Treap {
Node *root;
Treap() { root = nullptr; }
void split(Node *u, int x, Node *&l, Node *&r) {
if(u == nullptr) {
l = r = nullptr;
return ;
}
if(u->pos < x) {
split(u->r, x, l, r);
u->r = l;
l = u;
} else {
split(u->l, x, l, r);
u->l = r;
r = u;
}
u->update();
}
Node* merge(Node *l, Node *r) {
if(!l || !r) return (l ? l : r);
if(l->key < r->key) {
l->r = merge(l->r, r);
l->update();
return l;
} else {
r->l = merge(l, r->l);
r->update();
return r;
}
}
bool find(int id) {
Node *u = root;
while(u != nullptr) {
if(u->pos == id) return 1;
if(u->pos > id) {
if(u->l == nullptr) return 0;
u = u->l;
}
if(u->pos < id) {
if(u->r == nullptr) return 0;
u = u->r;
}
}
return 0;
}
void update(Node *u, int p, ll v) {
if(u->pos == p) {
u->val = v;
u->update();
return ;
}
if(u->pos > p) update(u->l, p, v);
else update(u->r, p, v);
u->update();
}
void insert(int p, ll v) {
if(find(p)) {
update(root, p, v);
return ;
}
Node *l, *r;
split(root, p, l, r);
root = merge(merge(l, new Node(p, v)), r);
}
ll query(Node *u, int tl, int tr) {
if(u->mx < tl || tr < u->mn) return 0;
if(tl <= u->mn && u->mx <= tr) return u->g;
ll ans = (tl <= u->pos && u->pos <= tr ? u->val : 0);
if(u->l) ans = gcd(ans, query(u->l, tl, tr));
if(u->r) ans = gcd(ans, query(u->r, tl, tr));
return ans;
}
ll query(int tl, int tr) { return (root ? query(root, tl, tr) : 0); }
};
struct SegTree {
SegTree *l, *r;
Treap t;
int L, R;
SegTree() { l = r = nullptr; }
SegTree(int a, int b) : L(a), R(b), l(nullptr), r(nullptr) {}
int tm() { return (L + R) / 2; }
void add_l() { if(!l) l = new SegTree(L, tm()); }
void add_r() { if(!r) r = new SegTree(tm()+1, R); }
void merge(int p) {
ll ans = 0;
if(l) ans = gcd(ans, l->t.query(p, p));
if(r) ans = gcd(ans, r->t.query(p, p));
t.insert(p, ans);
}
void update(int x, int y, ll val) {
if(x < L || R < x) return ;
if(L == R) {
t.insert(y, val);
return ;
}
if(x <= tm()) {
add_l();
l->update(x, y, val);
} else {
add_r();
r->update(x, y, val);
}
merge(y);
}
ll query(int x1, int x2, int y1, int y2) {
if(x2 < L || R < x1) return 0;
if(x1 <= L && R <= x2) return t.query(y1, y2);
ll ans = 0;
if(l) ans = gcd(ans, l->query(x1, x2, y1, y2));
if(r) ans = gcd(ans, r->query(x1, x2, y1, y2));
return ans;
}
};
SegTree tree;
void init(int R, int C) {
tree = SegTree(0, R-1);
}
void update(int P, int Q, ll K) {
tree.update(P, Q, K);
}
ll calculate(int P, int Q, int U, int V) {
return tree.query(P, U, Q, V);
}
Compilation message
game.cpp: In constructor 'SegTree::SegTree(int, int)':
game.cpp:128:12: warning: 'SegTree::R' will be initialized after [-Wreorder]
128 | int L, R;
| ^
game.cpp:126:14: warning: 'SegTree* SegTree::l' [-Wreorder]
126 | SegTree *l, *r;
| ^
game.cpp:131:5: warning: when initialized here [-Wreorder]
131 | SegTree(int a, int b) : L(a), R(b), l(nullptr), r(nullptr) {}
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
384 ms |
7068 KB |
Output is correct |
5 |
Correct |
162 ms |
7252 KB |
Output is correct |
6 |
Correct |
460 ms |
4084 KB |
Output is correct |
7 |
Correct |
520 ms |
3932 KB |
Output is correct |
8 |
Correct |
297 ms |
3152 KB |
Output is correct |
9 |
Correct |
518 ms |
3920 KB |
Output is correct |
10 |
Correct |
472 ms |
3608 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
545 ms |
9292 KB |
Output is correct |
13 |
Correct |
776 ms |
3784 KB |
Output is correct |
14 |
Correct |
143 ms |
856 KB |
Output is correct |
15 |
Correct |
840 ms |
4692 KB |
Output is correct |
16 |
Correct |
171 ms |
7248 KB |
Output is correct |
17 |
Correct |
681 ms |
4888 KB |
Output is correct |
18 |
Correct |
1188 ms |
7504 KB |
Output is correct |
19 |
Correct |
984 ms |
7756 KB |
Output is correct |
20 |
Correct |
996 ms |
6996 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
369 ms |
7116 KB |
Output is correct |
13 |
Correct |
157 ms |
7424 KB |
Output is correct |
14 |
Correct |
433 ms |
4180 KB |
Output is correct |
15 |
Correct |
501 ms |
3924 KB |
Output is correct |
16 |
Correct |
308 ms |
3160 KB |
Output is correct |
17 |
Correct |
484 ms |
3804 KB |
Output is correct |
18 |
Correct |
498 ms |
3412 KB |
Output is correct |
19 |
Correct |
542 ms |
9300 KB |
Output is correct |
20 |
Correct |
838 ms |
3848 KB |
Output is correct |
21 |
Correct |
138 ms |
856 KB |
Output is correct |
22 |
Correct |
816 ms |
4692 KB |
Output is correct |
23 |
Correct |
170 ms |
7260 KB |
Output is correct |
24 |
Correct |
640 ms |
4908 KB |
Output is correct |
25 |
Correct |
1190 ms |
7652 KB |
Output is correct |
26 |
Correct |
991 ms |
7740 KB |
Output is correct |
27 |
Correct |
1016 ms |
6996 KB |
Output is correct |
28 |
Correct |
280 ms |
28500 KB |
Output is correct |
29 |
Correct |
886 ms |
31568 KB |
Output is correct |
30 |
Correct |
1055 ms |
22812 KB |
Output is correct |
31 |
Correct |
937 ms |
17764 KB |
Output is correct |
32 |
Correct |
199 ms |
1104 KB |
Output is correct |
33 |
Correct |
315 ms |
1408 KB |
Output is correct |
34 |
Correct |
228 ms |
28756 KB |
Output is correct |
35 |
Correct |
805 ms |
15440 KB |
Output is correct |
36 |
Correct |
1550 ms |
29012 KB |
Output is correct |
37 |
Correct |
1283 ms |
29212 KB |
Output is correct |
38 |
Correct |
1343 ms |
28748 KB |
Output is correct |
39 |
Correct |
1056 ms |
22868 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
428 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
370 ms |
6948 KB |
Output is correct |
13 |
Correct |
172 ms |
7248 KB |
Output is correct |
14 |
Correct |
449 ms |
4016 KB |
Output is correct |
15 |
Correct |
512 ms |
3820 KB |
Output is correct |
16 |
Correct |
296 ms |
3124 KB |
Output is correct |
17 |
Correct |
480 ms |
3844 KB |
Output is correct |
18 |
Correct |
461 ms |
3400 KB |
Output is correct |
19 |
Correct |
522 ms |
9200 KB |
Output is correct |
20 |
Correct |
772 ms |
3720 KB |
Output is correct |
21 |
Correct |
138 ms |
848 KB |
Output is correct |
22 |
Correct |
810 ms |
4760 KB |
Output is correct |
23 |
Correct |
178 ms |
7240 KB |
Output is correct |
24 |
Correct |
644 ms |
4948 KB |
Output is correct |
25 |
Correct |
1152 ms |
7796 KB |
Output is correct |
26 |
Correct |
985 ms |
7712 KB |
Output is correct |
27 |
Correct |
969 ms |
7152 KB |
Output is correct |
28 |
Correct |
273 ms |
28496 KB |
Output is correct |
29 |
Correct |
864 ms |
31564 KB |
Output is correct |
30 |
Correct |
1061 ms |
22712 KB |
Output is correct |
31 |
Correct |
943 ms |
17488 KB |
Output is correct |
32 |
Correct |
213 ms |
1108 KB |
Output is correct |
33 |
Correct |
313 ms |
1436 KB |
Output is correct |
34 |
Correct |
229 ms |
28756 KB |
Output is correct |
35 |
Correct |
799 ms |
15544 KB |
Output is correct |
36 |
Correct |
1528 ms |
28928 KB |
Output is correct |
37 |
Correct |
1318 ms |
29268 KB |
Output is correct |
38 |
Correct |
1325 ms |
28548 KB |
Output is correct |
39 |
Correct |
436 ms |
60496 KB |
Output is correct |
40 |
Correct |
1659 ms |
62700 KB |
Output is correct |
41 |
Correct |
1659 ms |
47732 KB |
Output is correct |
42 |
Correct |
1511 ms |
36180 KB |
Output is correct |
43 |
Correct |
507 ms |
60752 KB |
Output is correct |
44 |
Correct |
303 ms |
1156 KB |
Output is correct |
45 |
Correct |
1070 ms |
22732 KB |
Output is correct |
46 |
Correct |
2160 ms |
61008 KB |
Output is correct |
47 |
Correct |
2084 ms |
61008 KB |
Output is correct |
48 |
Correct |
2120 ms |
60624 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |