結果
問題 | No.1096 Range Sums |
ユーザー | c-yan |
提出日時 | 2020-06-28 15:24:36 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,065 ms / 2,000 ms |
コード長 | 1,335 bytes |
コンパイル時間 | 96 ms |
コンパイル使用メモリ | 11,008 KB |
実行使用メモリ | 29,500 KB |
最終ジャッジ日時 | 2023-09-22 01:29:28 |
合計ジャッジ時間 | 6,709 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 17 ms
8,336 KB |
testcase_01 | AC | 17 ms
8,380 KB |
testcase_02 | AC | 21 ms
8,396 KB |
testcase_03 | AC | 18 ms
8,348 KB |
testcase_04 | AC | 18 ms
8,384 KB |
testcase_05 | AC | 17 ms
8,328 KB |
testcase_06 | AC | 17 ms
8,352 KB |
testcase_07 | AC | 17 ms
8,276 KB |
testcase_08 | AC | 17 ms
8,380 KB |
testcase_09 | AC | 17 ms
8,336 KB |
testcase_10 | AC | 1,051 ms
29,364 KB |
testcase_11 | AC | 1,053 ms
29,424 KB |
testcase_12 | AC | 1,060 ms
29,480 KB |
testcase_13 | AC | 1,065 ms
29,500 KB |
testcase_14 | AC | 1,057 ms
29,404 KB |
ソースコード
# Segment tree (+) from operator import add from itertools import accumulate class SegmentTree: _f = None _data = None _offset = None _size = None def __init__(self, size, f): self._f = f self._size = size t = 1 while t < size: t *= 2 self._offset = t - 1 self._data = [0] * (t * 2 - 1) def update_all(self, iterable): data = self._data data[self._offset:self._offset + self._size] = iterable for i in range(self._offset - 1, -1, -1): data[i] = data[i * 2 + 1] + data[i * 2 + 2] def query(self, start, stop): def iter_segments(data, l, r): while l < r: if l & 1 == 0: yield data[l] if r & 1 == 0: yield data[r - 1] l = l // 2 r = (r - 1) // 2 f = self._f it = iter_segments(self._data, start + self._offset, stop + self._offset) result = next(it) for e in it: result = f(result, e) return result N, *A = map(int, open(0).read().split()) a = list(accumulate(A)) st = SegmentTree(N, add) st.update_all(a) result = 0 result += st.query(0, N) for i in range(1, N): result += st.query(i, N) - a[i - 1] * (N - i) print(result)