結果

問題 No.2306 [Cherry 5th Tune C] ウソツキタマシイ
ユーザー i_takui_taku
提出日時 2023-05-19 21:55:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 1,809 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 92,288 KB
最終ジャッジ日時 2024-05-10 05:28:22
合計ジャッジ時間 8,057 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,096 KB
testcase_01 AC 39 ms
52,224 KB
testcase_02 AC 58 ms
66,176 KB
testcase_03 AC 47 ms
60,928 KB
testcase_04 AC 65 ms
68,864 KB
testcase_05 AC 70 ms
71,040 KB
testcase_06 AC 67 ms
70,784 KB
testcase_07 AC 171 ms
88,448 KB
testcase_08 AC 100 ms
82,560 KB
testcase_09 AC 132 ms
88,704 KB
testcase_10 AC 130 ms
77,184 KB
testcase_11 AC 146 ms
83,676 KB
testcase_12 AC 165 ms
92,160 KB
testcase_13 AC 140 ms
90,368 KB
testcase_14 AC 130 ms
84,352 KB
testcase_15 AC 185 ms
83,824 KB
testcase_16 AC 186 ms
78,720 KB
testcase_17 AC 220 ms
92,288 KB
testcase_18 AC 225 ms
92,032 KB
testcase_19 AC 216 ms
91,648 KB
testcase_20 AC 225 ms
92,160 KB
testcase_21 AC 211 ms
92,160 KB
testcase_22 AC 217 ms
91,904 KB
testcase_23 AC 217 ms
91,648 KB
testcase_24 AC 226 ms
91,648 KB
testcase_25 AC 220 ms
91,648 KB
testcase_26 AC 214 ms
91,904 KB
testcase_27 AC 191 ms
91,392 KB
testcase_28 AC 214 ms
91,520 KB
testcase_29 AC 153 ms
76,672 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