結果
問題 | No.2901 Logical Sum of Substring |
ユーザー | vjudge1 |
提出日時 | 2024-09-25 20:40:14 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 696 ms / 3,000 ms |
コード長 | 4,199 bytes |
コンパイル時間 | 2,037 ms |
コンパイル使用メモリ | 204,560 KB |
実行使用メモリ | 263,680 KB |
最終ジャッジ日時 | 2024-09-25 20:40:34 |
合計ジャッジ時間 | 18,048 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 5 ms
6,944 KB |
testcase_03 | AC | 5 ms
6,940 KB |
testcase_04 | AC | 4 ms
6,940 KB |
testcase_05 | AC | 5 ms
6,940 KB |
testcase_06 | AC | 5 ms
6,944 KB |
testcase_07 | AC | 5 ms
6,940 KB |
testcase_08 | AC | 592 ms
263,664 KB |
testcase_09 | AC | 598 ms
263,552 KB |
testcase_10 | AC | 628 ms
263,552 KB |
testcase_11 | AC | 615 ms
263,680 KB |
testcase_12 | AC | 629 ms
263,680 KB |
testcase_13 | AC | 629 ms
263,552 KB |
testcase_14 | AC | 571 ms
263,680 KB |
testcase_15 | AC | 577 ms
263,680 KB |
testcase_16 | AC | 566 ms
263,680 KB |
testcase_17 | AC | 685 ms
263,552 KB |
testcase_18 | AC | 696 ms
263,680 KB |
testcase_19 | AC | 679 ms
263,552 KB |
testcase_20 | AC | 593 ms
263,552 KB |
testcase_21 | AC | 598 ms
263,680 KB |
testcase_22 | AC | 593 ms
263,424 KB |
testcase_23 | AC | 601 ms
263,552 KB |
testcase_24 | AC | 610 ms
263,552 KB |
testcase_25 | AC | 610 ms
263,480 KB |
testcase_26 | AC | 597 ms
263,552 KB |
testcase_27 | AC | 594 ms
263,680 KB |
testcase_28 | AC | 599 ms
263,552 KB |
testcase_29 | AC | 599 ms
263,680 KB |
testcase_30 | AC | 633 ms
263,680 KB |
testcase_31 | AC | 600 ms
263,680 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, M = 20, INF = 1e9; struct Node { int ans, pre[M], suf[M], sum, pre_p[M], suf_p[M], len; } tr[4 * N], Empty; int n, k, a[N], q; Node Merge(Node i, Node j) { Node ret = Empty; ret.ans = min(i.ans, j.ans); for (int p = 0; p < k; p++) { for (int q = 0; q < k; q++) { if (i.suf[p] >= 0 && j.pre[q] >= 0 && (i.suf[p] | j.pre[q]) == (1 << k) - 1) { ret.ans = min(ret.ans, i.suf_p[p] + j.pre_p[q]); //cout << p << ' ' << q << ' ' << i.pre_p[p] << ' ' << j.suf_p[q] << '\n'; } } } /*cout << i.ans << ' ' << j.ans << ' ' << i.sum << ' ' << j.sum << ' ' << ret.sum << '\n'; cout << "i : "; for (int p = 0; p < k; p++) cout << i.suf[p] << ' '; cout << "\nj : "; for (int p = 0; p < k; p++) cout << j.pre[p] << ' '; cout << '\n';*/ int t = 0; for (int p = 0; p < k; p++) { if (i.pre[p] >= 0) ret.pre[t++] = i.pre[p], ret.pre_p[t - 1] = i.pre_p[p]; else { for (int q = 0; q < k; q++) { if (j.pre[q] >= 0 && (i.sum | j.pre[q]) > ret.pre[t - 1]) { ret.pre[t++] = i.sum | j.pre[q]; ret.pre_p[t - 1] = i.len + j.pre_p[q]; } } } } t = 0; for (int p = 0; p < k; p++) { if (j.suf[p] >= 0) ret.suf[t++] = j.suf[p], ret.suf_p[t - 1] = j.suf_p[p]; else { for (int q = 0; q < k; q++) { if (i.suf[q] >= 0 && (j.sum | i.suf[q]) > ret.suf[t - 1]) { ret.suf[t++] = j.sum | i.suf[q]; ret.suf_p[t - 1] = j.len + i.suf_p[q]; } } } } ret.sum = i.sum | j.sum, ret.len = i.len + j.len; return ret; } Node build(int i, int l, int r) { if (l == r) { tr[i].ans = (a[l] == (1 << k) - 1 ? 1 : INF); tr[i].pre[0] = tr[i].suf[0] = tr[i].sum = a[l]; tr[i].pre_p[0] = tr[i].suf_p[0] = tr[i].len = 1; return tr[i]; } int mid = (l + r) >> 1; tr[i] = Merge(build(i * 2, l, mid), build(i * 2 + 1, mid + 1, r)); //cout << "build : " << l << ' ' << r << ' ' << tr[i].ans << ' ' << tr[i].sum << '\n'; return tr[i]; } void modify(int i, int l, int r, int pos, int x) { if (l == r) { tr[i] = Empty; tr[i].ans = (x == (1 << k) - 1 ? 1 : INF); tr[i].pre[0] = tr[i].suf[0] = tr[i].sum = x; tr[i].pre_p[0] = tr[i].suf_p[0] = tr[i].len = 1; return ; } int mid = (l + r) >> 1; pos <= mid ? modify(i * 2, l, mid, pos, x) : modify(i * 2 + 1, mid + 1, r, pos, x); tr[i] = Merge(tr[i * 2], tr[i * 2 + 1]); } Node query(int i, int l, int r, int ql, int qr) { if (qr < l || ql > r) return Empty; if (ql <= l && r <= qr) return tr[i]; int mid = (l + r) >> 1; return Merge(query(i * 2, l, mid, ql, qr), query(i * 2 + 1, mid + 1, r, ql, qr)); } int main() { ios::sync_with_stdio(0), cin.tie(0); // freopen("test.in", "r", stdin); // freopen("1.out", "w", stdout); Empty.ans = INF, Empty.sum = Empty.len = 0; fill(Empty.pre, Empty.pre + 20, -1); fill(Empty.suf, Empty.suf + 20, -1); fill(Empty.pre_p, Empty.pre_p + 20, INF); fill(Empty.suf_p, Empty.suf_p + 20, INF); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; fill(tr + 1, tr + 4 * n + 1, Empty); cin >> q, build(1, 1, n); while (q--) { int op; cin >> op; if (op == 1) { int i, x; cin >> i >> x; modify(1, 1, n, i, x); } else { int l, r; cin >> l >> r; Node t = query(1, 1, n, l, r); cout << (t.ans == INF ? -1 : t.ans) << '\n'; } } return 0; } /* 15 14 4099 8602 13653 2718 5587 10360 12181 9352 2451 14529 7600 16196 5078 15897 3144 7 1 1 828 2 6 9 2 8 14 2 1 12 2 5 6 2 7 8 1 7 10808 5 3 7 5 1 7 0 18 2 1 2 1 3 6 2 3 5 2 1 2 1 1 1 2 4 4 1 1 6 2 4 5 2 2 5 2 2 3 2 2 2 1 4 6 1 5 7 2 3 5 1 3 4 2 4 5 2 5 5 2 2 4 6 5 4 6 7 110 101 100 110 111 18 4 8 10 1 7 6 2 2 7 10 13 1 1 14 6 12 7 0 5 16 1 9 7 2 10 15 1 7 7 2 14 18 1 9 2 2 12 13 1 15 10 1 10 0 2 12 14 1 8 2 2 2 15 1 7 14 2 9 10 2 2 6 2 6 11 2 7 10 8 10 1 7 6 2 14 2 2 0 1 1 14 6 10 7 0 5 */