結果

問題 No.1096 Range Sums
ユーザー c-yanc-yan
提出日時 2020-06-28 15:28:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,102 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 31,516 KB
最終ジャッジ日時 2023-12-26 12:10:50
合計ジャッジ時間 7,001 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,112 KB
testcase_01 AC 34 ms
10,112 KB
testcase_02 AC 32 ms
10,112 KB
testcase_03 AC 32 ms
10,112 KB
testcase_04 AC 32 ms
10,112 KB
testcase_05 AC 32 ms
10,112 KB
testcase_06 AC 32 ms
10,112 KB
testcase_07 AC 31 ms
10,112 KB
testcase_08 AC 31 ms
10,112 KB
testcase_09 AC 31 ms
10,112 KB
testcase_10 AC 1,100 ms
31,516 KB
testcase_11 AC 1,102 ms
31,516 KB
testcase_12 AC 1,100 ms
31,516 KB
testcase_13 AC 1,093 ms
31,516 KB
testcase_14 AC 1,099 ms
31,516 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):
        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.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