結果

問題 No.2306 [Cherry 5th Tune C] ウソツキタマシイ
ユーザー i_takui_taku
提出日時 2023-05-19 21:55:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,809 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 93,508 KB
最終ジャッジ日時 2023-08-22 23:53:31
合計ジャッジ時間 9,257 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,840 KB
testcase_01 AC 69 ms
71,840 KB
testcase_02 AC 88 ms
76,548 KB
testcase_03 AC 78 ms
76,332 KB
testcase_04 AC 96 ms
76,648 KB
testcase_05 AC 95 ms
76,524 KB
testcase_06 AC 92 ms
76,396 KB
testcase_07 AC 218 ms
89,892 KB
testcase_08 AC 132 ms
83,984 KB
testcase_09 AC 173 ms
89,936 KB
testcase_10 AC 163 ms
78,832 KB
testcase_11 AC 182 ms
85,292 KB
testcase_12 AC 220 ms
93,296 KB
testcase_13 AC 182 ms
91,432 KB
testcase_14 AC 166 ms
86,108 KB
testcase_15 AC 243 ms
85,240 KB
testcase_16 AC 234 ms
80,412 KB
testcase_17 AC 282 ms
93,508 KB
testcase_18 AC 290 ms
93,352 KB
testcase_19 AC 287 ms
93,320 KB
testcase_20 AC 286 ms
93,232 KB
testcase_21 AC 294 ms
93,284 KB
testcase_22 AC 300 ms
93,012 KB
testcase_23 AC 300 ms
93,020 KB
testcase_24 AC 288 ms
93,464 KB
testcase_25 AC 297 ms
93,020 KB
testcase_26 AC 297 ms
93,332 KB
testcase_27 AC 249 ms
92,624 KB
testcase_28 AC 260 ms
92,596 KB
testcase_29 AC 199 ms
79,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline


def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    Q = int(input())
    A_square = [a**2 for a in A]
    st = SegTree(A_square, op, 0)
    for _ in range(Q):
        c, k, d = map(int, input().split())
        c, d = c - 1, d - 1
        cn = int(st.get_value(c)**(1 / 2))
        dn = int(st.get_value(d)**(1 / 2))
        st.update(c, (cn - k)**2)
        st.update(d, (dn + k)**2)
        print(st.query(0, M))
        

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


class SegTree:
    def __init__(self, ls, op, e):
        n = len(ls)
        self.op = op
        self.e = e
        self.size = 1 << (n - 1).bit_length()
        self.tree = [self.e] * (2 * self.size)
        # 葉に対象の列を格納
        for i in range(n):
            self.tree[self.size + i] = ls[i]
        # 葉に近い場所から順に更新
        for i in range(self.size - 1, 0, -1):
            self.tree[i] = self.op(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        # 葉に移動
        k += self.size
        # 関連する場所を更新
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.op(self.tree[k], self.tree[k ^ 1])
            k >>= 1
        
    def query(self, l, r):
        res = self.e
        # 葉に移動
        l += self.size
        r += self.size        
        # 未計算の区間がなくなるまで
        while l < r:
            if l & 1:
                res = self.op(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.op(res, self.tree[r - 1])
            # 親に移動
            l >>= 1
            r >>= 1
        return res

    def get_value(self, k):
        return self.tree[k + self.size]


main()
0