結果

問題 No.1099 Range Square Sum
ユーザー 双六双六
提出日時 2020-09-16 01:47:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 657 ms / 2,000 ms
コード長 2,990 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 82,284 KB
実行使用メモリ 129,480 KB
最終ジャッジ日時 2024-06-22 02:56:29
合計ジャッジ時間 8,649 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,316 KB
testcase_01 AC 35 ms
52,880 KB
testcase_02 AC 35 ms
53,352 KB
testcase_03 AC 35 ms
54,660 KB
testcase_04 AC 35 ms
53,596 KB
testcase_05 AC 34 ms
53,376 KB
testcase_06 AC 35 ms
53,080 KB
testcase_07 AC 34 ms
52,828 KB
testcase_08 AC 35 ms
54,256 KB
testcase_09 AC 34 ms
53,548 KB
testcase_10 AC 33 ms
54,356 KB
testcase_11 AC 100 ms
77,152 KB
testcase_12 AC 102 ms
77,040 KB
testcase_13 AC 98 ms
77,072 KB
testcase_14 AC 98 ms
77,140 KB
testcase_15 AC 100 ms
77,084 KB
testcase_16 AC 97 ms
77,036 KB
testcase_17 AC 96 ms
77,344 KB
testcase_18 AC 96 ms
77,268 KB
testcase_19 AC 101 ms
77,148 KB
testcase_20 AC 97 ms
77,064 KB
testcase_21 AC 613 ms
129,032 KB
testcase_22 AC 657 ms
129,008 KB
testcase_23 AC 623 ms
129,136 KB
testcase_24 AC 611 ms
129,476 KB
testcase_25 AC 613 ms
129,172 KB
testcase_26 AC 518 ms
129,284 KB
testcase_27 AC 489 ms
129,012 KB
testcase_28 AC 518 ms
129,480 KB
testcase_29 AC 492 ms
128,892 KB
testcase_30 AC 510 ms
129,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
INF = float("inf")

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

class lazySegTree(object):
	def __init__(self, N):
		self.N = N
		self.LV = (N - 1).bit_length()
		self.N0 = 2 ** self.LV
		self.size = [1] * (2 * self.N0)
		self.ans = [0] * (2 * self.N0)
		self.data = [0] * (2 * self.N0)
		self.lazy = [0] * (2 * self.N0)
		self.lazy2 = [0] * (2 * self.N0)
		self.lazy3 = [0] * (2 * self.N0)
		for i in range(self.N0 - 2, -1, -1):
			self.size[i] = self.size[2 * i + 1] + self.size[2 * i + 2]

	def initialize(self, A):
		for i in range(self.N):
			self.data[self.N0 - 1 + i] = A[i]
			self.ans[self.N0 - 1 + i] = A[i] ** 2
		for i in range(self.N0 - 2, -1, -1):
			self.data[i] = self.data[2 * i + 1] + self.data[2 * i + 2]
			self.ans[i] = self.ans[2 * i + 1] + self.ans[2 * i + 2]

	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):
			v = self.lazy[i - 1]
			if not v:
				continue
			self.ans[2 * i - 1] += 2 * v * self.data[2 * i - 1] + self.size[2 * i - 1] * (v ** 2)
			self.ans[2 * i] += 2 * v * self.data[2 * i] + self.size[2 * i] * (v ** 2)
			self.data[2 * i - 1] += v * self.size[2 * i - 1]
			self.data[2 * i] += v * self.size[2 * i]
			self.lazy[2 * i - 1] += v
			self.lazy[2 * i] += v
			self.lazy[i - 1] = 0

	def add(self, l, r, x):
		*ids, = self.gindex(l, r + 1)
		self.propagates(*ids)

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

	def query(self, l, r):
		self.propagates(*self.gindex(l, r + 1))
		L = self.N0 + l; R = self.N0 + r + 1

		s = 0
		while L < R:
			if R & 1:
				R -= 1
				s += self.ans[R - 1]
			if L & 1:
				s += self.ans[L - 1]
				L += 1
			L >>= 1; R >>= 1
		return s

	def debug(self):
		ids = [i for i in range(self.N0, 0, -1)]
		self.propagates(*ids)
		print(self.data[self.N0 - 1:])
		print(self.ans[self.N0 - 1:])
		print()

def main():
	N = int(input())
	A = getlist()
	lSeg = lazySegTree(N)
	lSeg.initialize(A)
	Q = int(input())
	for i in range(Q):
		q = getlist()
		if q[0] == 1:
			_, l, r, x = q
			l -= 1; r -= 1
			lSeg.add(l, r, x)
		else:
			_, l, r = q
			l -= 1; r -= 1
			ans = lSeg.query(l, r)
			print(ans)

		# lSeg.debug()


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