結果

問題 No.1096 Range Sums
ユーザー c-yanc-yan
提出日時 2020-06-28 15:46:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,067 ms / 2,000 ms
コード長 1,374 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 10,972 KB
実行使用メモリ 29,648 KB
最終ジャッジ日時 2023-09-22 02:02:38
合計ジャッジ時間 6,529 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,364 KB
testcase_01 AC 17 ms
8,308 KB
testcase_02 AC 17 ms
8,364 KB
testcase_03 AC 17 ms
8,248 KB
testcase_04 AC 18 ms
8,304 KB
testcase_05 AC 18 ms
8,272 KB
testcase_06 AC 18 ms
8,412 KB
testcase_07 AC 18 ms
8,244 KB
testcase_08 AC 18 ms
8,320 KB
testcase_09 AC 18 ms
8,244 KB
testcase_10 AC 1,066 ms
29,536 KB
testcase_11 AC 1,048 ms
29,648 KB
testcase_12 AC 1,067 ms
29,400 KB
testcase_13 AC 1,052 ms
29,404 KB
testcase_14 AC 1,066 ms
29,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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 build(self, iterable):
        f = self._f
        data = self._data
        data[self._offset:self._offset + self._size] = iterable
        for i in range(self._offset - 1, -1, -1):
            data[i] = f(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.build(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)
0