結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー ぷらぷら
提出日時 2022-01-07 21:46:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,160 bytes
コンパイル時間 2,187 ms
コンパイル使用メモリ 198,704 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 18:34:31
合計ジャッジ時間 7,973 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 434 ms
5,376 KB
testcase_15 WA -
testcase_16 AC 1 ms
5,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct BinaryIndexedTree {
    int N;
    vector<T>bit;
    BinaryIndexedTree(){}
    BinaryIndexedTree(int x) {
        N = x;
        bit.resize(x+1);
    }
    void add(int x,T y) {
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    T sum(int x) {
        T res = 0;
        while(x) {
            res += bit[x];
            x -= x&-x;
        }
        return res;
    }
    inline T sum(int l, int r) {//[l,r]
        return sum(r)-sum(l-1);
    }
    inline T operator[](int k) {
        return sum(k)-sum(k-1);
    }
    int lower_bound(T w) {// left sum(i) >= w
        int x = 0;
        for (int k = 1 << __lg(N); k; k >>= 1) {
            if (x + k <= N - 1 && bit[x + k] < w) {
                w -= bit[x + k];
                x += k;
            }
        }
        return x;
    }
    int upper_bound(T w) {// left sum(i) > w
        int x = 0;
        for (int k = 1 << __lg(N); k; k >>= 1) {
            if (x + k <= N - 1 && bit[x + k] <= w) {
                w -= bit[x + k];
                x += k;
            }
        }
        return x;
    }
};

int main() {
    int N,Q;
    string S;
    cin >> N >> Q >> S;
    BinaryIndexedTree<int>bit(N);
    for(int i = 0; i+1 < N; i++) {
        if(S[i] == '(' && S[i+1] == ')') {
            bit.add(i+1,1);
        }
    }
    while (Q--) {
        int f;
        cin >> f;
        if(f == 1) {
            int a;
            cin >> a;
            a--;
            if(S[a] == '(') {
                if(a+1 < N && S[a+1] == ')') {
                    bit.add(a+1,-1);
                }
                if(a && S[a-1] == '(') {
                    bit.add(a,1);
                }
                S[a] = ')';
            }
            else {
                if(a+1 < N && S[a+1] == ')') {
                    bit.add(a+1,1);
                }
                if(a && S[a-1] == '(') {
                    bit.add(a,-1);
                }
            }
        }
        else {
            int l,r;
            cin >> l >> r;
            cout << bit.sum(l,r-1) << endl;
        }
    }
}
0