結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー kozykozy
提出日時 2022-01-07 21:48:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 706 ms / 2,000 ms
コード長 2,223 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 91,496 KB
最終ジャッジ日時 2023-09-12 14:10:04
合計ジャッジ時間 12,576 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,396 KB
testcase_01 AC 631 ms
90,432 KB
testcase_02 AC 633 ms
91,360 KB
testcase_03 AC 646 ms
90,016 KB
testcase_04 AC 623 ms
89,700 KB
testcase_05 AC 644 ms
91,428 KB
testcase_06 AC 619 ms
89,936 KB
testcase_07 AC 622 ms
89,712 KB
testcase_08 AC 632 ms
90,472 KB
testcase_09 AC 632 ms
91,096 KB
testcase_10 AC 629 ms
90,656 KB
testcase_11 AC 621 ms
90,624 KB
testcase_12 AC 622 ms
91,496 KB
testcase_13 AC 619 ms
90,288 KB
testcase_14 AC 706 ms
90,972 KB
testcase_15 AC 77 ms
71,372 KB
testcase_16 AC 74 ms
71,580 KB
testcase_17 AC 73 ms
71,532 KB
testcase_18 AC 75 ms
71,056 KB
testcase_19 AC 76 ms
71,304 KB
testcase_20 AC 77 ms
71,052 KB
testcase_21 AC 75 ms
71,288 KB
testcase_22 AC 75 ms
71,056 KB
testcase_23 AC 78 ms
71,468 KB
testcase_24 AC 75 ms
71,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:

    __all__ = ['add', 'sumrange', 'lower_left']

    def __init__(self, maxsize=10**6):
        assert (maxsize > 0)

        self._n = maxsize+1
        self._bitdata = [0]*(maxsize+1)

    def add(self, i, x):
        '''Add x to A[i] (A[i] += x) '''
        assert(0 <= i < self._n)

        pos = i+1
        while pos < self._n:
            self._bitdata[pos] += x
            pos += pos&(-pos)

    def running_total(self, i):
        ''' Return sum of (A[0] ... A[i]) '''
        assert (-1<= i < self._n)

        if i == -1:
            return 0
        returnval = 0
        pos = i+1
        while pos:
            returnval += self._bitdata[pos]
            pos -= pos & (-pos)
        return returnval

    def sumrange(self, lo=0, hi=None):
        ''' Return sum of (A[lo] ... A[hi]) '''
        if lo < 0:
            raise ValueError('lo must be non-negative')
        if hi is None:
            hi = self._n

        return self.running_total(hi) - self.running_total(lo-1)

    def lower_left(self, total):
        ''' Return min-index satisfying {sum(A0 ~ Ai) >= total}
        only if Ai >= 0 (for all i)
        '''
        if total < 0:
            return -1
        pos = 0
        k = 1<<(self._n.bit_length()-1)
        while k > 0:
            if pos+k < self._n and self._bitdata[pos+k] < total:
                total -= self._bitdata[pos+k]
                pos += k
            k //= 2
        return pos
def tentousu(lis):
  bit = BIT()
  ans = 0
  for i in range(len(lis)):
      bit.add(lis[i], 1)
      ans += i + 1 - bit.running_total(lis[i])
  return ans

N,Q=map(int,input().split())
S=input()
bit=BIT(N)
for i in range(N-1):
  if S[i]=="(" and S[i+1]==")":
    bit.add(i,1)
L=list(S)
for i in range(Q):
  A=list(map(int,input().split()))
  if A[0]==1:
    a=A[1]
    a-=1
    if L[a]=="(":
      if a!=N-1:
        if L[a+1]==")":
          bit.add(a,-1)
      if a!=0:
        if L[a-1]=="(":
          bit.add(a-1,1)
      L[a]=")"
    else:
      L[a]="("
      if a!=0:
        if L[a-1]=="(":
          bit.add(a-1,-1)
      if a!=N-1:
        if L[a+1]==")":
          bit.add(a,1)
  else:
    l,r=A[1],A[2]
    l-=1
    r-=1
    ans=bit.sumrange(l,r-1)
    print(ans)
0