結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-28 23:11:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 372 ms / 2,000 ms
コード長 1,573 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 109,948 KB
実行使用メモリ 4,496 KB
最終ジャッジ日時 2023-08-27 17:22:26
合計ジャッジ時間 8,352 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 251 ms
4,496 KB
testcase_02 AC 257 ms
4,376 KB
testcase_03 AC 251 ms
4,376 KB
testcase_04 AC 243 ms
4,380 KB
testcase_05 AC 246 ms
4,380 KB
testcase_06 AC 246 ms
4,452 KB
testcase_07 AC 238 ms
4,380 KB
testcase_08 AC 241 ms
4,380 KB
testcase_09 AC 246 ms
4,376 KB
testcase_10 AC 242 ms
4,376 KB
testcase_11 AC 231 ms
4,376 KB
testcase_12 AC 233 ms
4,376 KB
testcase_13 AC 231 ms
4,380 KB
testcase_14 AC 372 ms
4,384 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>

using namespace std;
using ll = long long;

//RSQ (0-indexed)
template<class T> struct BIT {
    vector<T> bit;
    int N;

    BIT (int n){
        N = n;
        bit.resize(N);
    }

    void add(int loc, T val){
        loc++;
        while(loc <= N){
            bit[loc-1] += val;
            loc += loc & -loc;
        }
    }

    T sum(int l, int r){
        T res = _sum(r) - _sum(l-1);
        return res;
    }

    T _sum(int r){
        r++;
        T res = 0;
        while(r > 0){
            res += bit[r-1];
            r -= r & -r;
        }
        return res;
    }
};

int main(){
 
    int N, Q, l, r, t;
    string S;
    cin >> N >> Q >> S;
    BIT<int> tree(N-1);
    for (int i=0; i<N-1; i++){
        if (S[i] == '(' && S[i+1] == ')') tree.add(i, 1);
    }

    for (int i=0; i<Q; i++){
        cin >> t;
        if (t == 1){
            cin >> l; l--;
            if (l-1>=0 && S[l-1] == '(' && S[l] == ')') tree.add(l-1, -1);
            else if (l-1>=0 && S[l-1] == '(' && S[l] == '(') tree.add(l-1, 1);
            if (l+1<=N-1 && S[l] == '(' && S[l+1] == ')') tree.add(l, -1);
            else if (l+1<=N-1 && S[l] == ')' && S[l+1] == ')') tree.add(l, 1);
            S[l] = '('+')'-S[l];
        }
        else{
            cin >> l >> r;
            l--; r--;
            cout << tree.sum(l, r-1) << endl;
        }
    }


    return 0;
}
0