提出 #26325029


ソースコード 拡げる

import sys
input = sys.stdin.readline
import logging

logger = logging.getLogger(__name__)
import numpy as np
from numba import njit, jitclass, i8


@jitclass([("n", i8), ("tree", i8[:])])
class FenwickTree:
    def __init__(self, n: i8) -> None:
        """Fenwick Treeを初期化します。

        Parameters
        ----------
        n : i8
            要素数
        """
        self.n = n
        self.tree = np.zeros(n + 1, dtype=np.int64)
    
    def _rsum(self, r):
        s = 0
        while r > 0:
            s += self.tree[r]
            r -= r & -r
        return s

    def sum(self, l: i8, r: i8) -> i8:
        """区間[l, r) の総和を返します。

        Parameters
        ----------
        l : i8
            区間の左端
        r : i8
            区間の右端

        Returns
        -------
        i8
            区間[l, r) の総和
        """
        return self._rsum(r) - self._rsum(l)

    def add(self, i: i8, x: i8) -> None:
        """添字iにxを加算します。

        Parameters
        ----------
        i : i8
            添字
        x : i8
            加数
        """
        i += 1
        while i <= self.n:
            self.tree[i] += x
            i += i & -i


def read():
    N, Q = map(int, input().strip().split())
    A = np.fromstring(input().strip(), sep=" ", dtype=np.int64)
    queries = np.empty((Q, 3), dtype=np.int64)
    for i in range(Q):
        queries[i, :] = np.fromstring(input().strip(), sep=" ", dtype=np.int64)
    return N, Q, A, queries


@njit((i8, i8, i8[:], i8[:, :]), cache=True)
def solve(N, Q, A, queries):
    ft = FenwickTree(N)
    for i in range(N):
        ft.add(i, A[i])
    for i in range(Q):
        query = queries[i, :]
        if query[0] == 0:
            p, x = query[1:]
            ft.add(p, x)
        elif query[0] == 1:
            l, r = query[1:]
            print(ft.sum(l, r))


if __name__ == '__main__':
    logging.basicConfig(level=logging.ERROR)
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

提出情報

提出日時
問題 B - Fenwick Tree
ユーザ kyoroid
言語 Python (3.8.2)
得点 100
コード長 2191 Byte
結果 AC
実行時間 1396 ms
メモリ 125944 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 100 / 100
結果
AC × 1
AC × 21
セット名 テストケース
Sample example_00
All example_00, max_random_00, max_random_01, max_random_02, max_random_03, max_random_04, random_00, random_01, random_02, random_03, random_04, small_00, small_01, small_02, small_03, small_04, small_05, small_06, small_07, small_08, small_09
ケース名 結果 実行時間 メモリ
example_00 AC 462 ms 105204 KiB
max_random_00 AC 1337 ms 125280 KiB
max_random_01 AC 1366 ms 125944 KiB
max_random_02 AC 1364 ms 125912 KiB
max_random_03 AC 1381 ms 125316 KiB
max_random_04 AC 1396 ms 125248 KiB
random_00 AC 1207 ms 121016 KiB
random_01 AC 1249 ms 122832 KiB
random_02 AC 1128 ms 116404 KiB
random_03 AC 563 ms 118096 KiB
random_04 AC 663 ms 113008 KiB
small_00 AC 459 ms 105732 KiB
small_01 AC 456 ms 106384 KiB
small_02 AC 464 ms 105808 KiB
small_03 AC 466 ms 105844 KiB
small_04 AC 467 ms 105736 KiB
small_05 AC 465 ms 105768 KiB
small_06 AC 457 ms 106372 KiB
small_07 AC 466 ms 105804 KiB
small_08 AC 464 ms 105192 KiB
small_09 AC 459 ms 105192 KiB