#include "game.h"
#include<bits/stdc++.h>
#define el '\n'
#define fi first
#define sc second
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
using ll=long long;
int m, n;
const int N = 5e6;
ll gcd(ll a, ll b)
{
if (!b) return a;
else return gcd(b, a % b);
}
ll ss[N];
int L[N], R[N], root[N], lazy[N];
int cur = 1;
void down(int id, int l, int r)
{
int mid=(l+r)>>1;
if(lazy[id]<=mid)
{
L[id] = ++cur;
ss[L[id]] = ss[id];
lazy[L[id]] = lazy[id];
}
else
{
R[id] = ++cur;
ss[R[id]] = ss[id];
lazy[R[id]] = lazy[id];
}
lazy[id] = 0;
}
void update1(int id, int l, int r, int pos, ll val)
{
if(l==r)
{
ss[id]=val;
return;
}
if(lazy[id])
{
if(lazy[id] == pos) ss[id] = val;
else down(id, l, r);
}
int mid = (l+r)>>1;
if(pos <= mid)
{
if(!L[id])
{
L[id] = ++cur;
ss[L[id]] = val;
lazy[L[id]] = pos;
}
else update1(L[id], l, mid, pos, val);
}
else
{
if(!R[id])
{
R[id] = ++cur;
ss[R[id]] = val;
lazy[R[id]] = pos;
}
else update1(R[id], mid + 1, r, pos, val);
}
ll s1 = 0, s2 = 0;
if(L[id]) s1 = ss[L[id]];
if(R[id]) s2 = ss[R[id]];
ss[id] = gcd(s1, s2);
}
ll get1(int id, int l, int r, int a, int b)
{
if(a > r || l > b) return 0;
if(a <= l && r <= b) return ss[id];
if(lazy[id])
{
if(a <= lazy[id] && lazy[id] <= b) return ss[id];
else return 0;
}
int mid=(l+r)>>1;
ll s1 = 0, s2 = 0;
if(L[id]) s1 = get1(L[id], l, mid, a, b);
if(R[id]) s2 = get1(R[id], mid + 1, r, a, b);
return gcd(s1, s2);
}
void update2(int id, int l, int r, int i, int x, ll val)
{
if(l == r)
{
if(!root[id]) root[id] = ++cur;
update1(root[id], 1, n, x, val);
}
else
{
int mid = (l + r) >> 1;
if(i <= mid)
{
if(!L[id]) L[id] = ++cur;
update2(L[id], l, mid, i, x, val);
}
else
{
if(!R[id]) R[id] = ++cur;
update2(R[id], mid + 1, r, i, x, val);
}
ll s1 = 0, s2 = 0;
if(L[id]) s1 = get1(root[L[id]], 1, n, x, x);
if(R[id]) s2 = get1(root[R[id]], 1, n, x, x);
ll s3 = gcd(s1, s2);
if(!root[id]) root[id] = ++cur;
update1(root[id], 1, n, x, s3);
}
}
ll get2(int id, int l, int r, int a, int b, int u, int v)
{
if(a > r || l > b) return 0;
else if(a <= l && r <= b)
{
if(root[id]) return get1(root[id], 1, n, u, v);
else return 0;
}
int mid = (l + r) >> 1;
ll s1 = 0, s2 = 0;
if(L[id]) s1 = get2(L[id], l, mid, a, b, u, v);
if(R[id]) s2 = get2(R[id], mid + 1, r, a, b, u, v);
return gcd(s1, s2);
}
void init(int R, int C)
{
m=R;
n=C;
}
void update(int P, int Q, ll K)
{
update2(1, 1, m, P+1, Q+1, K);
}
ll calculate(int P, int Q, int U, int V)
{
return get2(1, 1, m, P+1, U+1, Q+1, V+1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
304 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
380 ms |
7048 KB |
Output is correct |
5 |
Correct |
255 ms |
7368 KB |
Output is correct |
6 |
Correct |
304 ms |
4192 KB |
Output is correct |
7 |
Correct |
348 ms |
3836 KB |
Output is correct |
8 |
Correct |
248 ms |
3668 KB |
Output is correct |
9 |
Correct |
388 ms |
3968 KB |
Output is correct |
10 |
Correct |
303 ms |
3608 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
626 ms |
9056 KB |
Output is correct |
13 |
Correct |
1139 ms |
4492 KB |
Output is correct |
14 |
Correct |
224 ms |
1712 KB |
Output is correct |
15 |
Correct |
1251 ms |
5512 KB |
Output is correct |
16 |
Correct |
138 ms |
6972 KB |
Output is correct |
17 |
Correct |
518 ms |
5100 KB |
Output is correct |
18 |
Correct |
799 ms |
7228 KB |
Output is correct |
19 |
Correct |
695 ms |
7464 KB |
Output is correct |
20 |
Correct |
633 ms |
6864 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
381 ms |
7112 KB |
Output is correct |
13 |
Correct |
260 ms |
7312 KB |
Output is correct |
14 |
Correct |
307 ms |
4156 KB |
Output is correct |
15 |
Correct |
334 ms |
3916 KB |
Output is correct |
16 |
Correct |
230 ms |
3628 KB |
Output is correct |
17 |
Correct |
340 ms |
3900 KB |
Output is correct |
18 |
Correct |
296 ms |
3596 KB |
Output is correct |
19 |
Correct |
615 ms |
8984 KB |
Output is correct |
20 |
Correct |
1078 ms |
4492 KB |
Output is correct |
21 |
Correct |
208 ms |
1708 KB |
Output is correct |
22 |
Correct |
1262 ms |
5468 KB |
Output is correct |
23 |
Correct |
144 ms |
6992 KB |
Output is correct |
24 |
Correct |
495 ms |
5180 KB |
Output is correct |
25 |
Correct |
759 ms |
7328 KB |
Output is correct |
26 |
Correct |
666 ms |
7388 KB |
Output is correct |
27 |
Correct |
603 ms |
6844 KB |
Output is correct |
28 |
Correct |
358 ms |
25228 KB |
Output is correct |
29 |
Correct |
987 ms |
24600 KB |
Output is correct |
30 |
Correct |
2682 ms |
22336 KB |
Output is correct |
31 |
Correct |
2558 ms |
19268 KB |
Output is correct |
32 |
Correct |
399 ms |
2124 KB |
Output is correct |
33 |
Correct |
540 ms |
4132 KB |
Output is correct |
34 |
Correct |
177 ms |
21884 KB |
Output is correct |
35 |
Correct |
620 ms |
12428 KB |
Output is correct |
36 |
Correct |
1131 ms |
22312 KB |
Output is correct |
37 |
Correct |
950 ms |
22976 KB |
Output is correct |
38 |
Correct |
893 ms |
22316 KB |
Output is correct |
39 |
Correct |
764 ms |
18252 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
374 ms |
7924 KB |
Output is correct |
13 |
Correct |
253 ms |
7756 KB |
Output is correct |
14 |
Correct |
305 ms |
4928 KB |
Output is correct |
15 |
Correct |
337 ms |
4592 KB |
Output is correct |
16 |
Correct |
235 ms |
4300 KB |
Output is correct |
17 |
Correct |
328 ms |
4728 KB |
Output is correct |
18 |
Correct |
290 ms |
4396 KB |
Output is correct |
19 |
Correct |
605 ms |
9524 KB |
Output is correct |
20 |
Correct |
1052 ms |
4808 KB |
Output is correct |
21 |
Correct |
207 ms |
2496 KB |
Output is correct |
22 |
Correct |
1231 ms |
6264 KB |
Output is correct |
23 |
Correct |
141 ms |
7596 KB |
Output is correct |
24 |
Correct |
491 ms |
5856 KB |
Output is correct |
25 |
Correct |
760 ms |
8140 KB |
Output is correct |
26 |
Correct |
663 ms |
8040 KB |
Output is correct |
27 |
Correct |
604 ms |
7368 KB |
Output is correct |
28 |
Correct |
344 ms |
25808 KB |
Output is correct |
29 |
Correct |
972 ms |
25288 KB |
Output is correct |
30 |
Correct |
2662 ms |
23052 KB |
Output is correct |
31 |
Correct |
2470 ms |
19040 KB |
Output is correct |
32 |
Correct |
380 ms |
1996 KB |
Output is correct |
33 |
Correct |
506 ms |
3916 KB |
Output is correct |
34 |
Correct |
179 ms |
21816 KB |
Output is correct |
35 |
Correct |
603 ms |
12320 KB |
Output is correct |
36 |
Correct |
1136 ms |
22292 KB |
Output is correct |
37 |
Correct |
908 ms |
22272 KB |
Output is correct |
38 |
Correct |
878 ms |
21736 KB |
Output is correct |
39 |
Correct |
507 ms |
54260 KB |
Output is correct |
40 |
Correct |
1634 ms |
47208 KB |
Output is correct |
41 |
Correct |
3625 ms |
46436 KB |
Output is correct |
42 |
Correct |
3361 ms |
38484 KB |
Output is correct |
43 |
Correct |
349 ms |
44932 KB |
Output is correct |
44 |
Correct |
590 ms |
1652 KB |
Output is correct |
45 |
Correct |
769 ms |
17188 KB |
Output is correct |
46 |
Correct |
1876 ms |
45688 KB |
Output is correct |
47 |
Correct |
1619 ms |
45736 KB |
Output is correct |
48 |
Correct |
1573 ms |
45424 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |