結果
問題 | No.2901 Logical Sum of Substring |
ユーザー | vjudge1 |
提出日時 | 2024-09-25 20:40:56 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 575 ms / 3,000 ms |
コード長 | 3,169 bytes |
コンパイル時間 | 2,285 ms |
コンパイル使用メモリ | 204,256 KB |
実行使用メモリ | 263,680 KB |
最終ジャッジ日時 | 2024-09-25 20:41:19 |
合計ジャッジ時間 | 16,169 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,812 KB |
testcase_02 | AC | 5 ms
6,816 KB |
testcase_03 | AC | 5 ms
6,944 KB |
testcase_04 | AC | 5 ms
6,940 KB |
testcase_05 | AC | 5 ms
6,944 KB |
testcase_06 | AC | 6 ms
6,944 KB |
testcase_07 | AC | 5 ms
6,944 KB |
testcase_08 | AC | 515 ms
263,552 KB |
testcase_09 | AC | 501 ms
263,652 KB |
testcase_10 | AC | 496 ms
263,556 KB |
testcase_11 | AC | 549 ms
263,676 KB |
testcase_12 | AC | 526 ms
263,620 KB |
testcase_13 | AC | 524 ms
263,552 KB |
testcase_14 | AC | 470 ms
263,488 KB |
testcase_15 | AC | 497 ms
263,608 KB |
testcase_16 | AC | 470 ms
263,628 KB |
testcase_17 | AC | 574 ms
263,508 KB |
testcase_18 | AC | 571 ms
263,680 KB |
testcase_19 | AC | 575 ms
263,552 KB |
testcase_20 | AC | 492 ms
263,636 KB |
testcase_21 | AC | 514 ms
263,568 KB |
testcase_22 | AC | 488 ms
263,636 KB |
testcase_23 | AC | 506 ms
263,680 KB |
testcase_24 | AC | 507 ms
263,552 KB |
testcase_25 | AC | 504 ms
263,680 KB |
testcase_26 | AC | 493 ms
263,552 KB |
testcase_27 | AC | 492 ms
263,528 KB |
testcase_28 | AC | 488 ms
263,552 KB |
testcase_29 | AC | 490 ms
263,552 KB |
testcase_30 | AC | 490 ms
263,540 KB |
testcase_31 | AC | 503 ms
263,552 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]); } } } 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; return tr[i] = Merge(build(i * 2, l, mid), build(i * 2 + 1, mid + 1, r)); } 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); 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; }