#include <bits/stdc++.h>
#include "game.h"
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> struct treap{
struct node{
T pos, val, G, pri;
node *l, *r;
node(int pos_, T val_){
pos = pos_; val = val_; pri = rng();
l = r = NULL;
}
void cal(){ G = __gcd(val, __gcd((l ? l->G : 0), (r ? r->G : 0))); }
}*root;
treap(){ root = new node(0, 0); }
pair<node*, node*> split(node* t, int v){ // <= v on left
if (!t) return {t, t};
if (t->pos <= v){
auto p = split(t->r, v);
t->r = p.first; t->cal();
return {t, p.second};
}
else{
auto p = split(t->l, v);
t->l = p.second; t->cal();
return {p.first, t};
}
}
node* merge(node* l, node* r){ // l is before r
if (!l or !r) return l ? l : r;
if (l->pri < r->pri){
l->r = merge(l->r, r); l->cal();
return l;
}
else{
r->l = merge(l, r->l); r->cal();
return r;
}
}
void upd(int ind, T k){
auto [L, Rpart] = split(root, ind - 1);
auto [M, R] = split(Rpart, ind);
root = merge(merge(L, new node(ind, k)), R);
}
T qry(int l, int r){
auto [L, Rpart] = split(root, l - 1);
auto [M, R] = split(Rpart, r);
T res = M ? M->G : 0;
root = merge(merge(L, M), R);
return res;
}
};
template<class T> struct seg2d{
vector<treap<T>> seg{treap<T>(), treap<T>()}; vector<int> lc{0, 0}, rc{0, 0};
int ext(int i){
if (i) return i;
seg.push_back(treap<T>()); lc.push_back(0); rc.push_back(0);
return seg.size() - 1;
}
void upd(int ptX, int ptY, T v, int l = 0, int r = 1e9, int i = 1){
if (l == r){ seg[i].upd(ptY, v); return; }
int mid = (l + r) / 2;
if (ptX <= mid) upd(ptX, ptY, v, l, mid, lc[i] = ext(lc[i]));
else upd(ptX, ptY, v, mid + 1, r, rc[i] = ext(rc[i]));
seg[i].upd(ptY, __gcd(seg[lc[i]].qry(ptY, ptY), seg[rc[i]].qry(ptY, ptY)));
}
T qry(int qx1, int qy1, int qx2, int qy2, int l = 0, int r = 1e9, int i = 1){
if (!i or r < qx1 or l > qx2) return 0;
if (l >= qx1 and r <= qx2) return seg[i].qry(qy1, qy2);
int mid = (l + r) / 2;
return __gcd(qry(qx1, qy1, qx2, qy2, l, mid, lc[i]), qry(qx1, qy1, qx2, qy2, mid + 1, r, rc[i]));
}
};
seg2d<long long> ST;
void init(int r, int c){};
void update(int p, int q, long long k){ ST.upd(p, q, k); }
long long calculate(int p, int q, int u, int v){ return ST.qry(p, q, u, v); }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
6 ms |
596 KB |
Output is correct |
3 |
Correct |
5 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
5 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1393 ms |
28148 KB |
Output is correct |
5 |
Correct |
984 ms |
28080 KB |
Output is correct |
6 |
Correct |
2215 ms |
25360 KB |
Output is correct |
7 |
Correct |
2342 ms |
25204 KB |
Output is correct |
8 |
Correct |
1328 ms |
15416 KB |
Output is correct |
9 |
Correct |
2351 ms |
25316 KB |
Output is correct |
10 |
Correct |
1976 ms |
25108 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
5 ms |
468 KB |
Output is correct |
3 |
Correct |
5 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
6 ms |
432 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1588 ms |
27992 KB |
Output is correct |
13 |
Correct |
2901 ms |
24500 KB |
Output is correct |
14 |
Correct |
682 ms |
24872 KB |
Output is correct |
15 |
Correct |
2988 ms |
25168 KB |
Output is correct |
16 |
Correct |
1046 ms |
24896 KB |
Output is correct |
17 |
Correct |
2105 ms |
16000 KB |
Output is correct |
18 |
Correct |
3789 ms |
26272 KB |
Output is correct |
19 |
Correct |
3491 ms |
26464 KB |
Output is correct |
20 |
Correct |
2984 ms |
25936 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
468 KB |
Output is correct |
3 |
Correct |
5 ms |
476 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
5 ms |
432 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
5 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1462 ms |
28032 KB |
Output is correct |
13 |
Correct |
1006 ms |
27976 KB |
Output is correct |
14 |
Correct |
2248 ms |
25584 KB |
Output is correct |
15 |
Correct |
2270 ms |
25136 KB |
Output is correct |
16 |
Correct |
1292 ms |
15544 KB |
Output is correct |
17 |
Correct |
2332 ms |
25472 KB |
Output is correct |
18 |
Correct |
1899 ms |
24764 KB |
Output is correct |
19 |
Correct |
1719 ms |
28116 KB |
Output is correct |
20 |
Correct |
2815 ms |
24376 KB |
Output is correct |
21 |
Correct |
705 ms |
24880 KB |
Output is correct |
22 |
Correct |
2913 ms |
25032 KB |
Output is correct |
23 |
Correct |
980 ms |
24920 KB |
Output is correct |
24 |
Correct |
2121 ms |
15944 KB |
Output is correct |
25 |
Correct |
3809 ms |
26224 KB |
Output is correct |
26 |
Correct |
3286 ms |
26576 KB |
Output is correct |
27 |
Correct |
2755 ms |
25832 KB |
Output is correct |
28 |
Correct |
1113 ms |
44708 KB |
Output is correct |
29 |
Correct |
1997 ms |
47392 KB |
Output is correct |
30 |
Correct |
3670 ms |
34800 KB |
Output is correct |
31 |
Correct |
3331 ms |
33116 KB |
Output is correct |
32 |
Correct |
621 ms |
29432 KB |
Output is correct |
33 |
Correct |
941 ms |
29592 KB |
Output is correct |
34 |
Correct |
1274 ms |
41128 KB |
Output is correct |
35 |
Correct |
2021 ms |
28112 KB |
Output is correct |
36 |
Correct |
3863 ms |
45128 KB |
Output is correct |
37 |
Correct |
3111 ms |
45300 KB |
Output is correct |
38 |
Correct |
3013 ms |
44964 KB |
Output is correct |
39 |
Correct |
2651 ms |
37312 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
524 KB |
Output is correct |
3 |
Correct |
5 ms |
428 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
6 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
1356 ms |
28208 KB |
Output is correct |
13 |
Correct |
940 ms |
27936 KB |
Output is correct |
14 |
Correct |
2119 ms |
25388 KB |
Output is correct |
15 |
Correct |
2208 ms |
25328 KB |
Output is correct |
16 |
Correct |
1275 ms |
15508 KB |
Output is correct |
17 |
Correct |
2215 ms |
25144 KB |
Output is correct |
18 |
Correct |
1873 ms |
24988 KB |
Output is correct |
19 |
Correct |
1544 ms |
28028 KB |
Output is correct |
20 |
Correct |
2714 ms |
24348 KB |
Output is correct |
21 |
Correct |
649 ms |
24972 KB |
Output is correct |
22 |
Correct |
2778 ms |
25160 KB |
Output is correct |
23 |
Correct |
882 ms |
24924 KB |
Output is correct |
24 |
Correct |
2068 ms |
15956 KB |
Output is correct |
25 |
Correct |
3668 ms |
26268 KB |
Output is correct |
26 |
Correct |
3299 ms |
26380 KB |
Output is correct |
27 |
Correct |
2969 ms |
25816 KB |
Output is correct |
28 |
Correct |
1370 ms |
44552 KB |
Output is correct |
29 |
Correct |
2256 ms |
47268 KB |
Output is correct |
30 |
Correct |
3998 ms |
34812 KB |
Output is correct |
31 |
Correct |
3387 ms |
33200 KB |
Output is correct |
32 |
Correct |
642 ms |
29500 KB |
Output is correct |
33 |
Correct |
936 ms |
29424 KB |
Output is correct |
34 |
Correct |
1298 ms |
41224 KB |
Output is correct |
35 |
Correct |
2037 ms |
28272 KB |
Output is correct |
36 |
Correct |
3955 ms |
45208 KB |
Output is correct |
37 |
Correct |
3243 ms |
45284 KB |
Output is correct |
38 |
Correct |
3070 ms |
44768 KB |
Output is correct |
39 |
Correct |
1612 ms |
82732 KB |
Output is correct |
40 |
Correct |
3566 ms |
84804 KB |
Output is correct |
41 |
Correct |
5731 ms |
66504 KB |
Output is correct |
42 |
Correct |
5399 ms |
62760 KB |
Output is correct |
43 |
Correct |
1948 ms |
79500 KB |
Output is correct |
44 |
Correct |
998 ms |
52940 KB |
Output is correct |
45 |
Correct |
2784 ms |
37344 KB |
Output is correct |
46 |
Correct |
5455 ms |
83592 KB |
Output is correct |
47 |
Correct |
5365 ms |
83760 KB |
Output is correct |
48 |
Correct |
4863 ms |
83196 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |