結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー SSRSSSRS
提出日時 2022-01-07 21:29:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 1,657 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 168,252 KB
実行使用メモリ 5,264 KB
最終ジャッジ日時 2023-09-12 14:08:07
合計ジャッジ時間 7,098 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 273 ms
5,056 KB
testcase_02 AC 271 ms
5,024 KB
testcase_03 AC 266 ms
5,224 KB
testcase_04 AC 268 ms
4,980 KB
testcase_05 AC 272 ms
5,264 KB
testcase_06 AC 271 ms
5,136 KB
testcase_07 AC 274 ms
4,992 KB
testcase_08 AC 274 ms
4,964 KB
testcase_09 AC 266 ms
4,988 KB
testcase_10 AC 273 ms
4,968 KB
testcase_11 AC 262 ms
5,236 KB
testcase_12 AC 263 ms
4,972 KB
testcase_13 AC 260 ms
4,988 KB
testcase_14 AC 411 ms
4,972 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct binary_indexed_tree{
  int N;
  vector<T> BIT;
  binary_indexed_tree(vector<T> &A){
    N = A.size();
    BIT = vector<T>(N + 1, 0);
    for (int i = 0; i < N; i++){
      BIT[i + 1] = A[i];
    }
    for (int i = 1; i < N; i++){
      if (i + (i & -i) <= N){
        BIT[i + (i & -i)] += BIT[i];
      }
    }
  }
  void add(int i, T x){
    i++;
    while (i <= N){
      BIT[i] += x;
      i += i & -i;
    }
  }
  T sum(int i){
    T ans = 0;
    while (i > 0){
      ans += BIT[i];
      i -= i & -i;
    }
    return ans;
  }
  T sum(int L, int R){
    return sum(R) - sum(L);
  }
};
int main(){
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> A(N - 1, 0);
  for (int i = 0; i < N - 1; i++){
    if (S[i] == '(' && S[i + 1] == ')'){
      A[i]++;
    }
  }
  binary_indexed_tree<int> BIT(A);
  for (int j = 0; j < Q; j++){
    int t;
    cin >> t;
    if (t == 1){
      int i;
      cin >> i;
      i--;
      if (i > 0){
        if (S[i - 1] == '(' && S[i] == ')'){
          BIT.add(i - 1, -1);
        }
      }
      if (i < N - 1){
        if (S[i] == '(' && S[i + 1] == ')'){
          BIT.add(i, -1);
        }
      }
      if (S[i] == '('){
        S[i] = ')';
      } else {
        S[i] = '(';
      }
      if (i > 0){
        if (S[i - 1] == '(' && S[i] == ')'){
          BIT.add(i - 1, 1);
        }
      }
      if (i < N - 1){
        if (S[i] == '(' && S[i + 1] == ')'){
          BIT.add(i, 1);
        }
      }
    }
    if (t == 2){
      int l, r;
      cin >> l >> r;
      l--;
      cout << BIT.sum(l, r - 1) << endl;
    }
  }
}
0