結果

問題 No.2901 Logical Sum of Substring
ユーザー vjudge1
提出日時 2024-10-19 16:26:26
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 184 ms / 3,000 ms
コード長 2,897 bytes
コンパイル時間 2,968 ms
コンパイル使用メモリ 247,768 KB
実行使用メモリ 177,844 KB
最終ジャッジ日時 2024-10-19 16:26:35
合計ジャッジ時間 8,194 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
#define _1 (__int128)1
using namespace std;
using ll = long long;
void FileIO (const string s) {
freopen(string(s + ".in").c_str(), "r", stdin);
freopen(string(s + ".out").c_str(), "w", stdout);
}
const int N = 2e5 + 10, K = 20, INF = 1e9;
struct SegTree {
int pre[K], suf[K], p[K], s[K], pc, sc, ans, len;
} tr[N * 4], ans;
int n, k, a[N], q, op, x, y;
SegTree Merge (SegTree x, SegTree y) {
if (!x.len) return y;
if (!y.len) return x;
SegTree ret;
ret.pc = ret.sc = 0, ret.ans = min(x.ans, y.ans), ret.len = x.len + y.len;
for (int i = 1; i <= x.pc; i++) ret.pre[++ret.pc] = x.pre[i], ret.p[ret.pc] = x.p[i];
for (int i = 1; i <= y.pc; i++)
if ((y.pre[i] | ret.pre[ret.pc]) != ret.pre[ret.pc])
ret.pre[ret.pc + 1] = (y.pre[i] | ret.pre[ret.pc]), ret.p[ret.pc + 1] = y.p[i] + x.len, ret.pc++;
for (int i = 1; i <= y.sc; i++) ret.suf[++ret.sc] = y.suf[i], ret.s[ret.sc] = y.s[i];
for (int i = 1; i <= x.sc; i++)
if ((x.suf[i] | ret.suf[ret.sc]) != ret.suf[ret.sc])
ret.suf[ret.sc + 1] = (x.suf[i] | ret.suf[ret.sc]), ret.s[ret.sc + 1] = x.s[i] + y.len, ret.sc++;
for (int i = 1; i <= x.sc; i++)
for (int j = 1; j <= y.pc; j++)
if ((x.suf[i] | y.pre[j]) == (1 << k) - 1)
ret.ans = min(ret.ans, (x.s[i] + y.p[j]));
return ret;
}
void pushup (int id) {
tr[id] = Merge(tr[id * 2], tr[id * 2 + 1]);
}
void build (int id, int l, int r) {
if (l == r) {
tr[id].len = 1;
tr[id].pc = tr[id].sc = 0, tr[id].ans = INF;
tr[id].pre[++tr[id].pc] = a[l], tr[id].p[tr[id].pc] = 1;
tr[id].suf[++tr[id].sc] = a[l], tr[id].s[tr[id].sc] = 1;
if (a[l] == (1 << k) - 1) tr[id].ans = 1;
return ;
}
int mid = (l + r) >> 1;
build(id * 2, l, mid), build(id * 2 + 1, mid + 1, r);
pushup(id);
}
void modify (int id, int l, int r, int x, int y) {
if (l == r) {
tr[id].pc = tr[id].sc = 0, tr[id].ans = INF;
tr[id].pre[++tr[id].pc] = y, tr[id].p[tr[id].pc] = 1;
tr[id].suf[++tr[id].sc] = y, tr[id].s[tr[id].sc] = 1;
if (y == (1 << k) - 1) tr[id].ans = 1;
return ;
}
int mid = (l + r) >> 1;
if (mid >= x) modify(id * 2, l, mid, x, y);
else modify(id * 2 + 1, mid + 1, r, x, y);
pushup(id);
}
void Query (int id, int l, int r, int x, int y) {
if (l >= x && r <= y) {
ans = Merge(ans, tr[id]);
return ;
}
if (l > y || r < x) return ;
int mid = (l + r) >> 1;
Query(id * 2, l, mid, x, y), Query(id * 2 + 1, mid + 1, r, x, y);
}
signed main () {
ios::sync_with_stdio(0), cin.tie(0);
// FileIO("");
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
build(1, 1, n);
for (cin >> q; q--; ) {
cin >> op >> x >> y;
if (op == 1) modify(1, 1, n, x, y);
else {
ans.len = 0;
Query(1, 1, n, x, y);
cout << (ans.ans == INF ? -1 : ans.ans) << '\n';
}
}
return 0;
}
/*
3 2
1 2 3
3
2 1 3
1 2 1
2 1 2
*/
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0