結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-15 16:48:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 2,444 bytes
コンパイル時間 1,437 ms
コンパイル使用メモリ 129,196 KB
実行使用メモリ 9,792 KB
最終ジャッジ日時 2023-10-15 16:48:35
合計ジャッジ時間 6,644 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 125 ms
9,792 KB
testcase_02 AC 127 ms
9,692 KB
testcase_03 AC 126 ms
9,612 KB
testcase_04 AC 126 ms
9,616 KB
testcase_05 AC 126 ms
9,628 KB
testcase_06 AC 124 ms
9,680 KB
testcase_07 AC 124 ms
9,628 KB
testcase_08 AC 126 ms
9,696 KB
testcase_09 AC 136 ms
9,608 KB
testcase_10 AC 127 ms
9,616 KB
testcase_11 AC 105 ms
9,632 KB
testcase_12 AC 102 ms
9,616 KB
testcase_13 AC 104 ms
9,684 KB
testcase_14 AC 117 ms
9,680 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 1 ms
4,372 KB
testcase_17 AC 1 ms
4,368 KB
testcase_18 AC 1 ms
4,368 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 1 ms
4,372 KB
testcase_22 AC 1 ms
4,372 KB
testcase_23 AC 1 ms
4,368 KB
testcase_24 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
using namespace std;
template<class T,T (*op)(T,T),T (*e)()> struct SegmentTree
{
    private:
    int siz;
    vector<T> node;

    public:
    SegmentTree(int n) noexcept
    {
        siz = 1;
        while(siz < n) siz <<= 1;

        node.assign(2*siz-1,e());
    }

    void replace(int pos,T x) noexcept {func_update(pos,x,[](T u,T v){return u = v;});}

    void add(int pos,T x) noexcept {func_update(pos,x,[](T u,T v){return u+v;});}

    void func_update(int pos,T x) noexcept {func_update(pos,x,op);}

    void func_update(int pos,T x,T (*func)(T,T)) noexcept
    {
        assert(0 <= pos && pos < siz);
        pos += siz - 1;

        node[pos] = func(node[pos],x);

        while(pos)
        {
            pos = (pos-1)/2;
            node[pos] = op(node[2*pos+1],node[2*pos+2]);
        }
    }

    T operator [](int pos) noexcept
    {
        assert(0 <= pos && pos < siz);
        return node[pos+siz-1];
    }

    T prod(int l,int r) noexcept
    {
        assert(0 <= l && l <= r && r <= siz);

        l += siz-1;
        r += siz-1;

        T Lval = e();
        T Rval = e();

        while(l < r)
        {
            if(!(l & 1)) Lval = op(Lval,node[l++]);
            if(!(r & 1)) Rval = op(node[--r],Rval);

            l >>= 1;
            r >>= 1;
        }

        return op(Lval,Rval);
    }

    T prod() noexcept {return node[0];}
};
struct dat
{
	int l,r,cnt;
};
dat op(dat a,dat b)
{
	dat x;
	x.l = a.l;
	x.r = b.r;
	x.cnt = a.cnt + b.cnt;
	if(a.r == 0 && b.l == 1) x.cnt++;

	return x;
}
dat e() {return dat{2,2,0};}
int N,Q;
string S;
void solve()
{
	cin >> N >> Q >> S;
	SegmentTree<dat,op,e> seg(N);
	for(int i = 0;i < N;i++)
	{
		dat x;
		x.cnt = 0;
		if(S[i] == '(') x.l = x.r = 0;
		else x.l = x.r = 1;
		seg.replace(i,x);
	}
	while(Q--)
	{
		int t;
		cin >> t;
		if(t == 1)
		{
			int i;
			cin >> i;
			i--;
			dat cur = seg[i];
			cur.l ^= 1,cur.r ^= 1;
			seg.replace(i,cur);
		}
		else
		{
			int l,r;
			cin >> l >> r;
			l--;
			cout << seg.prod(l,r).cnt << "\n";
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
}
0