結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー penguinmanpenguinman
提出日時 2021-12-18 17:04:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 2,705 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 209,524 KB
実行使用メモリ 5,856 KB
最終ジャッジ日時 2023-09-12 14:07:53
合計ジャッジ時間 4,747 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 64 ms
5,844 KB
testcase_02 AC 65 ms
5,800 KB
testcase_03 AC 65 ms
5,852 KB
testcase_04 AC 64 ms
5,856 KB
testcase_05 AC 63 ms
5,800 KB
testcase_06 AC 63 ms
5,796 KB
testcase_07 AC 64 ms
5,800 KB
testcase_08 AC 64 ms
5,800 KB
testcase_09 AC 64 ms
5,784 KB
testcase_10 AC 65 ms
5,800 KB
testcase_11 AC 60 ms
5,804 KB
testcase_12 AC 60 ms
5,792 KB
testcase_13 AC 61 ms
5,852 KB
testcase_14 AC 68 ms
5,796 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 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,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, j, n) for(int i = int(j); i < int(n); i++)
#define REP(i, j, n) for(int i = int(j); i <= int(n); i++)
#define per(i, j, n) for(int i = int(j); int(n) <= i; i--)
#define ALL(a) (a).begin(),(a).end()
#define revALL(a) (a).rbegin(),(a).rend()
#define pb push_back
#define mp std::make_pair
#define mtp std::make_tuple
#define ln "\n"
using std::endl;
using std::cin;
using std::cout;
#define ll long long
using std::vector;
using std::string;
using std::upper_bound;
using std::lower_bound;
const ll MOD = int(1e9+7);
const ll MAX = 1e6+10;
const ll inf = (1ll << 60);

//modpow
ll modpow(ll X, ll Y, ll mod){
    ll ret = 1;
    while(Y){
        if(Y & 1){
            ret *= X;
            ret %= mod;
        }
        X *= X;
        X %= mod;
        Y >>= 1;
    }
    return ret % mod;
}

//binary_indexed_tree
template<typename T>
struct binary_indexed_tree{
    int N;
    vector<T> bit;
    binary_indexed_tree(int n):N(n){
        bit.resize(N+1,0);
    }
    T addition(T a, T b){
        return a+b;
    }
    void add(int x,T a){
        x++;
        for(; x<=N; x+=(x&-x)) bit[x] = addition(bit[x], a);
    }
    T sum(int x){
        x++;
        T ret=0;
        for(; x>0; x-=(x&-x)) ret = addition(ret, bit[x]);
        return ret;
    }
    ll lower_bound(T X){
        if(sum(N-1)<X) return -1;
        ll ret=0, memo=1;
        T sum=0;
        while(memo*2 <= N) memo *= 2;
        while(0 < memo){
            if(memo+ret<=N && addition(sum, bit[memo+ret])<X){
                sum = addition(sum, bit[memo+ret]);
                ret+=memo;
            }
            memo/=2;
        }
        return ret;
    }
};

//main
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    cout << std::fixed << std::setprecision(15);
    ll N,Q; cin >> N >> Q;
    string S; cin >> S;
    binary_indexed_tree<ll> bit(N);
    vector<int> A(N);
    rep(i,0,N) A[i] = S[i]-'(';
    rep(i,1,N){
        if(A[i-1] < A[i]) bit.add(i,1);
    }
    while(Q--){
        ll T; cin >> T;
        if(T == 1){
            ll X; cin >> X;
            X--;
            if(X && A[X-1] < A[X]) bit.add(X,-1);
            if(X+1 < N && A[X] < A[X+1]) bit.add(X+1,-1);
            A[X] ^= 1;
            if(X && A[X-1] < A[X]) bit.add(X,1);
            if(X+1 < N && A[X] < A[X+1]) bit.add(X+1,1);
        }
        else{
            ll l,r; cin >> l >> r;
            cout << bit.sum(r-1)-bit.sum(l-1) << ln;
        }
    }
}

0