#include <bits/stdc++.h>
#include "game.h"
using namespace std;
const int mod = 1e9 + 9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct treapNode {
treapNode* l;
treapNode* r;
int key, prior;
int64_t val, gcd;
};
using ptr = treapNode*;
using pt = pair<ptr, ptr>;
ptr NIL = new treapNode{nullptr, nullptr, 0, -1, 0, 0};
int64_t gcd2(int64_t X, int64_t Y) {
int64_t tmp;
while (X != Y && Y != 0) {
tmp = X;
X = Y;
Y = tmp % Y;
}
return X;
}
void updateNode(ptr &x) {
if (x != NIL) {
x->gcd = gcd2(gcd2(x->l->gcd, x->val), x->r->gcd);
}
}
pt split(ptr &x, int k) {
if (x == NIL) {
return {NIL, NIL};
}
if (k < x->key) {
pt p = split(x->l, k);
x->l = p.second;
updateNode(x);
return {p.first, x};
}
pt p = split(x->r, k);
x->r = p.first;
updateNode(x);
return {x, p.second};
}
ptr join(ptr x, ptr y) {
if (x == NIL) {
return y;
}
if (y == NIL) {
return x;
}
if (y->prior < x->prior) {
x->r = join(x->r, y);
updateNode(x);
return x;
}
y->l = join(x, y->l);
updateNode(y);
return y;
}
bool check(ptr &x, int k, int64_t v) {
if (x == NIL) {
return false;
}
if (x->key == k) {
x->val = v;
updateNode(x);
return true;
}
if (k < x->key) {
bool ret = check(x->l, k, v);
updateNode(x);
return ret;
} else {
bool ret = check(x->r, k, v);
updateNode(x);
return ret;
}
}
ptr ins(ptr &x, int k, int64_t v) {
if (check(x, k, v)) {
return x;
}
pt p = split(x, k);
ptr newNode = new treapNode{NIL, NIL, k, rng() % mod, v, v};
return join(join(p.first, newNode), p.second);
}
int64_t queryY(ptr &x, int st, int dr) {
pt p1 = split(x, st - 1);
pt p2 = split(p1.second, dr);
int64_t ans = p2.first->gcd;
p1.second = join(p2.first, p2.second);
x = join(p1.first, p1.second);
return ans;
}
struct sgtNode {
ptr root;
sgtNode* l;
sgtNode* r;
sgtNode() : root(NIL), l(nullptr), r(nullptr) {}
} *root;
void update(sgtNode* &x, int lx, int rx, int X, int Y, int64_t v) {
if (!x) {
x = new sgtNode();
}
if (lx == rx) {
x->root = ins(x->root, Y, v);
return;
}
int mid = (lx + rx) / 2;
if (X <= mid) {
update(x->l, lx, mid, X, Y, v);
} else {
update(x->r, mid + 1, rx, X, Y, v);
}
int64_t gcd = 0;
if (x->l) {
gcd = queryY(x->l->root, Y, Y);
}
if (x->r) {
gcd = gcd2(gcd, queryY(x->r->root, Y, Y));
}
x->root = ins(x->root, Y, gcd);
}
int64_t queryX(sgtNode* &x, int lx, int rx, int x1, int x2, int y1, int y2) {
if (!x) {
return 0;
}
if (x1 <= lx && rx <= x2) {
return queryY(x->root, y1, y2);
}
int mid = (lx + rx) / 2;
int64_t ans = 0;
if (x1 <= mid) {
ans = gcd2(ans, queryX(x->l, lx, mid, x1, x2, y1, y2));
}
if (mid < x2) {
ans = gcd2(ans, queryX(x->r, mid + 1, rx, x1, x2, y1, y2));
}
return ans;
}
int N;
void init(int R, int C) {
N = R;
}
void update(int P, int Q, long long K) {
update(root, 0, N - 1, P, Q, K);
}
long long calculate(int P, int Q, int U, int V) {
return queryX(root, 0, N - 1, P, U, Q, V);
}
Compilation message
game.cpp: In function 'treapNode* ins(treapNode*&, int, int64_t)':
game.cpp:94:50: warning: narrowing conversion of '(rng.std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::operator()() % ((std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::result_type)((int)mod)))' from 'std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::result_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
94 | ptr newNode = new treapNode{NIL, NIL, k, rng() % mod, v, v};
| ~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
296 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
2 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
913 ms |
6784 KB |
Output is correct |
5 |
Correct |
478 ms |
6940 KB |
Output is correct |
6 |
Correct |
1409 ms |
3716 KB |
Output is correct |
7 |
Correct |
1656 ms |
3464 KB |
Output is correct |
8 |
Correct |
1072 ms |
3240 KB |
Output is correct |
9 |
Correct |
1552 ms |
3580 KB |
Output is correct |
10 |
Correct |
1378 ms |
3172 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
300 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1483 ms |
8392 KB |
Output is correct |
13 |
Correct |
3252 ms |
3424 KB |
Output is correct |
14 |
Correct |
420 ms |
1204 KB |
Output is correct |
15 |
Correct |
3542 ms |
4076 KB |
Output is correct |
16 |
Correct |
488 ms |
5920 KB |
Output is correct |
17 |
Correct |
2007 ms |
4360 KB |
Output is correct |
18 |
Correct |
3678 ms |
6472 KB |
Output is correct |
19 |
Correct |
3054 ms |
6580 KB |
Output is correct |
20 |
Correct |
2925 ms |
6072 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
944 ms |
6720 KB |
Output is correct |
13 |
Correct |
439 ms |
7020 KB |
Output is correct |
14 |
Correct |
1385 ms |
3752 KB |
Output is correct |
15 |
Correct |
1615 ms |
3480 KB |
Output is correct |
16 |
Correct |
1039 ms |
3208 KB |
Output is correct |
17 |
Correct |
1512 ms |
3492 KB |
Output is correct |
18 |
Correct |
1344 ms |
3276 KB |
Output is correct |
19 |
Correct |
1390 ms |
8436 KB |
Output is correct |
20 |
Correct |
3215 ms |
3368 KB |
Output is correct |
21 |
Correct |
438 ms |
1272 KB |
Output is correct |
22 |
Correct |
3492 ms |
4088 KB |
Output is correct |
23 |
Correct |
468 ms |
6076 KB |
Output is correct |
24 |
Correct |
2121 ms |
4336 KB |
Output is correct |
25 |
Correct |
3448 ms |
6252 KB |
Output is correct |
26 |
Correct |
3098 ms |
6480 KB |
Output is correct |
27 |
Correct |
2827 ms |
5944 KB |
Output is correct |
28 |
Correct |
1109 ms |
21172 KB |
Output is correct |
29 |
Correct |
2087 ms |
25608 KB |
Output is correct |
30 |
Correct |
4343 ms |
18440 KB |
Output is correct |
31 |
Correct |
3699 ms |
14640 KB |
Output is correct |
32 |
Correct |
542 ms |
2416 KB |
Output is correct |
33 |
Correct |
854 ms |
2840 KB |
Output is correct |
34 |
Correct |
724 ms |
22416 KB |
Output is correct |
35 |
Correct |
2204 ms |
12924 KB |
Output is correct |
36 |
Correct |
4115 ms |
22428 KB |
Output is correct |
37 |
Correct |
3460 ms |
22508 KB |
Output is correct |
38 |
Correct |
3345 ms |
22060 KB |
Output is correct |
39 |
Correct |
2767 ms |
17912 KB |
Output is correct |
40 |
Correct |
1 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
903 ms |
6220 KB |
Output is correct |
13 |
Correct |
410 ms |
6596 KB |
Output is correct |
14 |
Correct |
1339 ms |
3508 KB |
Output is correct |
15 |
Correct |
1515 ms |
3084 KB |
Output is correct |
16 |
Correct |
983 ms |
2884 KB |
Output is correct |
17 |
Correct |
1464 ms |
3372 KB |
Output is correct |
18 |
Correct |
1232 ms |
2708 KB |
Output is correct |
19 |
Correct |
1342 ms |
7932 KB |
Output is correct |
20 |
Correct |
2975 ms |
3112 KB |
Output is correct |
21 |
Correct |
401 ms |
836 KB |
Output is correct |
22 |
Correct |
3340 ms |
3960 KB |
Output is correct |
23 |
Correct |
445 ms |
5728 KB |
Output is correct |
24 |
Correct |
1937 ms |
4088 KB |
Output is correct |
25 |
Correct |
3252 ms |
5940 KB |
Output is correct |
26 |
Correct |
2878 ms |
6092 KB |
Output is correct |
27 |
Correct |
2667 ms |
5508 KB |
Output is correct |
28 |
Correct |
1050 ms |
20852 KB |
Output is correct |
29 |
Correct |
1949 ms |
24636 KB |
Output is correct |
30 |
Correct |
4290 ms |
17500 KB |
Output is correct |
31 |
Correct |
3750 ms |
13636 KB |
Output is correct |
32 |
Correct |
617 ms |
1728 KB |
Output is correct |
33 |
Correct |
909 ms |
2060 KB |
Output is correct |
34 |
Correct |
814 ms |
21880 KB |
Output is correct |
35 |
Correct |
2432 ms |
12308 KB |
Output is correct |
36 |
Correct |
4670 ms |
22156 KB |
Output is correct |
37 |
Correct |
3662 ms |
22400 KB |
Output is correct |
38 |
Correct |
3678 ms |
21820 KB |
Output is correct |
39 |
Correct |
1541 ms |
44996 KB |
Output is correct |
40 |
Correct |
3321 ms |
47336 KB |
Output is correct |
41 |
Correct |
6386 ms |
36176 KB |
Output is correct |
42 |
Correct |
5645 ms |
27576 KB |
Output is correct |
43 |
Correct |
1357 ms |
45228 KB |
Output is correct |
44 |
Correct |
833 ms |
1768 KB |
Output is correct |
45 |
Correct |
2850 ms |
17704 KB |
Output is correct |
46 |
Correct |
5642 ms |
45324 KB |
Output is correct |
47 |
Correct |
5700 ms |
45340 KB |
Output is correct |
48 |
Correct |
5407 ms |
44656 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |