結果
問題 | No.880 Yet Another Segment Tree Problem |
ユーザー | pekempey |
提出日時 | 2019-09-06 22:52:03 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,112 ms / 5,000 ms |
コード長 | 3,036 bytes |
コンパイル時間 | 1,831 ms |
コンパイル使用メモリ | 169,604 KB |
実行使用メモリ | 15,592 KB |
最終ジャッジ日時 | 2024-09-22 19:34:58 |
合計ジャッジ時間 | 16,308 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 10 ms
11,648 KB |
testcase_01 | AC | 12 ms
11,776 KB |
testcase_02 | AC | 13 ms
11,620 KB |
testcase_03 | AC | 13 ms
11,648 KB |
testcase_04 | AC | 13 ms
11,648 KB |
testcase_05 | AC | 13 ms
11,648 KB |
testcase_06 | AC | 13 ms
11,648 KB |
testcase_07 | AC | 13 ms
11,776 KB |
testcase_08 | AC | 13 ms
11,776 KB |
testcase_09 | AC | 14 ms
11,776 KB |
testcase_10 | AC | 12 ms
11,776 KB |
testcase_11 | AC | 794 ms
14,208 KB |
testcase_12 | AC | 776 ms
14,336 KB |
testcase_13 | AC | 564 ms
14,888 KB |
testcase_14 | AC | 797 ms
15,444 KB |
testcase_15 | AC | 891 ms
15,316 KB |
testcase_16 | AC | 935 ms
15,328 KB |
testcase_17 | AC | 1,112 ms
15,464 KB |
testcase_18 | AC | 1,089 ms
15,464 KB |
testcase_19 | AC | 216 ms
15,320 KB |
testcase_20 | AC | 242 ms
15,592 KB |
testcase_21 | AC | 225 ms
15,304 KB |
testcase_22 | AC | 255 ms
15,580 KB |
testcase_23 | AC | 249 ms
15,308 KB |
testcase_24 | AC | 177 ms
15,316 KB |
testcase_25 | AC | 202 ms
15,592 KB |
testcase_26 | AC | 184 ms
15,304 KB |
testcase_27 | AC | 205 ms
15,448 KB |
testcase_28 | AC | 207 ms
15,312 KB |
testcase_29 | AC | 779 ms
15,572 KB |
testcase_30 | AC | 833 ms
15,312 KB |
testcase_31 | AC | 906 ms
15,584 KB |
testcase_32 | AC | 118 ms
14,440 KB |
testcase_33 | AC | 88 ms
15,056 KB |
testcase_34 | AC | 92 ms
14,680 KB |
testcase_35 | AC | 85 ms
14,680 KB |
testcase_36 | AC | 85 ms
14,692 KB |
testcase_37 | AC | 90 ms
14,428 KB |
ソースコード
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() using namespace std; using ll = long long; constexpr int U = 1 << 17; ll gcd(ll x, ll y) { while (y != 0) { ll r = x % y; x = y; y = r; } return x; } ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } ll dat[U * 2]; ll dat2[U * 2]; ll sum[U * 2]; ll lazy[U * 2]; ll least[U * 2]; ll sz[U * 2]; constexpr ll INF = 1.01e9; void apply_set(int k, ll x) { if (x == 0) return; dat[k] = x; dat2[k] = x; sum[k] = x * sz[k]; lazy[k] = x; least[k] = x; } void push(int k) { apply_set(k * 2 + 0, lazy[k]); apply_set(k * 2 + 1, lazy[k]); lazy[k] = 0; } void pull(int k) { dat[k] = max(dat[k * 2], dat[k * 2 + 1]); dat2[k] = min(dat2[k * 2], dat2[k * 2 + 1]); sum[k] = sum[k * 2] + sum[k * 2 + 1]; least[k] = min(INF, lcm(least[k * 2], least[k * 2 + 1])); } void update_set(int a, int b, ll x, int k = 1, int l = 0, int r = U) { if (r <= a || b <= l) return; if (a <= l && r <= b) { apply_set(k, x); return; } push(k); int m = l + r >> 1; update_set(a, b, x, k * 2 + 0, l, m); update_set(a, b, x, k * 2 + 1, m, r); pull(k); } void update_gcd(int a, int b, ll g, int k = 1, int l = 0, int r = U) { if (r <= a || b <= l) return; if (a <= l && r <= b && dat[k] == dat2[k]) { apply_set(k, gcd(dat[k], g)); return; } if (lcm(least[k], g) == g) return; push(k); int m = l + r >> 1; update_gcd(a, b, g, k * 2 + 0, l, m); update_gcd(a, b, g, k * 2 + 1, m, r); pull(k); } ll query_max(int a, int b, int k = 1, int l = 0, int r = U) { if (r <= a || b <= l) return -1e18; if (a <= l && r <= b) return dat[k]; push(k); int m = l + r >> 1; ll vl = query_max(a, b, k * 2 + 0, l, m); ll vr = query_max(a, b, k * 2 + 1, m, r); return max(vl, vr); } ll query_sum(int a, int b, int k = 1, int l = 0, int r = U) { if (r <= a || b <= l) return 0; if (a <= l && r <= b) return sum[k]; push(k); int m = l + r >> 1; ll vl = query_sum(a, b, k * 2 + 0, l, m); ll vr = query_sum(a, b, k * 2 + 1, m, r); return vl + vr; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; vector<int> A(N); rep(i, U * 2) { least[i] = 1; dat[i] = -1e18; dat2[i] = 1e18; } rep(i, N) { cin >> A[i]; dat[i + U] = A[i]; dat2[i + U] = A[i]; sum[i + U] = A[i]; least[i + U] = A[i]; sz[i + U] = 1; } for (int i = U - 1; i >= 1; i--) pull(i), sz[i] = sz[i * 2] + sz[i * 2 + 1]; while (Q--) { int type; cin >> type; if (type == 1) { int l, r, x; cin >> l >> r >> x; l--; update_set(l, r, x); } else if (type == 2) { int l, r, x; cin >> l >> r >> x; l--; update_gcd(l, r, x); } else if (type == 3) { int l, r; cin >> l >> r; l--; cout << query_max(l, r) << '\n'; } else { int l, r; cin >> l >> r; l--; cout << query_sum(l, r) << '\n'; } } }