結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー kozykozy
提出日時 2022-01-07 21:47:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,223 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 14,592 KB
最終ジャッジ日時 2024-04-26 18:35:45
合計ジャッジ時間 28,410 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,880 KB
testcase_01 AC 1,937 ms
14,336 KB
testcase_02 AC 1,885 ms
14,464 KB
testcase_03 AC 1,897 ms
14,464 KB
testcase_04 AC 1,909 ms
14,336 KB
testcase_05 AC 1,872 ms
14,208 KB
testcase_06 AC 1,853 ms
14,208 KB
testcase_07 AC 1,894 ms
14,464 KB
testcase_08 AC 1,891 ms
14,336 KB
testcase_09 AC 1,882 ms
14,336 KB
testcase_10 AC 1,884 ms
14,336 KB
testcase_11 AC 1,727 ms
14,208 KB
testcase_12 AC 1,752 ms
14,336 KB
testcase_13 AC 1,779 ms
14,336 KB
testcase_14 TLE -
testcase_15 AC 31 ms
10,880 KB
testcase_16 AC 30 ms
10,880 KB
testcase_17 AC 32 ms
11,008 KB
testcase_18 AC 30 ms
11,008 KB
testcase_19 AC 30 ms
11,008 KB
testcase_20 AC 30 ms
10,880 KB
testcase_21 AC 30 ms
11,008 KB
testcase_22 AC 30 ms
11,008 KB
testcase_23 AC 31 ms
11,008 KB
testcase_24 AC 31 ms
11,008 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