結果

問題 No.1000 Point Add and Array Add
ユーザー yuly3yuly3
提出日時 2020-07-15 22:59:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 783 ms / 2,000 ms
コード長 2,192 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 178,936 KB
最終ジャッジ日時 2024-05-02 04:48:49
合計ジャッジ時間 8,648 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,480 KB
testcase_01 AC 44 ms
52,864 KB
testcase_02 AC 44 ms
52,992 KB
testcase_03 AC 40 ms
52,480 KB
testcase_04 AC 43 ms
52,224 KB
testcase_05 AC 44 ms
52,608 KB
testcase_06 AC 44 ms
52,608 KB
testcase_07 AC 43 ms
52,992 KB
testcase_08 AC 43 ms
52,352 KB
testcase_09 AC 45 ms
52,608 KB
testcase_10 AC 45 ms
52,224 KB
testcase_11 AC 45 ms
52,864 KB
testcase_12 AC 104 ms
76,672 KB
testcase_13 AC 93 ms
77,184 KB
testcase_14 AC 103 ms
78,080 KB
testcase_15 AC 106 ms
77,568 KB
testcase_16 AC 604 ms
145,964 KB
testcase_17 AC 488 ms
129,700 KB
testcase_18 AC 749 ms
178,308 KB
testcase_19 AC 736 ms
178,936 KB
testcase_20 AC 715 ms
175,868 KB
testcase_21 AC 620 ms
178,724 KB
testcase_22 AC 783 ms
178,428 KB
testcase_23 AC 686 ms
178,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from operator import add

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


class DualSegmentTree:
    def __init__(self, size: int, segfunc, lazy_ide_ele=0):
        self.lazy_ide_ele = lazy_ide_ele
        self.segfunc = segfunc
        self.N0 = 1 << (size - 1).bit_length()
        self.lazy = [self.lazy_ide_ele] * (2 * self.N0)
    
    def gindex(self, left, right):
        L = left + self.N0
        R = right + self.N0
        lm = (L // (L & -L)) >> 1
        rm = (R // (R & -R)) >> 1
        while L < R:
            if R <= rm:
                yield R
            if L <= lm:
                yield L
            L >>= 1
            R >>= 1
        while L:
            yield L
            L >>= 1
    
    def propagates(self, *ids):
        for i in reversed(ids):
            idx = i - 1
            v = self.lazy[idx]
            if v == self.lazy_ide_ele:
                continue
            self.lazy[2 * idx + 1] = self.segfunc(self.lazy[2 * idx + 1], v)
            self.lazy[2 * idx + 2] = self.segfunc(self.lazy[2 * idx + 2], v)
            self.lazy[idx] = self.lazy_ide_ele
    
    def update(self, left: int, right: int, x):
        L = self.N0 + left
        R = self.N0 + right
        
        while L < R:
            if R & 1:
                R -= 1
                self.lazy[R - 1] = self.segfunc(self.lazy[R - 1], x)
            if L & 1:
                self.lazy[L - 1] = self.segfunc(self.lazy[L - 1], x)
                L += 1
            L >>= 1
            R >>= 1
    
    def query(self, k: int):
        self.propagates(*self.gindex(k, k + 1))
        return self.lazy[k + self.N0 - 1]


def solve():
    N, Q = map(int, rl().split())
    A = list(map(int, rl().split()))
    query = [['A', idx + 1, ai] for idx, ai in enumerate(A)]
    for _ in range(Q):
        c, x, y = rl().split()
        query.append([c, int(x), int(y)])
    
    dual_seg_tree = DualSegmentTree(N, add)
    ans = [0] * N
    for c, x, y in reversed(query):
        x -= 1
        if c == 'B':
            dual_seg_tree.update(x, y, 1)
        else:
            ans[x] += y * dual_seg_tree.query(x)
    print(*ans)


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