結果
問題 | No.1099 Range Square Sum |
ユーザー | rlangevin |
提出日時 | 2023-12-13 12:27:21 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,935 ms / 2,000 ms |
コード長 | 4,499 bytes |
コンパイル時間 | 513 ms |
コンパイル使用メモリ | 82,312 KB |
実行使用メモリ | 240,848 KB |
最終ジャッジ日時 | 2024-09-27 05:20:28 |
合計ジャッジ時間 | 16,704 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,960 KB |
testcase_01 | AC | 40 ms
53,420 KB |
testcase_02 | AC | 42 ms
53,976 KB |
testcase_03 | AC | 41 ms
53,200 KB |
testcase_04 | AC | 43 ms
54,472 KB |
testcase_05 | AC | 40 ms
54,580 KB |
testcase_06 | AC | 41 ms
54,304 KB |
testcase_07 | AC | 40 ms
54,108 KB |
testcase_08 | AC | 60 ms
54,012 KB |
testcase_09 | AC | 42 ms
54,812 KB |
testcase_10 | AC | 40 ms
53,544 KB |
testcase_11 | AC | 111 ms
76,852 KB |
testcase_12 | AC | 124 ms
76,904 KB |
testcase_13 | AC | 110 ms
76,716 KB |
testcase_14 | AC | 118 ms
76,920 KB |
testcase_15 | AC | 115 ms
77,416 KB |
testcase_16 | AC | 112 ms
76,808 KB |
testcase_17 | AC | 120 ms
77,344 KB |
testcase_18 | AC | 116 ms
76,996 KB |
testcase_19 | AC | 117 ms
77,700 KB |
testcase_20 | AC | 122 ms
77,596 KB |
testcase_21 | AC | 1,825 ms
240,128 KB |
testcase_22 | AC | 1,782 ms
240,848 KB |
testcase_23 | AC | 1,775 ms
239,888 KB |
testcase_24 | AC | 1,935 ms
239,632 KB |
testcase_25 | AC | 1,796 ms
240,372 KB |
testcase_26 | AC | 719 ms
143,760 KB |
testcase_27 | AC | 737 ms
145,168 KB |
testcase_28 | AC | 720 ms
144,508 KB |
testcase_29 | AC | 732 ms
145,012 KB |
testcase_30 | AC | 706 ms
144,524 KB |
ソースコード
import sys input = sys.stdin.readline class LazySegmentTree: def __init__( self, n, identity_e_node, identity_e_lazy, combine_node_f, combine_lazy_f, reflect_f, ): self._n = n self._size = 1 self._height = 0 while self._size < self._n: self._size <<= 1 self._height += 1 self._identity_e_node = identity_e_node self._identity_e_lazy = identity_e_lazy self._combine_node_f = combine_node_f self._combine_lazy_f = combine_lazy_f self._reflect_f = reflect_f self._node = [self._identity_e_node] * (2 * self._size) self._lazy = [self._identity_e_lazy] * (2 * self._size) #遅延データの値を値データに反映させたときの値を返す。 def _reflect_lazy(self, index): return self._reflect_f(self._node[index], self._lazy[index]) def _propagate_from_top(self, index): index += self._size for h in range(self._height, 0, -1): i = index >> h if self._lazy[i] != self._identity_e_lazy: self._lazy[i << 1] = self._combine_lazy_f( self._lazy[i << 1], self._lazy[i] ) self._lazy[i << 1 | 1] = self._combine_lazy_f( self._lazy[i << 1 | 1], self._lazy[i] ) self._node[i] = self._reflect_lazy(i) self._lazy[i] = self._identity_e_lazy def _update_from_bottom(self, index): index = (index + self._size) >> 1 while index: self._node[index] = self._combine_node_f( self._reflect_lazy(index << 1), self._reflect_lazy(index << 1 | 1) ) index >>= 1 def build(self, array): assert len(array) == self._n for index, value in enumerate(array, start=self._size): self._node[index] = value for index in range(self._size - 1, 0, -1): self._node[index] = self._combine_node_f( self._node[index << 1], self._node[index << 1 | 1] ) # 区間更新 位置[L, R) (0-indexed)を値valueで更新 def update(self, L, R, value): self._propagate_from_top(L) self._propagate_from_top(R - 1) L_lazy = L + self._size R_lazy = R + self._size while L_lazy < R_lazy: if L_lazy & 1: self._lazy[L_lazy] = self._combine_lazy_f(self._lazy[L_lazy], value) L_lazy += 1 if R_lazy & 1: R_lazy -= 1 self._lazy[R_lazy] = self._combine_lazy_f(self._lazy[R_lazy], value) L_lazy >>= 1 R_lazy >>= 1 self._update_from_bottom(L) self._update_from_bottom(R - 1) # 区間取得 区間[L, R) (0-indexed)内の要素について # L番目から順にcombine_node_fを適用した値を返す。 def fold(self, L, R): self._propagate_from_top(L) self._propagate_from_top(R - 1) L += self._size R += self._size value_L = self._identity_e_node value_R = self._identity_e_node while L < R: if L & 1: value_L = self._combine_node_f(value_L, self._reflect_lazy(L)) L += 1 if R & 1: R -= 1 value_R = self._combine_node_f(self._reflect_lazy(R), value_R) L >>= 1 R >>= 1 return self._combine_node_f(value_L, value_R) N = int(input()) def combine_node_f(x, y): a = x[0] + y[0] b = x[1] + y[1] c = x[2] + y[2] return (a, b, c) def reflect(v, f): a = v[0] + f ** 2 * v[2] + 2 * v[1] * f b = v[1] + f * v[2] return (a, b, v[2]) from operator import add T = LazySegmentTree(N, (0, 0, 0), 0, combine_node_f, add, reflect) A = list(map(int, input().split())) for i in range(N): A[i] = (A[i] ** 2, A[i], 1) T.build(A) Q = int(input()) for _ in range(Q): q = list(map(int, input().split())) if q[0] == 1: l, r, x = q[1:] l -= 1 T.update(l, r, x) else: l, r = q[1:] l -= 1 print(T.fold(l, r)[0])