Neterukun's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Neterukun1993/Library

:heavy_check_mark: 矩形加算・一点取得
(DataStructure/BinaryIndexedTree/RangeAddPointGet2D.py)

概要

二次元配列に対して矩形範囲加算、一点取得を計算量 $O(\log h\log w)$ で行えるデータ構造。

使い方

BinaryIndexedTree(h: int, w: int)
大きさ $h × w$ の二次元 Binary Indexed Tree を構築する。初期値はすべて $0$ である。計算量 $O(hw)$

Verified with

Code

class BinaryIndexedTree:
    def __init__(self, h, w):
        self.h = h
        self.w = w
        self.bit = [[0] * (self.w + 1) for _ in range(self.h + 1)]

    def get(self, i, j):
        j0 = j
        i = i + 1
        s = 0
        while i <= self.h:
            j = j0 + 1
            while j <= self.w:
                s += self.bit[i][j]
                j += j & -j
            i += i & -i
        return s

    def _add(self, i, j, val):
        j0 = j
        while i > 0:
            j = j0
            while j > 0:
                self.bit[i][j] += val
                j -= j & -j
            i -= i & -i

    def add(self, hl, hr, wl, wr, val):
        """add value in range [l, r)"""
        self._add(hl, wl, val)
        self._add(hr, wl, -val)
        self._add(hl, wr, -val)
        self._add(hr, wr, val)
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/python.py", line 96, in bundle
    raise NotImplementedError
NotImplementedError
Back to top page