Submission #70991335


Source Code Expand

# フェニック木 (binary indexed tree: BIT) クラス
class BIT:
    def __init__(self, size):
        self.__size = size
        self.__bit = [0] * (size + 1)

    # O(log N): インデックス idx の値に val を加算するメソッド
    def add(self, idx, val):
        while idx <= self.__size:
            self.__bit[idx] += val
            idx += idx & -idx

    # O(log N): 区間 1 ~ idx の和を求める計算用のメソッド
    def __sum(self, idx):
        result = 0
        while idx > 0:
            result += self.__bit[idx]
            idx -= idx & -idx
        return result

    # O(log N): 区間 1 ~ idx1 または 区間 idx1 ~ idx2 の和を求めるメソッド
    def get_sum(self, idx1, idx2 = None):
        if idx2 is None:
            return self.__sum(idx1)
        return self.__sum(idx2) - self.__sum(idx1 - 1)

n, q = map(int, input().split())
a = list(map(int, input().split()))
query = [list(map(int, input().split())) for _ in range(q)]

SIZE = 5 * (10 ** 5) + 3
bit = BIT(SIZE)
total_bit = BIT(SIZE)
for i in range(n):
    bit.add(a[i] + 1, 1)
    total_bit.add(a[i] + 1, a[i])

for type, xl, yr in query:
    if type == 1:
        x, y = xl - 1, yr
        bit.add(a[x] + 1, -1)
        bit.add(y + 1, 1)
        total_bit.add(a[x] + 1, -a[x])
        total_bit.add(y + 1, y)
        a[x] = y
    else:
        l, r = xl, yr
        if l >= r:
            print(n * l)
        else:
            ans = r * bit.get_sum(r + 1, SIZE)
            ans += total_bit.get_sum(l + 2, r)
            ans += l * bit.get_sum(l + 1)
            print(ans)

Submission Info

Submission Time
Task E - Clamp
User shunta
Language Python (PyPy 3.11-v7.3.20)
Score 450
Code Size 1649 Byte
Status AC
Exec Time 660 ms
Memory 184416 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 21
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 02_handmade_00.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 54 ms 87868 KiB
00_sample_01.txt AC 55 ms 88056 KiB
01_random_00.txt AC 349 ms 161356 KiB
01_random_01.txt AC 500 ms 183968 KiB
01_random_02.txt AC 427 ms 154200 KiB
01_random_03.txt AC 285 ms 128568 KiB
01_random_04.txt AC 581 ms 169588 KiB
01_random_05.txt AC 548 ms 184296 KiB
01_random_06.txt AC 651 ms 184184 KiB
01_random_07.txt AC 626 ms 184300 KiB
01_random_08.txt AC 601 ms 184356 KiB
01_random_09.txt AC 653 ms 184204 KiB
01_random_10.txt AC 609 ms 184284 KiB
01_random_11.txt AC 614 ms 184024 KiB
01_random_12.txt AC 605 ms 184400 KiB
01_random_13.txt AC 545 ms 184408 KiB
01_random_14.txt AC 660 ms 184248 KiB
01_random_15.txt AC 590 ms 184284 KiB
01_random_16.txt AC 55 ms 87804 KiB
01_random_17.txt AC 431 ms 138820 KiB
02_handmade_00.txt AC 508 ms 184416 KiB