結果

問題 No.2265 Xor Range Substring Sum Query
ユーザー SSRSSSRS
提出日時 2023-04-07 15:49:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,210 ms / 5,000 ms
コード長 2,096 bytes
コンパイル時間 1,548 ms
コンパイル使用メモリ 172,040 KB
実行使用メモリ 132,876 KB
最終ジャッジ日時 2024-04-10 16:18:07
合計ジャッジ時間 19,983 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 732 ms
132,812 KB
testcase_05 AC 715 ms
132,592 KB
testcase_06 AC 740 ms
132,876 KB
testcase_07 AC 739 ms
132,764 KB
testcase_08 AC 751 ms
132,668 KB
testcase_09 AC 1,031 ms
132,672 KB
testcase_10 AC 1,047 ms
132,680 KB
testcase_11 AC 1,032 ms
132,836 KB
testcase_12 AC 1,035 ms
132,532 KB
testcase_13 AC 1,025 ms
132,592 KB
testcase_14 AC 816 ms
132,696 KB
testcase_15 AC 809 ms
132,596 KB
testcase_16 AC 839 ms
132,668 KB
testcase_17 AC 865 ms
132,752 KB
testcase_18 AC 581 ms
132,704 KB
testcase_19 AC 588 ms
132,768 KB
testcase_20 AC 1,187 ms
132,656 KB
testcase_21 AC 1,210 ms
132,676 KB
testcase_22 AC 589 ms
64,876 KB
testcase_23 AC 573 ms
65,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
struct node{
  long long x, p2, p11;
  node(): x(0), p2(1), p11(1){
  }
  node(int x): x(x), p2(2), p11(11){
  }
};
node op(node L, node R){
  node ans;
  ans.x = (L.x * R.p11 + L.p2 * R.x) % MOD;
  ans.p2 = L.p2 * R.p2 % MOD;
  ans.p11 = L.p11 * R.p11 % MOD;
  return ans;
}
node e(){
  return node();
}
template <typename T, T (*op)(T, T), T (*e)()>
struct xor_segment_tree{
  int LOG, N;
  vector<vector<T>> ST;
  xor_segment_tree(vector<T> &A){
    N = A.size();
    LOG = __builtin_ctz(N);
    ST = vector<vector<T>>(LOG + 1, vector<T>(N));
    for (int i = 0; i < N; i++){
      ST[0][i] = A[i];
    }
    for (int i = 0; i < LOG; i++){
      for (int j = 0; j < N; j++){
        ST[i + 1][j] = op(ST[i][j], ST[i][j ^ (1 << i)]);
      }
    }
  }
  void update(int p, T x){
    ST[0][p] = x;
    for (int i = 0; i < min(LOG, 9); i++){
      for (int j = 0; j < (1 << (i + 1)); j++){
        ST[i + 1][p ^ j] = op(ST[i][p ^ j], ST[i][p ^ j ^ (1 << i)]);
      }
    }
  }
  T range_fold(int L, int R, int x){
    T ansL = e(), ansR = e();
    for (int i = 0; i <= min(LOG, 9); i++){
      if (L == R){
        break;
      }
      if ((L >> i & 1) == 1){
        ansL = op(ansL, ST[i][L ^ x]);
        L += 1 << i;
      }
      if ((R >> i & 1) == 1){
        R -= 1 << i;
        ansR = op(ST[i][R ^ x], ansR);
      }
    }
    while (L < R){
      ansL = op(ansL, ST[9][L ^ x]);
      L += 1 << 9;
    }
    return op(ansL, ansR);
  }
};
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string S;
  cin >> S;
  vector<node> A(1 << n);
  for (int i = 0; i < (1 << n); i++){
    A[i] = node(S[i] - '0');
  }
  int Q;
  cin >> Q;
  xor_segment_tree<node, op, e> ST(A);
  for (int i = 0; i < Q; i++){
    int t;
    cin >> t;
    if (t == 1){
      int x, y;
      cin >> x >> y;
      ST.update(x, node(y));
      A[x] = node(y);
    }
    if (t == 2){
      int L, R, X;
      cin >> L >> R >> X;
      R++;
      cout << ST.range_fold(L, R, X).x << endl;
    }
  }
}
0