# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
967320 |
2024-04-21T20:42:39 Z |
obokaman |
Game (IOI13_game) |
C++17 |
|
4581 ms |
133084 KB |
#include <iostream>
#include <vector>
#include <string>
#include "game.h"
using namespace std;
bool safecheck = false;
typedef long long ll;
struct Node {
Node *l, *r;
int x;
ll val;
int prio;
ll gcd;
};
struct RowNode {
RowNode *l, *r;
int x;
Node* row;
Node* thick_row;
int prio;
};
Node* row(RowNode* t) {
return t ? t->row : nullptr;
}
Node* thick_row(RowNode* t) {
return t ? t->thick_row : nullptr;
}
ll gcd(Node* t) {
return t ? t->gcd : -1;
}
ll val(Node* t) {
return t ? t->val : -1;
}
ll gcdrec(ll a, ll b) {
return a == 0 ? b : gcdrec(b%a, a);
}
ll gcd(ll a, ll b) {
if (a == -1 || b == -1) return a == -1 ? b : a;
return gcdrec(a, b);
}
Node* init_node(int x, ll val) {
Node* t = new Node;
t->l = t->r = nullptr;
t->x = x;
t->val = t->gcd = val;
t->prio = rand();
return t;
}
void update(Node* t) {
if (t) {
t->gcd = gcd(gcd(gcd(t->l), t->val), gcd(t->r));
}
}
// x in l
void split(Node* t, Node*& l, Node*& r, int x) {
if (!t) {
l = r = nullptr;
} else {
if (x < t->x) {
r = t;
split(t->l, l, t->l, x);
} else {
l = t;
split(t->r, t->r, r, x);
}
update(t);
}
}
void merge(Node*& t, Node* l, Node* r) {
if (!l || !r) {
t = l ? l : r;
} else {
if (l->prio > r->prio) {
t = l;
merge(t->r, t->r, r);
} else {
t = r;
merge(t->l, l, t->l);
}
update(t);
}
}
void update_x(Node*& t, int x, ll val) {
Node* t1, *t2, *t3;
split(t, t1, t2, x-1);
split(t2, t2, t3, x);
if (!t2) t2 = init_node(x, val);
else t2->val = t2->gcd = val;
merge(t, t1, t2);
merge(t, t, t3);
}
ll gcdRS(Node* t, int R, int S) {
Node* t1, *t2, *t3;
split(t, t1, t2, R-1);
split(t2, t2, t3, S);
ll res = gcd(t2);
merge(t, t1, t2);
merge(t, t, t3);
return res;
}
// gcd of [P, Q]x[R, S], knowing that t covers [a,b]
ll query(RowNode* t, int P, int Q, int R, int S, int a, int b) {
if (!t || Q < a || P > b || a > b) return -1;
if (P <= a && b <= Q) {
return gcdRS(thick_row(t), R, S);
} else {
ll gcd_x = (P <= t->x && t->x <= Q) ? gcdRS(row(t), R, S) : -1;
return gcd(gcd_x, gcd(query(t->l, P, Q, R, S, a, t->x-1),
query(t->r, P, Q, R, S, t->x+1, b)));
}
}
ll find_gcd(Node* t, int Q) {
if (!t) return -1;
if (t->x == Q) return t->val;
else {
return find_gcd(Q < t->x ? t->l : t->r, Q);
}
}
void pointUpdateThickrow(RowNode*& t, int Q) {
// We don't need to fully recalculate thickrow, because only x=Q had
// changed.
Node* t1, *t2, *t3;
split(t->thick_row, t1, t2, Q-1);
split(t2, t2, t3, Q);
ll newval = gcd( find_gcd(thick_row(t->l), Q),
gcd( find_gcd(thick_row(t->r), Q), find_gcd(t->row, Q)) );
if (!t2) t2 = init_node(Q, newval);
else t2->val = t2->gcd = newval;
merge(t->thick_row, t1, t2);
merge(t->thick_row, t->thick_row, t3);
}
RowNode* find(RowNode* t, int P) {
if (!t) {
return nullptr;
}
else if (P < t->x) {
return find(t->l, P);
}
else if (P > t->x) {
return find(t->r, P);
}
else {
return t;
}
}
void copy(Node* src, Node*& dst) {
if (!src) dst = nullptr;
else {
dst = new Node;
copy(src->l, dst->l);
copy(src->r, dst->r);
dst->x = src->x;
dst->val = src->val;
dst->prio = src->prio;
dst->gcd = src->gcd;
}
}
Node* join(Node* t1, Node* t2) {
Node* t;
if (!t1 && !t2) t = nullptr;
else if (!t1) copy(t2, t);
else if (!t2) copy(t1, t);
else {
if (t1->prio < t2->prio) swap(t1, t2);
t = new Node;
Node* t2l, *t2x, *t2r;
ll t2gcd = gcd(t2);
split(t2, t2l, t2x, t1->x-1);
split(t2x, t2x, t2r, t1->x);
t->x = t1->x;
t->val = gcd(val(t1), val(t2x));
t->gcd = gcd(gcd(t1), t2gcd);
t->prio = t1->prio;
t->l = join(t1->l, t2l);
t->r = join(t1->r, t2r);
merge(t2, t2l, t2x);
merge(t2, t2, t2r);
}
return t;
}
// P in l.
void split(RowNode* t, RowNode*& l, RowNode*& r, int P) {
if (!t) {
l = r = nullptr;
} else {
if (P < t->x) {
r = t;
split(t->l, l, t->l, P);
} else {
l = t;
split(t->r, t->r, r, P);
}
t->thick_row = join(join(thick_row(t->l), t->row), thick_row(t->r));
}
}
void set_rec(RowNode*& t, int P, int Q, ll val, int prio) {
if (!t) {
t = new RowNode;
t->l = t->r = nullptr;
t->x = P;
t->row = init_node(Q, val);
t->prio = prio;
} else {
if (prio > t->prio) {
// prio > 0 means we won't find P.
RowNode* child = t;
t = new RowNode;
t->x = P;
split(child, t->l, t->r, P);
t->row = init_node(Q, val);
// (Q,val) will be updated as part of pointUpdateThickrow
t->thick_row = join(thick_row(t->l), thick_row(t->r));
t->prio = prio;
} else {
if (P < t->x) set_rec(t->l, P, Q, val, prio);
else if (P == t->x) update_x(t->row, Q, val);
else set_rec(t->r, P, Q, val, prio);
}
}
pointUpdateThickrow(t, Q);
}
void set(RowNode*& t, int P, int Q, ll val) {
set_rec(t, P, Q, val, find(t, P) ? 0 : rand());
}
RowNode* globalt = nullptr;
int globalRows = -1;
void init(int R, int C) {
globalRows = R;
}
void update(int P, int Q, ll K) {
set(globalt, P, Q, K);
}
ll calculate(int P, int Q, int R, int S) {
ll res = query(globalt, P, R, Q, S, 0, globalRows-1);
return res == -1 ? 0 : res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
424 KB |
Output is correct |
4 |
Correct |
877 ms |
6960 KB |
Output is correct |
5 |
Correct |
322 ms |
7152 KB |
Output is correct |
6 |
Correct |
1008 ms |
4060 KB |
Output is correct |
7 |
Correct |
1255 ms |
4256 KB |
Output is correct |
8 |
Correct |
806 ms |
3544 KB |
Output is correct |
9 |
Correct |
1302 ms |
4384 KB |
Output is correct |
10 |
Correct |
1124 ms |
3408 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
939 ms |
12792 KB |
Output is correct |
13 |
Correct |
1890 ms |
9904 KB |
Output is correct |
14 |
Correct |
418 ms |
4992 KB |
Output is correct |
15 |
Correct |
2795 ms |
13864 KB |
Output is correct |
16 |
Correct |
271 ms |
18260 KB |
Output is correct |
17 |
Correct |
1734 ms |
14216 KB |
Output is correct |
18 |
Correct |
3366 ms |
18436 KB |
Output is correct |
19 |
Correct |
2493 ms |
19216 KB |
Output is correct |
20 |
Correct |
2874 ms |
18872 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
883 ms |
7140 KB |
Output is correct |
13 |
Correct |
325 ms |
7352 KB |
Output is correct |
14 |
Correct |
1046 ms |
3836 KB |
Output is correct |
15 |
Correct |
1252 ms |
3600 KB |
Output is correct |
16 |
Correct |
801 ms |
3184 KB |
Output is correct |
17 |
Correct |
1297 ms |
4444 KB |
Output is correct |
18 |
Correct |
1131 ms |
3412 KB |
Output is correct |
19 |
Correct |
921 ms |
12724 KB |
Output is correct |
20 |
Correct |
1904 ms |
10260 KB |
Output is correct |
21 |
Correct |
448 ms |
4872 KB |
Output is correct |
22 |
Correct |
2830 ms |
13700 KB |
Output is correct |
23 |
Correct |
282 ms |
18256 KB |
Output is correct |
24 |
Correct |
1738 ms |
14036 KB |
Output is correct |
25 |
Correct |
3339 ms |
18592 KB |
Output is correct |
26 |
Correct |
2516 ms |
19304 KB |
Output is correct |
27 |
Correct |
2853 ms |
18664 KB |
Output is correct |
28 |
Correct |
788 ms |
34128 KB |
Output is correct |
29 |
Correct |
1372 ms |
60372 KB |
Output is correct |
30 |
Correct |
1788 ms |
25872 KB |
Output is correct |
31 |
Correct |
1288 ms |
20688 KB |
Output is correct |
32 |
Correct |
456 ms |
8016 KB |
Output is correct |
33 |
Correct |
633 ms |
8724 KB |
Output is correct |
34 |
Correct |
540 ms |
47084 KB |
Output is correct |
35 |
Correct |
1734 ms |
30668 KB |
Output is correct |
36 |
Correct |
3374 ms |
58736 KB |
Output is correct |
37 |
Correct |
2607 ms |
53584 KB |
Output is correct |
38 |
Correct |
2867 ms |
53848 KB |
Output is correct |
39 |
Correct |
2312 ms |
39392 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
436 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 |
1 ms |
344 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 |
552 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
902 ms |
9592 KB |
Output is correct |
13 |
Correct |
324 ms |
9452 KB |
Output is correct |
14 |
Correct |
1020 ms |
6760 KB |
Output is correct |
15 |
Correct |
1263 ms |
6420 KB |
Output is correct |
16 |
Correct |
820 ms |
5628 KB |
Output is correct |
17 |
Correct |
1281 ms |
7288 KB |
Output is correct |
18 |
Correct |
1117 ms |
6192 KB |
Output is correct |
19 |
Correct |
911 ms |
15100 KB |
Output is correct |
20 |
Correct |
1920 ms |
10636 KB |
Output is correct |
21 |
Correct |
421 ms |
5572 KB |
Output is correct |
22 |
Correct |
2874 ms |
14192 KB |
Output is correct |
23 |
Correct |
278 ms |
18624 KB |
Output is correct |
24 |
Correct |
1717 ms |
14936 KB |
Output is correct |
25 |
Correct |
3415 ms |
19380 KB |
Output is correct |
26 |
Correct |
2510 ms |
20132 KB |
Output is correct |
27 |
Correct |
2899 ms |
19836 KB |
Output is correct |
28 |
Correct |
834 ms |
35084 KB |
Output is correct |
29 |
Correct |
1387 ms |
61596 KB |
Output is correct |
30 |
Correct |
1805 ms |
26932 KB |
Output is correct |
31 |
Correct |
1290 ms |
21468 KB |
Output is correct |
32 |
Correct |
455 ms |
9304 KB |
Output is correct |
33 |
Correct |
631 ms |
9964 KB |
Output is correct |
34 |
Correct |
514 ms |
47088 KB |
Output is correct |
35 |
Correct |
1725 ms |
30472 KB |
Output is correct |
36 |
Correct |
3332 ms |
58784 KB |
Output is correct |
37 |
Correct |
2561 ms |
52508 KB |
Output is correct |
38 |
Correct |
2824 ms |
52464 KB |
Output is correct |
39 |
Correct |
1160 ms |
70740 KB |
Output is correct |
40 |
Correct |
2277 ms |
133084 KB |
Output is correct |
41 |
Correct |
2629 ms |
52728 KB |
Output is correct |
42 |
Correct |
3038 ms |
41404 KB |
Output is correct |
43 |
Correct |
978 ms |
108192 KB |
Output is correct |
44 |
Correct |
654 ms |
10320 KB |
Output is correct |
45 |
Correct |
2281 ms |
39164 KB |
Output is correct |
46 |
Correct |
4406 ms |
129700 KB |
Output is correct |
47 |
Correct |
4434 ms |
107932 KB |
Output is correct |
48 |
Correct |
4581 ms |
125832 KB |
Output is correct |
49 |
Correct |
1 ms |
500 KB |
Output is correct |