結果

問題 No.1000 Point Add and Array Add
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-04-05 21:57:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 761 ms / 2,000 ms
コード長 5,194 bytes
コンパイル時間 758 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 109,392 KB
最終ジャッジ日時 2024-05-05 10:19:36
合計ジャッジ時間 10,041 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,376 KB
testcase_01 AC 42 ms
53,504 KB
testcase_02 AC 42 ms
53,504 KB
testcase_03 AC 40 ms
53,376 KB
testcase_04 AC 39 ms
53,376 KB
testcase_05 AC 39 ms
52,992 KB
testcase_06 AC 38 ms
53,504 KB
testcase_07 AC 39 ms
53,760 KB
testcase_08 AC 40 ms
53,248 KB
testcase_09 AC 39 ms
53,888 KB
testcase_10 AC 38 ms
53,504 KB
testcase_11 AC 41 ms
53,248 KB
testcase_12 AC 157 ms
78,960 KB
testcase_13 AC 150 ms
77,736 KB
testcase_14 AC 166 ms
78,580 KB
testcase_15 AC 153 ms
78,536 KB
testcase_16 AC 566 ms
109,392 KB
testcase_17 AC 489 ms
92,928 KB
testcase_18 AC 734 ms
108,884 KB
testcase_19 AC 761 ms
108,560 KB
testcase_20 AC 685 ms
108,544 KB
testcase_21 AC 646 ms
108,500 KB
testcase_22 AC 689 ms
108,616 KB
testcase_23 AC 661 ms
108,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
class LazySegTree:

    def __init__(self, n, op, e, mapping, composition, id):
        self.n = n
        self.op = op
        self.e = e
        self.mapping = mapping
        self.composition = composition
        self.id = id
        self.log = (n-1).bit_length()
        self.size = 1 << self.log
        self.data = [e]*(2*self.size)
        self.lazy = [id]*(self.size)

    def update(self, k):
        self.data[k] = self.op(self.data[2*k],self.data[2*k+1])

    def build(self, lis):
        for i, l in enumerate(lis,self.size):
            self.data[i] = l
        for i in range(self.size-1, 0, -1):
            self.update(i)

    def point_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 point_get(self, p):
        p += self.size
        for i in range(self.log, 0,-1):
            self.push(p >> i)
        return self.data[p]

    def apply(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 range_apply(self, l, r, f):
        ''' change the value you define in "mapping" and "composition" of [l,r) '''
        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 = l
        r2 = r
        while l2 < r2:
            if l2 & 1:
                self.all_apply(l2, f)
                l2 += 1
            if r2 & 1:
                r2 -= 1
                self.all_apply(r2, f)
            l2 >>= 1
            r2 >>= 1
        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 all_apply(self, k, f):
        self.data[k] = self.mapping(f, self.data[k])
        if k < self.size:
            self.lazy[k] = self.composition(f, self.lazy[k])

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

    def prod(self, l, r):
        ''' get the value you define in "op" of [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
        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 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 True:
            while l % 2 == 0:
                l >>= 1
            if not g(self.op(sm, self.data[l])):
                while l < self.size:
                    self.push(l)
                    l <<= 1
                    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:
                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 True:
            r -= 1
            while r > 1 and r % 2:
                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:
                return 0

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

def mapping(p, x):
    return p + x

def composition(p, q):
    return p+q

n,q = map(int,input().split())
A = list(map(int,input().split()))
B = [0]*n
e = 0
id = 0
lazyseg = LazySegTree(n, op, e, mapping, composition, id)


for _ in range(q):
    c,x,y = input().split()
    x,y = int(x)-1,int(y)
    

    if c == "A":
        num = lazyseg.point_get(x)
        B[x] += num*A[x]
        lazyseg.range_apply(x,x+1,-num)
        A[x] += y
    else:
        lazyseg.range_apply(x,y,1)

for i in range(n):
    num = lazyseg.point_get(i)
    B[i] += num*A[i]

print(*B)
0