結果

問題 No.1099 Range Square Sum
ユーザー 👑 rin204rin204
提出日時 2022-04-17 17:58:53
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,105 bytes
コンパイル時間 483 ms
コンパイル使用メモリ 87,220 KB
実行使用メモリ 280,644 KB
最終ジャッジ日時 2023-08-27 03:51:06
合計ジャッジ時間 22,506 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,228 KB
testcase_01 AC 72 ms
71,584 KB
testcase_02 AC 73 ms
71,124 KB
testcase_03 AC 75 ms
71,540 KB
testcase_04 AC 74 ms
71,320 KB
testcase_05 AC 72 ms
71,284 KB
testcase_06 AC 73 ms
71,356 KB
testcase_07 AC 72 ms
71,176 KB
testcase_08 AC 72 ms
71,512 KB
testcase_09 AC 72 ms
71,364 KB
testcase_10 AC 72 ms
71,308 KB
testcase_11 AC 196 ms
79,044 KB
testcase_12 AC 201 ms
79,656 KB
testcase_13 AC 196 ms
79,328 KB
testcase_14 AC 190 ms
78,736 KB
testcase_15 AC 196 ms
79,052 KB
testcase_16 AC 200 ms
80,004 KB
testcase_17 AC 196 ms
78,596 KB
testcase_18 AC 194 ms
79,040 KB
testcase_19 AC 192 ms
79,268 KB
testcase_20 AC 190 ms
78,852 KB
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 1,070 ms
157,112 KB
testcase_27 AC 1,095 ms
157,480 KB
testcase_28 AC 1,078 ms
158,636 KB
testcase_29 AC 1,098 ms
158,392 KB
testcase_30 AC 1,084 ms
156,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
a <- a + x
a^2 <- a^2 + 2ax + x^2
"""

class lazy_segtree():
    def __init__(self, lst, ope, e, mapping, composition, id_):
        self.n = len(lst)
        self.log = (self.n - 1).bit_length()
        self.size = 1 << self.log
        self.data = [e for _ in range(2 * self.size)]
        self.lz = [id_ for _ in range(self.size)]
        self.e = e
        self.op = ope
        self.mapping = mapping
        self.composition = composition
        self.identity = id_
        for i in range(self.n):
            self.data[self.size + i] = lst[i]
        for i in range(self.size - 1, 0, -1):
            self.update(i)
        
    def update(self, k):
        self.data[k] = self.op(self.data[2 * k], self.data[2 * k + 1])
        
    def all_apply(self, k, f):
        self.data[k] = self.mapping(f, self.data[k])
        if k < self.size:
            self.lz[k] = self.composition(f, self.lz[k])

    def push(self, k):
        self.all_apply(2 * k, self.lz[k])
        self.all_apply(2 * k + 1, self.lz[k])
        self.lz[k] = self.identity

    def set(self, p, x):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        self.data[p] = x
        for i in range(1, self.log + 1):
            self.update(p >> i)

    def get(self, p):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        return self.data[p]

    def prod(self, l, r):
        if l == r: return self.e
        l += self.size
        r += self.size
        for i in range(self.log, 0, -1):
            if (l >> i) << i != l:
                self.push(l >> i)
            if (r >> i) << i != r:
                self.push(r >> i)
        sml, smr = self.e, self.e
        while l < r:
            if l & 1:
                sml = self.op(sml, self.data[l])
                l += 1
            if r & 1:
                r -= 1
                smr = self.op(self.data[r], smr)
            l >>= 1
            r >>= 1
        return self.op(sml, smr)

    def all_prod(self):
        return self.data[1]

    def apply_point(self, p, f):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        self.data[p] = self.mapping(f, self.data[p])
        for i in range(1, self.log + 1):
            self.update(p >> i)

    def apply(self, l, r, f):
        if l == r: return
        l += self.size
        r += self.size
        for i in range(self.log, 0, -1):
            if (l >> i) << i != l:
                self.push(l >> i)
            if (r >> i) << i != r:
                self.push((r - 1) >> i)
        l2, r2 = l, r
        while l < r:
            if l & 1:
                self.all_apply(l, f)
                l += 1
            if r & 1:
                r -= 1
                self.all_apply(r, f)
            l >>= 1
            r >>= 1
        l, r = l2, r2
        for i in range(1, self.log + 1):
            if (l >> i) << i != l:
                self.update(l >> i)
            if (r >> i) << i != r:
                self.update((r - 1) >> i)

    def max_right(self, l, g):
        if l == self.n: return self.n
        l += self.size
        for i in range(self.log, 0, -1):
            self.push(l >> i)
        sm = self.e
        while 1:
            while i % 2 == 0:
                l >>= 1
            if not g(self.op(sm, self.data[l])):
                while l < self.size:
                    self.push(l)
                    l *= 2
                    if g(self.op(sm, self.data[l])):
                        sm = self.op(sm, self.data[l])
                        l += 1
                return l - self.size
            sm = self.op(sm, self.data[l])
            l += 1
            if l & -l == l:
                break
        return self.n

    def min_left(self, r, g):
        if r == 0:
            return 0
        r += self.size
        for i in range(self.log, 0, -1):
            self.push((r - 1) >> i)
        sm = self.e
        while 1:
            r -= 1
            while r > 1 and r % 2 == 1:
                r >>= 1
            if not g(self.op(self.data[r], sm)):
                while r < self.size:
                    self.push(r)
                    r = 2 * r + 1
                    if g(self.op(self.data[r], sm)):
                        sm = self.op(self.data[r], sm)
                        r -= 1
                return r + 1 - self.size
            sm = self.op(self.data[r], sm)
            if r & -r == r:
                break
        return 0



n = int(input())
A = list(map(int, input().split()))

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

e = (0, 0, 0)

def mapping(f, x):
    return (x[0], x[1] + f * x[0], x[2] + 2 * x[1] * f + x[0] * f * f)

def composition(f, g):
    return f + g

seg = lazy_segtree([(1, a, a * a) for a in A], ope, e, mapping, composition, 0)

Q = int(input())
for _ in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        l, r, x = query[1:]
        seg.apply(l - 1, r, x)
    else:
        l, r = query[1:]
        print(seg.prod(l - 1, r)[2])


0