#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long long long
#define sz(x) int((x).size())
const int maxn1 = 10 << 16, maxn2 = 400 << 16;
struct N1 {
int lc, rc, v;
} h1[maxn1];
bitset<maxn2 * 2> h21;
uint16_t h216[maxn2];
uint32_t h232[maxn2];
int h2v[maxn2];
vector<long> vals {0};
map<long, int> vis;
void storev(int ind, long x) {
auto [it, inserted] = vis.emplace(x, sz(vals));
if (inserted) {
vals.push_back(x);
}
h2v[ind] = it->second;
dbg("STORE", ind, x);
}
long loadv(int ind) {
dbg("LOAD", ind, vals[h2v[ind]]);
return vals[h2v[ind]];
}
template <int offset>
void store(int ind, int x) {
h21[ind * 2 + offset] = x >> 24;
((uint8_t*)(h216 + ind))[offset] = x >> 16;
((uint16_t*)(h232 + ind))[offset] = x & 65535;
}
template <int offset>
int load(int ind) {
return h21[ind * 2 + offset] << 24 |
((uint8_t*)(h216 + ind))[offset] << 16 |
((uint16_t*)(h232 + ind))[offset];
}
int hind1 = 1, hind2 = 1;
int alloc1() {
return hind1++;
}
int alloc2() {
assert(hind2 < maxn2);
return hind2++;
}
template <int offset>
int load_or_alloc(int ind) {
int res = load<offset>(ind);
if (!res) {
store<offset>(ind, res = alloc2());
}
return res;
}
int n, m, root;
extern "C" void init(int _n, int _m) {
n = _n;
m = _m;
}
long query2(int o, int l, int r, int ql, int qr) {
if (!o) {
return 0;
} else if (ql <= l && r <= qr) {
return loadv(o);
}
int mid = (l + r) / 2;
long ans = 0;
if (ql <= mid) {
ans = query2(load<0>(o), l, mid, ql, qr);
}
if (mid < qr) {
ans = gcd(ans, query2(load<1>(o), mid + 1, r, ql, qr));
}
return ans;
}
long query1(int o, int l, int r, int ql, int qr, int ql2, int qr2) {
if (!o) {
return 0;
} else if (ql <= l && r <= qr) {
return query2(h1[o].v, 0, m - 1, ql2, qr2);
}
auto& [lc, rc, _] = h1[o];
int mid = (l + r) / 2;
long ans = 0;
if (ql <= mid) {
ans = query1(lc, l, mid, ql, qr, ql2, qr2);
}
if (mid < qr) {
ans = gcd(ans, query1(rc, mid + 1, r, ql, qr, ql2, qr2));
}
return ans;
}
void update2(int o, int l, int r, int y, long v) {
assert(o);
if (l == r) {
storev(o, v);
return;
}
int mid = (l + r) / 2;
if (y <= mid) {
update2(load_or_alloc<0>(o), l, mid, y, v);
} else {
update2(load_or_alloc<1>(o), mid + 1, r, y, v);
}
storev(o, gcd(loadv(load<0>(o)), loadv(load<1>(o))));
}
void update1(int& o, int l, int r, int x, int y, long v) {
if (!o) {
o = alloc1();
}
auto& [lc, rc, cv] = h1[o];
if (!cv) {
cv = alloc2();
}
if (l == r) {
update2(cv, 0, m - 1, y, v);
return;
}
int mid = (l + r) / 2;
if (x <= mid) {
update1(lc, l, mid, x, y, v);
} else {
update1(rc, mid + 1, r, x, y, v);
}
update2(cv, 0, m - 1, y,
gcd(query2(h1[lc].v, 0, m - 1, y, y),
query2(h1[rc].v, 0, m - 1, y, y)));
}
extern "C" void update(int x, int y, long v) {
update1(root, 0, n - 1, x, y, v);
}
extern "C" long calculate(int x1, int y1, int x2, int y2) {
return query1(root, 0, n - 1, x1, x2, y1, y2);
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
260 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 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 |
340 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 |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
543 ms |
7020 KB |
Output is correct |
5 |
Correct |
475 ms |
7272 KB |
Output is correct |
6 |
Correct |
483 ms |
5756 KB |
Output is correct |
7 |
Correct |
530 ms |
5492 KB |
Output is correct |
8 |
Correct |
339 ms |
4536 KB |
Output is correct |
9 |
Correct |
528 ms |
5592 KB |
Output is correct |
10 |
Correct |
469 ms |
4192 KB |
Output is correct |
11 |
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 |
1 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1121 ms |
8040 KB |
Output is correct |
13 |
Correct |
1547 ms |
2812 KB |
Output is correct |
14 |
Correct |
334 ms |
944 KB |
Output is correct |
15 |
Correct |
1792 ms |
3616 KB |
Output is correct |
16 |
Correct |
244 ms |
7316 KB |
Output is correct |
17 |
Correct |
687 ms |
6240 KB |
Output is correct |
18 |
Correct |
1114 ms |
9312 KB |
Output is correct |
19 |
Correct |
1077 ms |
9412 KB |
Output is correct |
20 |
Correct |
826 ms |
7100 KB |
Output is correct |
21 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
499 ms |
6948 KB |
Output is correct |
13 |
Correct |
402 ms |
7300 KB |
Output is correct |
14 |
Correct |
443 ms |
5908 KB |
Output is correct |
15 |
Correct |
494 ms |
5572 KB |
Output is correct |
16 |
Correct |
310 ms |
4512 KB |
Output is correct |
17 |
Correct |
454 ms |
5596 KB |
Output is correct |
18 |
Correct |
384 ms |
4180 KB |
Output is correct |
19 |
Correct |
864 ms |
8036 KB |
Output is correct |
20 |
Correct |
1396 ms |
2864 KB |
Output is correct |
21 |
Correct |
305 ms |
948 KB |
Output is correct |
22 |
Correct |
1705 ms |
3688 KB |
Output is correct |
23 |
Correct |
241 ms |
7376 KB |
Output is correct |
24 |
Correct |
678 ms |
6244 KB |
Output is correct |
25 |
Correct |
1150 ms |
9204 KB |
Output is correct |
26 |
Correct |
1052 ms |
9552 KB |
Output is correct |
27 |
Correct |
804 ms |
7244 KB |
Output is correct |
28 |
Correct |
1077 ms |
82300 KB |
Output is correct |
29 |
Correct |
2070 ms |
92280 KB |
Output is correct |
30 |
Correct |
5429 ms |
66072 KB |
Output is correct |
31 |
Correct |
5136 ms |
50760 KB |
Output is correct |
32 |
Correct |
847 ms |
972 KB |
Output is correct |
33 |
Correct |
966 ms |
1772 KB |
Output is correct |
34 |
Correct |
857 ms |
90316 KB |
Output is correct |
35 |
Correct |
1449 ms |
47616 KB |
Output is correct |
36 |
Correct |
2905 ms |
92088 KB |
Output is correct |
37 |
Correct |
2479 ms |
92220 KB |
Output is correct |
38 |
Correct |
2220 ms |
89928 KB |
Output is correct |
39 |
Correct |
1995 ms |
71432 KB |
Output is correct |
40 |
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 |
0 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
497 ms |
7056 KB |
Output is correct |
13 |
Correct |
413 ms |
7248 KB |
Output is correct |
14 |
Correct |
458 ms |
5744 KB |
Output is correct |
15 |
Correct |
467 ms |
5500 KB |
Output is correct |
16 |
Correct |
315 ms |
4656 KB |
Output is correct |
17 |
Correct |
479 ms |
5736 KB |
Output is correct |
18 |
Correct |
377 ms |
4216 KB |
Output is correct |
19 |
Correct |
930 ms |
8172 KB |
Output is correct |
20 |
Correct |
1400 ms |
2892 KB |
Output is correct |
21 |
Correct |
312 ms |
1016 KB |
Output is correct |
22 |
Correct |
1689 ms |
3708 KB |
Output is correct |
23 |
Correct |
260 ms |
7376 KB |
Output is correct |
24 |
Correct |
696 ms |
6432 KB |
Output is correct |
25 |
Correct |
1136 ms |
9296 KB |
Output is correct |
26 |
Correct |
1006 ms |
9452 KB |
Output is correct |
27 |
Correct |
799 ms |
7176 KB |
Output is correct |
28 |
Correct |
1080 ms |
82220 KB |
Output is correct |
29 |
Correct |
2074 ms |
92316 KB |
Output is correct |
30 |
Correct |
5385 ms |
66168 KB |
Output is correct |
31 |
Correct |
5104 ms |
50780 KB |
Output is correct |
32 |
Correct |
850 ms |
1012 KB |
Output is correct |
33 |
Correct |
969 ms |
1948 KB |
Output is correct |
34 |
Correct |
877 ms |
90268 KB |
Output is correct |
35 |
Correct |
1408 ms |
47532 KB |
Output is correct |
36 |
Correct |
2885 ms |
92136 KB |
Output is correct |
37 |
Correct |
2515 ms |
92416 KB |
Output is correct |
38 |
Correct |
2306 ms |
90236 KB |
Output is correct |
39 |
Correct |
1986 ms |
171980 KB |
Output is correct |
40 |
Correct |
3571 ms |
204016 KB |
Output is correct |
41 |
Correct |
7418 ms |
144320 KB |
Output is correct |
42 |
Correct |
6896 ms |
111708 KB |
Output is correct |
43 |
Correct |
1884 ms |
200484 KB |
Output is correct |
44 |
Correct |
1527 ms |
10628 KB |
Output is correct |
45 |
Correct |
1981 ms |
81832 KB |
Output is correct |
46 |
Correct |
4670 ms |
206684 KB |
Output is correct |
47 |
Correct |
4603 ms |
206568 KB |
Output is correct |
48 |
Correct |
4003 ms |
204008 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |