結果

問題 No.1099 Range Square Sum
ユーザー tamatotamato
提出日時 2020-09-25 11:24:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 846 ms / 2,000 ms
コード長 4,001 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 144,152 KB
最終ジャッジ日時 2024-06-28 05:47:28
合計ジャッジ時間 9,893 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,752 KB
testcase_01 AC 39 ms
54,788 KB
testcase_02 AC 43 ms
53,404 KB
testcase_03 AC 40 ms
53,364 KB
testcase_04 AC 39 ms
54,896 KB
testcase_05 AC 39 ms
54,064 KB
testcase_06 AC 39 ms
53,368 KB
testcase_07 AC 40 ms
54,772 KB
testcase_08 AC 39 ms
53,472 KB
testcase_09 AC 39 ms
54,352 KB
testcase_10 AC 39 ms
53,520 KB
testcase_11 AC 102 ms
77,172 KB
testcase_12 AC 110 ms
77,496 KB
testcase_13 AC 97 ms
76,936 KB
testcase_14 AC 100 ms
76,928 KB
testcase_15 AC 103 ms
76,868 KB
testcase_16 AC 104 ms
76,808 KB
testcase_17 AC 109 ms
77,348 KB
testcase_18 AC 103 ms
76,976 KB
testcase_19 AC 103 ms
76,756 KB
testcase_20 AC 103 ms
77,056 KB
testcase_21 AC 822 ms
144,152 KB
testcase_22 AC 811 ms
142,628 KB
testcase_23 AC 834 ms
144,012 KB
testcase_24 AC 846 ms
143,956 KB
testcase_25 AC 828 ms
142,556 KB
testcase_26 AC 485 ms
127,676 KB
testcase_27 AC 479 ms
127,120 KB
testcase_28 AC 497 ms
127,176 KB
testcase_29 AC 481 ms
127,268 KB
testcase_30 AC 482 ms
127,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9
mask = (1 << 60) - 1


def main():
    import sys
    input = sys.stdin.buffer.readline

    def op(a, b):
        return (a[0] + b[0], a[1] + b[1])

    e = (0, 0)

    def mapping(a, x, seg_len):
        sqsum, sum_ = a
        sqsum_new = sqsum + seg_len * (x ** 2) + 2 * x * sum_
        sum_new = sum_ + seg_len * x
        return (sqsum_new, sum_new)

    def composition(x, y):
        return x + y

    id = 0

    class LazySegTree:
        # Range update query
        def __init__(self, A, op=op, e=e, mapping=mapping, composition=composition, id=id, initialize=True):
            self.N = len(A)
            self.LV = (self.N - 1).bit_length()
            self.N0 = 1 << self.LV
            self.op = op
            self.e = e
            self.mapping = mapping
            self.composition = composition
            self.id = id
            if initialize:
                self.data = [self.e] * self.N0 + A + [self.e] * (self.N0 - self.N)
                for i in range(self.N0 - 1, 0, -1):
                    self.data[i] = op(self.data[i * 2], self.data[i * 2 + 1])
            else:
                self.data = [self.e] * (self.N0 * 2)
            self.lazy = [id] * (self.N0 * 2)

        def _ascend(self, i):
            for _ in range(i.bit_length() - 1):
                i >>= 1
                self.data[i] = self.op(self.data[i * 2], self.data[i * 2 + 1])

        def _descend(self, idx):
            lv = idx.bit_length()
            seg_len = 1 << self.LV
            for j in range(lv - 1, 0, -1):
                seg_len >>= 1
                i = idx >> j
                x = self.lazy[i]
                if x == self.id:
                    continue
                self.lazy[i * 2] = self.composition(self.lazy[i * 2], x)
                self.lazy[i * 2 + 1] = self.composition(self.lazy[i * 2 + 1], x)
                self.lazy[i] = self.id
                self.data[i * 2] = self.mapping(self.data[i * 2], x, seg_len)
                self.data[i * 2 + 1] = self.mapping(self.data[i * 2 + 1], x, seg_len)

        # open interval [l, r)
        def apply(self, l, r, x):
            l += self.N0 - 1
            r += self.N0 - 1
            self._descend(l // (l & -l))
            self._descend(r // (r & -r) - 1)
            l_ori = l
            r_ori = r
            seg_len = 1
            while l < r:
                if l & 1:
                    self.data[l] = self.mapping(self.data[l], x, seg_len)
                    self.lazy[l] = self.composition(self.lazy[l], x)
                    l += 1
                if r & 1:
                    r -= 1
                    self.data[r] = self.mapping(self.data[r], x, seg_len)
                    self.lazy[r] = self.composition(self.lazy[r], x)
                l >>= 1
                r >>= 1
                seg_len <<= 1
            self._ascend(l_ori // (l_ori & -l_ori))
            self._ascend(r_ori // (r_ori & -r_ori) - 1)

        # open interval [l, r)
        def query(self, l, r):
            l += self.N0 - 1
            r += self.N0 - 1
            self._descend(l // (l & -l))
            self._descend(r // (r & -r) - 1)
            ret_l = self.e
            ret_r = self.e
            while l < r:
                if l & 1:
                    ret_l = self.op(ret_l, self.data[l])
                    l += 1
                if r & 1:
                    ret_r = self.op(self.data[r - 1], ret_r)
                    r -= 1
                l >>= 1
                r >>= 1
            return self.op(ret_l, ret_r)

    N = int(input())
    A = list(map(int, input().split()))
    B = [(a*a, a) for a in A]
    ST = LazySegTree(B)
    ans = []
    for _ in range(int(input())):
        query = list(map(int, input().split()))
        if query[0] == 1:
            l, r, x = query[1:]
            ST.apply(l, r+1, x)
        else:
            l, r = query[1:]
            ans.append(ST.query(l, r+1)[0])
    print(*ans, sep="\n")


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