結果

問題 No.1099 Range Square Sum
ユーザー brthyyjpbrthyyjp
提出日時 2022-01-02 15:35:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 872 ms / 2,000 ms
コード長 3,509 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 134,016 KB
最終ジャッジ日時 2024-10-11 18:50:45
合計ジャッジ時間 12,679 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
52,864 KB
testcase_01 AC 43 ms
52,736 KB
testcase_02 AC 43 ms
52,736 KB
testcase_03 AC 42 ms
52,864 KB
testcase_04 AC 43 ms
52,352 KB
testcase_05 AC 42 ms
53,120 KB
testcase_06 AC 44 ms
52,864 KB
testcase_07 AC 42 ms
52,352 KB
testcase_08 AC 43 ms
52,608 KB
testcase_09 AC 43 ms
52,992 KB
testcase_10 AC 43 ms
52,480 KB
testcase_11 AC 123 ms
77,568 KB
testcase_12 AC 127 ms
76,504 KB
testcase_13 AC 123 ms
76,508 KB
testcase_14 AC 122 ms
76,508 KB
testcase_15 AC 128 ms
77,032 KB
testcase_16 AC 121 ms
76,288 KB
testcase_17 AC 123 ms
77,056 KB
testcase_18 AC 124 ms
77,184 KB
testcase_19 AC 119 ms
76,360 KB
testcase_20 AC 122 ms
76,928 KB
testcase_21 AC 864 ms
133,760 KB
testcase_22 AC 865 ms
134,016 KB
testcase_23 AC 855 ms
133,632 KB
testcase_24 AC 872 ms
133,888 KB
testcase_25 AC 862 ms
133,760 KB
testcase_26 AC 586 ms
132,992 KB
testcase_27 AC 591 ms
133,120 KB
testcase_28 AC 597 ms
132,608 KB
testcase_29 AC 584 ms
132,864 KB
testcase_30 AC 592 ms
133,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class LazySegTree:
    def __init__(self, init_val, seg_ide, lazy_ide, f, g, h):
        self.n = len(init_val)
        self.num = 2**(self.n-1).bit_length()

        self.seg_ide = seg_ide
        self.lazy_ide = lazy_ide

        self.f = f
        self.g = g
        self.h = h

        self.seg = [seg_ide]*2*self.num
        for i in range(self.n):
            self.seg[i+self.num] = init_val[i]
        for i in range(self.num-1, 0, -1):
            self.seg[i] = self.f(self.seg[2*i], self.seg[2*i+1])

        self.size = [0]*2*self.num
        for i in range(self.n):
            self.size[i+self.num] = 1
        for i in range(self.num-1, 0, -1):
            self.size[i] = self.size[2*i] + self.size[2*i+1]

        self.lazy = [lazy_ide]*2*self.num

    def update(self, i, x):
        i += self.num
        self.seg[i] = x
        while i:
            i = i >> 1
            self.seg[i] = self.f(self.seg[2*i], self.seg[2*i+1])

    def calc(self, i):
        return self.g(self.seg[i], self.lazy[i], self.size[i])

    def calc_above(self, i):
        i = i >> 1
        while i:
            self.seg[i] = self.f(self.calc(2*i), self.calc(2*i+1))
            i = i >> 1

    def propagate(self, i):
        self.seg[i] = self.g(self.seg[i], self.lazy[i], self.size[i])
        self.lazy[2*i] = self.h(self.lazy[2*i], self.lazy[i])
        self.lazy[2*i+1] = self.h(self.lazy[2*i+1], self.lazy[i])
        self.lazy[i] = self.lazy_ide

    def propagate_above(self, i):
        H = i.bit_length()
        for h in range(H, 0, -1):
            self.propagate(i >> h)

    def query(self, l, r):
        l += self.num
        r += self.num
        lm = l // (l & -l)
        rm = r // (r & -r) -1
        self.propagate_above(lm)
        self.propagate_above(rm)

        al = self.seg_ide
        ar = self.seg_ide
        while l < r:
            if l & 1:
                al = self.f(al, self.calc(l))
                l += 1
            if r & 1:
                r -= 1
                ar = self.f(self.calc(r), ar)
            l = l >> 1
            r = r >> 1
        return self.f(al, ar)

    def operate_range(self, l, r, a):
        l += self.num
        r += self.num
        lm = l // (l & -l)
        rm = r // (r & -r) -1
        self.propagate_above(lm)
        self.propagate_above(rm)
        while l < r:
            if l & 1:
                self.lazy[l] = self.h(self.lazy[l], a)
                l += 1
            if r & 1:
                r -= 1
                self.lazy[r] = self.h(self.lazy[r], a)
            l = l >> 1
            r = r >> 1

        self.calc_above(lm)
        self.calc_above(rm)

    def __str__(self): # for debug
        arr = [self.query(i,i+1) for i in range(self.n)]
        return str(arr)

def f(x, y):
    return (x[0]+y[0], x[1]+y[1])

def g(x, a, s):
    return x[0]+a*s, x[1]+2*x[0]*a+s*a**2

h = lambda a, b: a+b

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():

    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    B = []
    for a in A:
        B.append((a, a**2))
    seg = LazySegTree(B, (0, 0), 0, f, g, h)
    for i in range(q):
        temp = list(map(int, input().split()))
        if temp[0] == 1:
            l, r, x = temp[1:]
            l, r = l-1, r-1
            seg.operate_range(l, r+1, x)
        else:
            l, r = temp[1:]
            l, r = l-1, r-1
            print(seg.query(l, r+1)[1])

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