結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-11-20 15:53:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,291 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 105,600 KB
最終ジャッジ日時 2024-04-26 18:13:55
合計ジャッジ時間 11,171 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
51,712 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 WA -
testcase_15 WA -
testcase_16 AC 39 ms
52,480 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 41 ms
52,352 KB
testcase_20 AC 40 ms
52,480 KB
testcase_21 AC 38 ms
52,608 KB
testcase_22 AC 38 ms
52,480 KB
testcase_23 AC 40 ms
52,864 KB
testcase_24 AC 39 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinaryIndexedTree():
	def __init__(self, n: int) -> None:
		self.n = 1 << (n.bit_length())
		self.BIT = [0] * (self.n + 1)

	def build(self, init_lis: list) -> None:
		for i, v in enumerate(init_lis):
			self.add(i, v)

	def add(self, i: int, x: int) -> None:
		i += 1
		while i <= self.n:
			self.BIT[i] += x
			i += i & -i
	
	def sum(self, l: int, r: int) -> int:
		return self._sum(r) - self._sum(l)

	def _sum(self, i: int) -> int:
		res = 0
		while i > 0:
			res += self.BIT[i]
			i -= i & -i
		return res

n, q = map(int, input().split())
s = input()
pos_lis = []
brac_lis = []
for i in range(n):
	if i + 1 < n and s[i] == "(" and s[i + 1] == ")": pos_lis.append(1)
	else: pos_lis.append(0)
	if s[i] == "(": brac_lis.append(1)
	else: brac_lis.append(0)
BIT = BinaryIndexedTree(n)
BIT.build(pos_lis)
for _ in range(q):
	t, *Query = map(int, input().split())
	if t == 1:
		i = Query[0]
		i -= 1
		if brac_lis[i] == 0:
			brac_lis[i] = 1
			if i - 1 >= 0 and brac_lis[i - 1] == 1:
				BIT.add(i - 1, -1)
			if i + 1 < n and brac_lis[i + 1] == 0:
				BIT.add(i, 1)
		else:
			brac_lis[i] = 0
			if i + 1 < n and brac_lis[i + 1] == 0:
				BIT.add(i, -1)
			if i - 1 >= 0 and brac_lis[i - 1] == 1:
				BIT.add(i - 1, 1)
	else:
		l, r = Query
		l -= 1; r -= 1
		print(BIT.sum(l, r + 1))
0