def resolve(): N, M = LI() LR = [LI_() for _ in range(M)] # 余事象を考える ans = M * (M - 1) // 2 # 1. 端点が一致する場合 cnt = collections.Counter() for i, j in LR: cnt[i] += 1 cnt[j] += 1 num1 = sum(i * (i - 1) // 2 for i in cnt.values()) # 2. 重複が無い場合 # 各Lに対して、R<LとなるRをカウントする cnt = [0] * N for _, i in LR: cnt[i] += 1 acm = list(itertools.accumulate(cnt, initial=0)) # print(acm) num2 = 0 for i, _ in LR: num2 += acm[i] # num2_ = 0 # for i in range(M - 1): # for j in range(i + 1, M): # aL, aR = LR[i] # bL, bR = LR[j] # if aR < bL or bR < aL: # num2_ += 1 # 3. 包含関係にある場合 LR.sort(key=lambda x: x[1]) # Rの小さい順に線分を追加していって、追加した線分よりLが大きい線分を数える # Rを小さい順に追加することで、Lだけを見ればよくなる fwt = FenwickTree(N) num3 = 0 for L, R in LR: if L + 1 <= R - 1: num3 += fwt.sum(L + 1, R - 1) fwt.add(L, 1) # print(num1, num2, num2_, num3) ans = ans - num1 - num2 - num3 print(ans)
def test_add_when_negative_value_is_given( self, fenwicktree: FenwickTree ) -> None: ''' fenwicktree.add(p, x) is expected to add x to the p-th of the number sequence. GIVEN an initialized fenwicktree object WHEN add negative value at arbitrary positions in the sequence THEN fenwicktree.data has a partial sum at any position in the sequence ''' for i in range(5): fenwicktree.add(i, i + 1) assert fenwicktree.data == [1, 3, 3, 10, 5] fenwicktree.add(0, -5) assert fenwicktree.data == [-4, -2, 3, 5, 5]
def test_add_multiple_times(self, fenwicktree: FenwickTree) -> None: ''' fenwicktree.add(p, x) is expected to add x to the p-th of the number sequence. GIVEN an initialized fenwicktree object WHEN add arbitrary value(s) at arbitrary positions in the sequence THEN fenwicktree.data has a partial sum at any position in the sequence ''' expected = [[1, 1, 0, 1, 0], [1, 3, 0, 3, 0], [1, 3, 3, 6, 0], [1, 3, 3, 10, 0], [1, 3, 3, 10, 5] ] for i in range(5): fenwicktree.add(i, i + 1) assert fenwicktree.data == expected[i]
def test_initial_status(self, fenwicktree: FenwickTree) -> None: ''' An initialized fenwicktree object is expected to have all the elements 0. GIVEN an initialized fenwicktree object WHEN before executing fenwicktree.add(p, x) THEN all the elements are 0 ''' assert fenwicktree.data == [0, 0, 0, 0, 0] pair_of_positions = self._generate_pair_of_positions() for left, right in pair_of_positions: assert fenwicktree.sum(left, right) == 0
def main() -> None: n, q = map(int, sys.stdin.readline().split()) fenwick_tree = FenwickTree(n) a = map(int, sys.stdin.readline().split()) for i, ai in enumerate(a): fenwick_tree.add(i, ai) for _ in range(q): t, x, y = map(int, sys.stdin.readline().split()) if t == 0: fenwick_tree.add(x, y) if t == 1: print(fenwick_tree.sum(x, y))
def main(): h, w, m = list(map(int, input().strip().split())) x2y = [w] * h y2x = [h] * w obs_y2x = [[] for _ in range(h)] for i in range(m): xt, yt = list(map(int, input().strip().split())) xt -= 1 yt -= 1 x2y[xt] = min(x2y[xt], yt) y2x[yt] = min(y2x[yt], xt) obs_y2x[xt].append(yt) result = sum(x2y[: y2x[0]]) + sum(y2x[: x2y[0]]) tree = FenwickTree(w) for j in range(x2y[0]): tree.add(j, 1) for i in range(y2x[0]): for j in obs_y2x[i]: tree.add(j, -tree.sum(j, j+1)) result -= tree.sum(0, x2y[i]) print(result)
def test_sum_when_additional_element_is_given( self, fenwicktree: FenwickTree, left: int, right: int, expected: int ) -> None: ''' fenwicktree.sum(left, right) is expected to calculate the sum of the interval [left, right) GIVEN With arbitrary value(s) at arbitrary positions in the sequence added to an initialized fenwicktree object WHEN specify the interval [left, right) after the additional element is given THEN returns the sum of interval [left, right) ''' for i in range(5): fenwicktree.add(i, i + 1) assert fenwicktree.sum(0, 5) == 15 fenwicktree.add(2, 2) assert fenwicktree.data == [1, 3, 5, 12, 5] assert fenwicktree.sum(left, right) == expected
def fenwicktree() -> FenwickTree: return FenwickTree(5)
#!/bin/python3 from atcoder.fenwicktree import FenwickTree n = int(input().strip()) tree = FenwickTree(n) for i in range(n): tree.add(i, 1) # AtCoder Library doesn't have get API. # Insted you can use sum with span 1. j = 3 print(tree.sum(j, j + 1)) # return the sum of a[l, r) print(tree.sum(0, n))