結果

問題 No.876 Range Compress Query
ユーザー 双六双六
提出日時 2020-07-24 17:04:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 953 ms / 2,000 ms
コード長 3,206 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 94,888 KB
最終ジャッジ日時 2023-09-07 20:11:20
合計ジャッジ時間 10,445 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,584 KB
testcase_01 AC 145 ms
78,496 KB
testcase_02 AC 107 ms
77,496 KB
testcase_03 AC 150 ms
78,580 KB
testcase_04 AC 118 ms
77,604 KB
testcase_05 AC 114 ms
77,544 KB
testcase_06 AC 139 ms
78,808 KB
testcase_07 AC 136 ms
78,572 KB
testcase_08 AC 139 ms
78,488 KB
testcase_09 AC 141 ms
78,532 KB
testcase_10 AC 138 ms
78,512 KB
testcase_11 AC 925 ms
93,144 KB
testcase_12 AC 822 ms
94,224 KB
testcase_13 AC 832 ms
93,740 KB
testcase_14 AC 927 ms
94,820 KB
testcase_15 AC 767 ms
92,824 KB
testcase_16 AC 948 ms
94,732 KB
testcase_17 AC 931 ms
94,888 KB
testcase_18 AC 953 ms
94,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class lazySegTree(object):
	# N:処理する区間の長さ
	def __init__(self, N):
		self.N = N
		self.LV = (N - 1).bit_length()
		self.N0 = 2 ** self.LV
		self.data = [0] * (2 * self.N0)
		self.lazy = [0] * (2 * self.N0)

	# 遅延伝播を行うindexを生成
	def gindex(self, l, r):
		L = (l + self.N0) >> 1; R = (r + self.N0) >> 1
		lc = 0 if l & 1 else (L & -L).bit_length()
		rc = 0 if r & 1 else (R & -R).bit_length()
		for i in range(self.LV):
			if rc <= i:
				yield R
			if L < R and lc <= i:
				yield L
			L >>= 1; R >>= 1

	# 遅延伝搬処理
	def propagates(self, *ids):
		for i in reversed(ids):
			# print(ids)
			v = self.lazy[i-1]
			if not v:
				continue
			self.lazy[2 * i - 1] += v; self.lazy[2 * i] += v
			self.data[2 * i - 1] += v; self.data[2 * i] += v
			self.lazy[i - 1] = 0

	# 区間[l, r]にxを加算
	def update(self, l, r, x):
		*ids, = self.gindex(l, r)
		self.propagates(*ids)

		L = self.N0 + l; R = self.N0 + r
		while L < R:
			if R & 1:
				R -= 1
				self.lazy[R - 1] += x; self.data[R - 1] += x
			if L & 1:
				self.lazy[L - 1] += x; self.data[L - 1] += x
				L += 1
			L >>= 1; R >>= 1
		for i in ids:
			self.data[i - 1] = min(self.data[2 * i - 1], self.data[2 * i])

	# 区間[l, r]内の最小値を求める
	def query(self, l, r):
		self.propagates(*self.gindex(l, r))
		L = self.N0 + l; R = self.N0 + r

		s = INF
		while L < R:
			if R & 1:
				R -= 1
				s = min(s, self.data[R - 1])
			if L & 1:
				s = min(s, self.data[L - 1])
				L += 1
			L >>= 1; R >>= 1
		return s

class SegmentTree(object):
	#N:処理する区間の長さ
	def __init__(self, N):
		self.N0 = 2 ** (N - 1).bit_length()
		self.data = [0] * (2 * self.N0)

	#k番目の値をxに更新
	def update(self, k, x):
		k += self.N0 - 1
		self.data[k] = x
		while k > 0:
			k = (k - 1) // 2
			self.data[k] = self.data[2 * k + 1] + self.data[2 * k + 2]

	def query(self, l, r):
		L = l + self.N0; R = r + self.N0 + 1
		m = 0
		while L < R:
			if R & 1:
				R -= 1
				m = m + self.data[R - 1]
			if L & 1:
				m = m + self.data[L - 1]
				L += 1
			L >>= 1; R >>= 1

		return m

#処理内容
def main():
	N, Q = getlist()
	A = getlist()
	# セグ木初期化
	Seg = SegmentTree(N)
	for i in range(N - 1):
		if A[i] != A[i + 1]:
			Seg.update(i, 1)

	# print(Seg.data)

	lazySeg = lazySegTree(N)
	for i in range(N):
		lazySeg.update(i, i + 1, A[i])

	for i in range(Q):
		q = getlist()
		if q[0] == 1:
			l, r, x = q[1], q[2], q[3]
			l -= 1; r -= 1
			if l > 0:
				a = lazySeg.query(l - 1, l)
				b = lazySeg.query(l, l + 1)
				if a == b + x:
					Seg.update(l - 1, 0)
				if a == b:
					Seg.update(l - 1, 1)
			if r < N - 1:
				a = lazySeg.query(r, r + 1)
				b = lazySeg.query(r + 1, r + 2)
				if a + x == b:
					Seg.update(r, 0)
				if a == b:
					Seg.update(r, 1)

			lazySeg.update(l, r + 1, x)

		else:
			l, r = q[1], q[2]
			l -= 1; r -= 1
			if l == r:
				print(1)
			else:
				ans = Seg.query(l, r - 1)
				print(ans + 1)



if __name__ == '__main__':
	main()
0