結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-11-20 16:42:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,984 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 16,712 KB
最終ジャッジ日時 2024-04-26 18:15:11
合計ジャッジ時間 31,306 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 1,990 ms
16,712 KB
testcase_12 AC 1,920 ms
16,712 KB
testcase_13 AC 1,889 ms
16,708 KB
testcase_14 TLE -
testcase_15 AC 29 ms
10,880 KB
testcase_16 AC 29 ms
10,880 KB
testcase_17 AC 29 ms
10,880 KB
testcase_18 AC 28 ms
10,880 KB
testcase_19 AC 27 ms
10,880 KB
testcase_20 AC 29 ms
11,008 KB
testcase_21 AC 30 ms
10,880 KB
testcase_22 AC 29 ms
10,880 KB
testcase_23 AC 28 ms
10,880 KB
testcase_24 AC 28 ms
11,008 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: # i 文字目が ) のとき
			brac_lis[i] = 1 # ( に変更
			if i - 1 >= 0 and brac_lis[i - 1] == 1: # i - 1 文字目が ( であったとき
				BIT.add(i - 1, -1) # () が 1 つ減るので、i - 1 番目から 1 を引く
			if i + 1 < n and brac_lis[i + 1] == 0: # i + 1 文字目が ) であったとき
				BIT.add(i, 1) # () が 1 つ増えるので、i 番目の場所に記録しておく
		else: # i 文字目が ( のとき
			brac_lis[i] = 0 # ) に変更
			if i + 1 < n and brac_lis[i + 1] == 0: # i + 1 文字目が ) であったとき
				BIT.add(i, -1) # () が 1 つ減るので、i 番目から 1 を引く
			if i - 1 >= 0 and brac_lis[i - 1] == 1: # i - 1 文字目が ( であったとき
				BIT.add(i - 1, 1) # () が 1 つ増えるので、i - 1 番目の場所に記録しておく
	else:
		l, r = Query
		l -= 1; r -= 1
		print(BIT.sum(l, r)) # l 文字目から r 文字目まで見ているが、() の位置は ( の方で管理しているので、a[l]+a[l+1]+...+a[r-1] でいい
0