#include <iostream>
#include <utility>
#include "game.h"
#define LL long long
#define MAXN 22005
using namespace std;
LL gcd(LL X, LL Y) {
LL tmp;
while (X != Y && Y != 0) {
tmp = X;
X = Y;
Y = tmp % Y;
}
return X;
}
//begin treap
struct Node {
LL GCD, val; //gcd of subtree
int c, pri, l, r; //column, priority, children
};
class TreapForest {
public:
Node trp[MAXN * 125];
int ptr = 1;
void pushup(int t) {
trp[t].GCD = gcd(gcd(trp[trp[t].l].GCD, trp[trp[t].r].GCD), trp[t].val);
}
int addnode(LL v, int c) { //can also be used to make new treap in forest
trp[ptr].val = trp[ptr].GCD = v;
trp[ptr].pri = rand();
trp[ptr].c = c;
return(ptr++); //return node index
}
void Split(int t, int &l, int &r, int val) {
//split subtree t so c <= val to l, c > val to r
if (!t) {
l = r = 0;
return;
} else if (trp[t].c > val) {
Split(trp[t].l, l, trp[t].l, val);
r = t;
} else {
Split(trp[t].r, trp[t].r, r, val);
l = t;
}
pushup(t);
}
void Merge(int &t, int l, int r) {
//maxC(l) <= maxC(r)
//combine subtrees l, r into pointer t
if (!l || !r) {
t = max(l, r);
return;
} else if (trp[l].pri < trp[r].pri) { //bigger pri on top
Merge(trp[r].l, l, trp[r].l);
t = r;
} else {
Merge(trp[l].r, trp[l].r, r);
t = l;
}
pushup(t);
}
int Insert(int rt, LL val, int c) {
int NewNode = addnode(val, c); //assume rt does not already have key c
int l, r;
Split(rt, l, r, c);
Merge(l, l, NewNode);
Merge(r, l, r);
return(r); //new root
}
int Del(int rt, int c) { //Delete if exists
int l, r, trash;
Split(rt, l, r, c);
Split(l, l, trash, c-1);
Merge(r, l, r);
return(r);
}
pair <LL,int> Ask(int rt, int lo, int hi) { //range query
int l, mid, r;
LL res;
Split(rt, l, mid, lo-1);
Split(mid, mid, r, hi);
res = trp[mid].GCD;
Merge(mid, mid, r);
Merge(l, l, mid);
return(pair <LL,int> {res, l});
}
} Treap;
//end treap begin segtree
class LazyCreate {
int ST[MAXN * 125]; //store treap root, order by rows
int L[MAXN * 125], R[MAXN * 125], ptr = 2;
public:
void addnode(int par) {
L[par] = ptr++;
R[par] = ptr++;
}
void modify(int cur, LL val, int c) {
if (!ST[cur]) {
ST[cur] = Treap.addnode(val, c);
} else {
ST[cur] = Treap.Del(ST[cur], c);
ST[cur] = Treap.Insert(ST[cur], val, c);
}
}
LL upd(int r, int c, LL val, int Rl, int Rr, int cur) {
if (r < Rl || r > Rr) {
pair <LL,int> ret = Treap.Ask(ST[cur],c,c);
ST[cur] = ret.second;
return(ret.first);
} else if (Rl == Rr) {
modify(cur, val, c);
return(val);
} else {
if (!L[cur]) {addnode(cur);}
int mid = (Rl + Rr) >> 1;
LL a = upd(r, c, val, Rl, mid, L[cur]);
LL b = gcd(a, upd(r, c, val, mid+1, Rr, R[cur]));
modify(cur, b, c);
return(b);
}
}
LL ask(int Lr, int Hr, int Lc, int Hc, int Rl, int Rr, int cur) {
if (Hr < Rl || Lr > Rr || !cur) {return(0);}
else if (Lr <= Rl && Hr >= Rr) {
pair <LL,int> ret = Treap.Ask(ST[cur],Lc, Hc);
ST[cur] = ret.second;
return(ret.first);
} else {
int mid = (Rl + Rr) >> 1;
return(gcd( ask(Lr, Hr, Lc, Hc, Rl, mid, L[cur]),
ask(Lr, Hr, Lc, Hc, mid+1, Rr, R[cur])));
}
}
} ST;
int R,C;
void init(int r, int c) {
R = r, C = c;
}
void update(int P, int Q, long long K) {
ST.upd(P,Q,K, 0, R-1, 1);
}
long long calculate(int P, int Q, int U, int V) {
return(ST.ask(P,U,Q,V,0, R-1, 1));
}
Compilation message
grader.c: In function 'int main()':
grader.c:18:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
18 | int res;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1156 ms |
10232 KB |
Output is correct |
5 |
Correct |
601 ms |
10060 KB |
Output is correct |
6 |
Correct |
1522 ms |
7672 KB |
Output is correct |
7 |
Correct |
1680 ms |
7312 KB |
Output is correct |
8 |
Correct |
1154 ms |
7204 KB |
Output is correct |
9 |
Correct |
1636 ms |
7252 KB |
Output is correct |
10 |
Correct |
1468 ms |
7132 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1743 ms |
12024 KB |
Output is correct |
13 |
Correct |
3839 ms |
8580 KB |
Output is correct |
14 |
Correct |
582 ms |
9324 KB |
Output is correct |
15 |
Correct |
4265 ms |
9580 KB |
Output is correct |
16 |
Correct |
510 ms |
9208 KB |
Output is correct |
17 |
Correct |
2342 ms |
8456 KB |
Output is correct |
18 |
Correct |
4235 ms |
10580 KB |
Output is correct |
19 |
Correct |
3623 ms |
10756 KB |
Output is correct |
20 |
Correct |
3438 ms |
10084 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1171 ms |
10232 KB |
Output is correct |
13 |
Correct |
605 ms |
10012 KB |
Output is correct |
14 |
Correct |
1605 ms |
7580 KB |
Output is correct |
15 |
Correct |
1832 ms |
7344 KB |
Output is correct |
16 |
Correct |
1153 ms |
7028 KB |
Output is correct |
17 |
Correct |
1628 ms |
7288 KB |
Output is correct |
18 |
Correct |
1424 ms |
7116 KB |
Output is correct |
19 |
Correct |
1753 ms |
12024 KB |
Output is correct |
20 |
Correct |
3901 ms |
8488 KB |
Output is correct |
21 |
Correct |
625 ms |
9336 KB |
Output is correct |
22 |
Correct |
4308 ms |
9292 KB |
Output is correct |
23 |
Correct |
485 ms |
9072 KB |
Output is correct |
24 |
Correct |
2306 ms |
8532 KB |
Output is correct |
25 |
Correct |
4019 ms |
10616 KB |
Output is correct |
26 |
Correct |
3342 ms |
10616 KB |
Output is correct |
27 |
Correct |
3673 ms |
10104 KB |
Output is correct |
28 |
Correct |
1399 ms |
25068 KB |
Output is correct |
29 |
Correct |
2749 ms |
27688 KB |
Output is correct |
30 |
Correct |
5115 ms |
19748 KB |
Output is correct |
31 |
Correct |
4747 ms |
19600 KB |
Output is correct |
32 |
Correct |
867 ms |
19960 KB |
Output is correct |
33 |
Correct |
1221 ms |
19960 KB |
Output is correct |
34 |
Correct |
698 ms |
21624 KB |
Output is correct |
35 |
Correct |
2670 ms |
18428 KB |
Output is correct |
36 |
Correct |
5101 ms |
25720 KB |
Output is correct |
37 |
Correct |
4097 ms |
25884 KB |
Output is correct |
38 |
Correct |
4112 ms |
25224 KB |
Output is correct |
39 |
Correct |
3498 ms |
22268 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1152 ms |
10288 KB |
Output is correct |
13 |
Correct |
559 ms |
10048 KB |
Output is correct |
14 |
Correct |
1526 ms |
7528 KB |
Output is correct |
15 |
Correct |
1730 ms |
7288 KB |
Output is correct |
16 |
Correct |
1169 ms |
7052 KB |
Output is correct |
17 |
Correct |
1724 ms |
7448 KB |
Output is correct |
18 |
Correct |
1455 ms |
7016 KB |
Output is correct |
19 |
Correct |
1737 ms |
12020 KB |
Output is correct |
20 |
Correct |
4116 ms |
8440 KB |
Output is correct |
21 |
Correct |
611 ms |
9312 KB |
Output is correct |
22 |
Correct |
4424 ms |
9400 KB |
Output is correct |
23 |
Correct |
490 ms |
9080 KB |
Output is correct |
24 |
Correct |
2445 ms |
8480 KB |
Output is correct |
25 |
Correct |
4066 ms |
10512 KB |
Output is correct |
26 |
Correct |
3514 ms |
10616 KB |
Output is correct |
27 |
Correct |
3559 ms |
10148 KB |
Output is correct |
28 |
Correct |
1474 ms |
25024 KB |
Output is correct |
29 |
Correct |
2812 ms |
27776 KB |
Output is correct |
30 |
Correct |
5075 ms |
20016 KB |
Output is correct |
31 |
Correct |
4689 ms |
19572 KB |
Output is correct |
32 |
Correct |
861 ms |
19868 KB |
Output is correct |
33 |
Correct |
1260 ms |
19832 KB |
Output is correct |
34 |
Correct |
711 ms |
21500 KB |
Output is correct |
35 |
Correct |
2584 ms |
18808 KB |
Output is correct |
36 |
Correct |
5321 ms |
25840 KB |
Output is correct |
37 |
Correct |
3984 ms |
25860 KB |
Output is correct |
38 |
Correct |
4002 ms |
25240 KB |
Output is correct |
39 |
Correct |
2025 ms |
41100 KB |
Output is correct |
40 |
Correct |
4424 ms |
43364 KB |
Output is correct |
41 |
Correct |
7614 ms |
34444 KB |
Output is correct |
42 |
Correct |
6910 ms |
33088 KB |
Output is correct |
43 |
Correct |
1429 ms |
37692 KB |
Output is correct |
44 |
Correct |
1380 ms |
31788 KB |
Output is correct |
45 |
Correct |
3491 ms |
22368 KB |
Output is correct |
46 |
Correct |
8000 ms |
41868 KB |
Output is correct |
47 |
Correct |
7057 ms |
41824 KB |
Output is correct |
48 |
Correct |
6718 ms |
41460 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |