結果

問題 No.2901 Logical Sum of Substring
ユーザー vjudge1vjudge1
提出日時 2024-10-19 16:26:26
言語 C++23
(gcc 12.3.0 + boost 1.83.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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 3 ms
6,816 KB
testcase_03 AC 3 ms
6,820 KB
testcase_04 AC 3 ms
6,820 KB
testcase_05 AC 3 ms
6,820 KB
testcase_06 AC 3 ms
6,816 KB
testcase_07 AC 3 ms
6,820 KB
testcase_08 AC 144 ms
176,184 KB
testcase_09 AC 141 ms
176,272 KB
testcase_10 AC 145 ms
176,340 KB
testcase_11 AC 155 ms
176,824 KB
testcase_12 AC 160 ms
177,040 KB
testcase_13 AC 184 ms
176,924 KB
testcase_14 AC 163 ms
176,660 KB
testcase_15 AC 163 ms
176,928 KB
testcase_16 AC 166 ms
176,348 KB
testcase_17 AC 154 ms
176,664 KB
testcase_18 AC 157 ms
176,832 KB
testcase_19 AC 152 ms
177,844 KB
testcase_20 AC 155 ms
176,448 KB
testcase_21 AC 155 ms
177,320 KB
testcase_22 AC 155 ms
176,580 KB
testcase_23 AC 145 ms
177,288 KB
testcase_24 AC 138 ms
176,300 KB
testcase_25 AC 143 ms
177,132 KB
testcase_26 AC 138 ms
176,624 KB
testcase_27 AC 141 ms
176,864 KB
testcase_28 AC 139 ms
176,540 KB
testcase_29 AC 135 ms
176,356 KB
testcase_30 AC 135 ms
177,296 KB
testcase_31 AC 138 ms
176,524 KB
権限があれば一括ダウンロードができます

ソースコード

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
*/
0