結果

問題 No.3652 Range Bracket Sequence
コンテスト
ユーザー p-adic
提出日時 2026-07-22 16:09:11
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,048 ms / 2,000 ms
+ 207µs
コード長 1,581 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 255 ms
コンパイル使用メモリ 96,364 KB
実行使用メモリ 207,444 KB
最終ジャッジ日時 2026-08-28 21:07:11
合計ジャッジ時間 36,030 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def copy(n):return n.copy()if hasattr(n,"copy")else n
def rec_str(a):return"".join(["[",", ".join(rec_str(x)for x in a),"]"])if isinstance(a,list)else str(a)
class SegmentTree:
	e=(0,0,0) #User's definition
	def op(x,y): #User's definition
		lr=min(x[2],y[0])
		return(x[0]+y[0]-lr,x[1]+y[1]+lr,x[2]+y[2]-lr)

	def __init__(self,x):
		b=isinstance(x,int)
		if b:self.N=x
		else:self.N=len(x)
		self.p=1
		while self.N>self.p:self.p<<=1
		if b:self.T=[copy(__class__.e)for i in R(self.p<<1)]
		else:
			self.T=[__class__.e]*(self.p<<1)
			for i in R(0,self.N):self.T[self.p|i]=copy(x[i])
			for j in R(self.p-1,0,-1):self.T[j]=__class__.op(self.T[j<<1],self.T[(j<<1)|1])

	def copy(self):
		a=__class__([])
		a.N=self.N
		a.p=self.p
		a.T=copy(self.T)
		return a

	def Set(self,i,u):
		assert 0<=i<self.N
		j=self.p|i
		self.T[j],j=u,j>>1
		while j:self.T[j],j=__class__.op(self.T[j<<1],self.T[(j<<1)|1]),j>>1

	def Get(self,i):
		assert 0<=i<self.N
		return self.IntervalProduct(i,i)

	def IntervalProduct(self,l,r): #l,rは始端,終端
		l,r=max(0,l),min(r,self.N-1)
		assert l-1<=r
		l|=self.p
		r+=self.p+1
		a,b=copy(__class__.e),copy(__class__.e)
		while l<r:
			if l&1:a,l=__class__.op(a,self.T[l]),l+1
			if r&1:b,r=__class__.op(self.T[r-1],b),r-1
			l,r=l>>1,r>>1
		return __class__.op(a,b)


R=range
I=input
J=lambda:map(int,I().split())
N,Q=J()
S=[c for c in I()]
def T(c):return(c==')',0,c=='(')
X=SegmentTree([T(c)for c in S])
for q in R(Q):
	t,l,r=J()
	if t<2:
		c="()"[r-1]
		if S[l-1]!=c:
			S[l-1]=c
			X.Set(l-1,T(c))
	else:print(X.IntervalProduct(l-1,r-1)[1]*2)
0