Problem:


Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

0 p x: a_p \gets a_p + x

1 l r: Print \sum_{i = l}^{r - 1}{a_i}.

Constraints

1 \leq N, Q \leq 500,000

0 \leq a_i, x \leq 10^9

0 \leq p < N

0 \leq l_i < r_i \leq N

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
a_0 a_1 ... a_{N - 1}
\textrm{Query}_0
\textrm{Query}_1
:
\textrm{Query}_{Q - 1}

Output

For each query of the latter type, print the answer.

Sample Input 1

5 5
1 2 3 4 5
1 0 5
1 2 4
0 3 10
1 0 5
1 0 3

Sample Output 1

15
7
25
6



Sample input:
5 5
1 2 3 4 5
1 0 5
1 2 4
0 3 10
1 0 5
1 0 3


Sample output:
15
7
25
6

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

class BIT:
  def __init__(self, n):
    self.bit = [0] * n

  def add(self, i, x):
    i += 1
    while i <= len(self.bit):
      self.bit[i-1] += x
      i += i & -i

  def sum_sub(self, i):
    a = 0
    i += 1
    while i:
      a += self.bit[i-1]
      i -= i & -i
    return a

  def sum(self, i, j):
    a = 0
    if j != 0:
      a += self.sum_sub(j-1)
    if i != 0:
      a -= self.sum_sub(i-1)
    return a

n,q=map(int,input().split())
a=list(map(int,input().split()))
Bit=BIT(n)
for i in range(n):
  Bit.add(i,a[i])
for i in range(q):
  t,a,b=map(int,input().split())
  if t==0:
    Bit.add(a,b)
  else:
    print(Bit.sum(a,b))

Submission status: Accepted

Solution code:
library = """
# distutils: language=c++
# distutils: include_dirs=[/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include, /opt/atcoder-stl]
# cython: boundscheck=False
# cython: wraparound=False
from libcpp cimport bool

cdef extern from "/opt/atcoder-stl/atcoder/fenwicktree.hpp" namespace "atcoder":
    cdef cppclass fenwick_tree[T]:
        fenwick_tree()
        fenwick_tree(int n)
        void add(int p, T x)
        T sum(int l, int r)

ctypedef long ValueType
cdef class BIT:
    cdef fenwick_tree[ValueType] *_thisptr
    def __cinit__(self, int n = 0):
        if n == 0:
            self._thisptr = new fenwick_tree[ValueType]()
        else:
            self._thisptr = new fenwick_tree[ValueType](n)  
    def __dealloc__(self):
        del self._thisptr
    cpdef void add(self, int p, ValueType x):
        self._thisptr.add(p, x)
    cpdef ValueType sum(self, int l, int r):
        return self._thisptr.sum(l, r)
"""
import os,sys
if sys.argv[-1] == 'ONLINE_JUDGE':
    open('atcoder.pyx','w').write(library)
    os.system('cythonize -i -3 -b atcoder.pyx')
 
# ↑ここまでライブラリ↓ここから解法
 
import sys
import atcoder
input = sys.stdin.buffer.readline
n, q = map(int, input().split())
dummy = atcoder.BIT()
bit = atcoder.BIT(n)
*map(bit.add, range(n), map(int, input().split())),
res = []
for i in range(q):
    t, u, v = map(int, input().split())
    if t: res.append(bit.sum(u, v))
    else: bit.add(u, v)
print('\n'.join(map(str, res)))

Submission status: Accepted

Solution code:
# test
class fenwick_tree:
  def __init__(s, n):
    s._n = n
    s.data = [0] * n

  def add(s, p, x):
    p += 1
    while p <= s._n:
      s.data[p - 1] += x
      p += p & -p

  def sumlr(s, l, r):
    return s.sum(r) - s.sum(l)

  def sum(self, r):
    s = 0
    while r > 0:
      s += self.data[r - 1]
      r -= r & -r
    return s


N, Q = list(map(int, input().split()))
a = list(map(int, input().split()))
Query = [list(map(int, input().split())) for _ in range(Q)]
  
bit = fenwick_tree(N)
for i in range(N):
  bit.add(i, a[i])

for q in Query:
  if q[0] == 0:
    _, p, x = q
    bit.add(p, x)
  else:
    _, l, r = q
    print(bit.sumlr(l, r))

Submission status: Accepted

Solution code:
# test
class BIT:
  ini = 0
  def __init__(s, num):
    s.N = 1
    while s.N <= num:
      s.N *= 2
    s.T = [s.ini] * s.N
  def set(s, L):
    for i in range(len(L)):
      s.update(i, L[i])
  def update(s, x, n): # xにnを加える
    k = x + 1
    s.T[k - 1] += n
    k += k & -k
    while k <= s.N:
      s.T[k - 1] += n
      k += k & -k
  def getV(s, x): # xまでの和(x含む)
    if x < 0: return 0
    x += 1
    ans = s.T[x - 1]
    x -= x & -x
    while x != 0:
      ans += s.T[x - 1]
      x -= x & -x
    return ans

N, Q = list(map(int, input().split()))
a = list(map(int, input().split()))
Query = [list(map(int, input().split())) for _ in range(Q)]
  
bit = BIT(N)
bit.set(a)

for q in Query:
  if q[0] == 0:
    _, p, x = q
    bit.update(p, x)
  else:
    _, l, r = q
    print(bit.getV(r - 1) - bit.getV(l - 1))

Submission status: Accepted

Solution code:
import typing


class FenwickTree:
    '''Reference: https://en.wikipedia.org/wiki/Fenwick_tree'''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.data = [0] * n

    def add(self, p: int, x: typing.Any) -> None:
        assert 0 <= p < self._n

        p += 1
        while p <= self._n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, left: int, right: int) -> typing.Any:
        assert 0 <= left <= right <= self._n

        return self._sum(right) - self._sum(left)

    def _sum(self, r: int) -> typing.Any:
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r

        return s


# https://atcoder.jp/contests/practice2/tasks/practice2_b
def main() -> None:
    import sys

    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))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
library = """
# distutils: language=c++
# distutils: include_dirs=[/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include, /opt/atcoder-stl]
# cython: boundscheck=False
# cython: wraparound=False
from libcpp cimport bool
cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder":
    cdef cppclass dsu:
        dsu(int n)
        int merge(int a, int b)
        bool same(int a, int b)
        int leader(int a)
        int size(int a)
cdef extern from "/opt/atcoder-stl/atcoder/fenwicktree.hpp" namespace "atcoder":
    cdef cppclass fenwick_tree[T]:
        fenwick_tree() except +
        fenwick_tree(int n) except +
        void add(int p, T x)
        T sum(int l, int r)
cdef class DSU:
    cdef dsu *_thisptr
    def __cinit__(self, int n):
        self._thisptr = new dsu(n)
    def __dealloc__(self):
        del self._thisptr
    cpdef int merge(self, int a, int b):
        return self._thisptr.merge(a, b)
    cpdef bool same(self, int a, int b):
        return self._thisptr.same(a, b)
    cpdef int leader(self, int a):
        return self._thisptr.leader(a)
    cpdef int size(self, int a):
        return self._thisptr.size(a)
cdef class BIT:
    cdef fenwick_tree[long] *_thisptr
    def __cinit__(self, int n = 0):
        if n == 0:
            self._thisptr = new fenwick_tree[long]()
        else:
            self._thisptr = new fenwick_tree[long](n)  
    def __dealloc__(self):
        del self._thisptr
    cpdef void add(self, int p, long x):
        self._thisptr.add(p, x)
    cpdef long sum(self, int l, int r):
        return self._thisptr.sum(l, r)
"""
import os,sys
if sys.argv[-1] == 'ONLINE_JUDGE':
    open('atcoder.pyx','w').write(library)
    os.system('cythonize -i -3 -b atcoder.pyx')
 
# ↑ここまでライブラリ↓ここから解法
 
import sys
import atcoder
input = sys.stdin.buffer.readline
n, q = map(int, input().split())
dummy = atcoder.BIT()
bit = atcoder.BIT(n)
*map(bit.add, range(n), map(int, input().split())),
res = []
for i in range(q):
    t, u, v = map(int, input().split())
    if t: res.append(bit.sum(u, v))
    else: bit.add(u, v)
print('\n'.join(map(str, res)))

Submission status: Accepted

Solution code:
library = """
# distutils: language=c++
# distutils: include_dirs=[/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include, /opt/atcoder-stl]
# cython: boundscheck=False
# cython: wraparound=False
from libcpp cimport bool
cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder":
    cdef cppclass dsu:
        dsu(int n)
        int merge(int a, int b)
        bool same(int a, int b)
        int leader(int a)
        int size(int a)
cdef extern from "/opt/atcoder-stl/atcoder/fenwicktree.hpp" namespace "atcoder":
    cdef cppclass fenwick_tree[T]:
        fenwick_tree() except +
        fenwick_tree(int n) except +
        void add(int p, T x)
        T sum(int l, int r)
cdef class DSU:
    cdef dsu *_thisptr
    def __cinit__(self, int n):
        self._thisptr = new dsu(n)
    def __dealloc__(self):
        del self._thisptr
    cpdef int merge(self, int a, int b):
        return self._thisptr.merge(a, b)
    cpdef bool same(self, int a, int b):
        return self._thisptr.same(a, b)
    cpdef int leader(self, int a):
        return self._thisptr.leader(a)
    cpdef int size(self, int a):
        return self._thisptr.size(a)
cdef class BIT:
    cdef fenwick_tree[long] _thisptr
    def __cinit__(self, int n = 0):
        if n == 0:
            self._thisptr = fenwick_tree[long]()
        else:
            self._thisptr = fenwick_tree[long](n)  
    cpdef void add(self, int p, long x):
        self._thisptr.add(p, x)
    cpdef long sum(self, int l, int r):
        return self._thisptr.sum(l, r)
"""
import os,sys
if sys.argv[-1] == 'ONLINE_JUDGE':
    open('atcoder.pyx','w').write(library)
    os.system('cythonize -i -3 -b atcoder.pyx')
 
# ↑ここまでライブラリ↓ここから解法
 
import sys
import atcoder
input = sys.stdin.buffer.readline
n, q = map(int, input().split())
dummy = atcoder.BIT()
bit = atcoder.BIT(n)
*map(bit.add, range(n), map(int, input().split())),
res = []
for i in range(q):
    t, u, v = map(int, input().split())
    if t: res.append(bit.sum(u, v))
    else: bit.add(u, v)
print('\n'.join(map(str, res)))

Submission status: Accepted

Solution code:
library = """
# distutils: language=c++
# distutils: include_dirs=[/home/contestant/.local/lib/python3.8/site-packages/numpy/core/include, /opt/atcoder-stl]
# cython: boundscheck=False
# cython: wraparound=False
from libcpp cimport bool
cdef extern from "/opt/atcoder-stl/atcoder/dsu.hpp" namespace "atcoder":
    cdef cppclass dsu:
        dsu(int n)
        int merge(int a, int b)
        bool same(int a, int b)
        int leader(int a)
        int size(int a)
cdef extern from "/opt/atcoder-stl/atcoder/fenwicktree.hpp" namespace "atcoder":
    cdef cppclass fenwick_tree[T]:
        fenwick_tree()
        fenwick_tree(int n)
        void add(int p, T x)
        T sum(int l, int r)
cdef class DSU:
    cdef dsu *_thisptr
    def __cinit__(self, int n):
        self._thisptr = new dsu(n)
    def __dealloc__(self):
        del self._thisptr
    cpdef int merge(self, int a, int b):
        return self._thisptr.merge(a, b)
    cpdef bool same(self, int a, int b):
        return self._thisptr.same(a, b)
    cpdef int leader(self, int a):
        return self._thisptr.leader(a)
    cpdef int size(self, int a):
        return self._thisptr.size(a)
cdef class BIT:
    cdef fenwick_tree[long] *_thisptr
    def __cinit__(self, int n = 0):
        if n == 0:
            self._thisptr = new fenwick_tree[long]()
        else:
            self._thisptr = new fenwick_tree[long](n)  
    def __dealloc__(self):
        del self._thisptr
    cpdef void add(self, int p, long x):
        self._thisptr.add(p, x)
    cpdef long sum(self, int l, int r):
        return self._thisptr.sum(l, r)
"""
import os,sys
if sys.argv[-1] == 'ONLINE_JUDGE':
    open('atcoder.pyx','w').write(library)
    os.system('cythonize -i -3 -b atcoder.pyx')
 
# ↑ここまでライブラリ↓ここから解法
 
import sys
import atcoder
input = sys.stdin.buffer.readline
n, q = map(int, input().split())
dummy = atcoder.BIT()
bit = atcoder.BIT(n)
*map(bit.add, range(n), map(int, input().split())),
res = []
for i in range(q):
    t, u, v = map(int, input().split())
    if t: res.append(bit.sum(u, v))
    else: bit.add(u, v)
print('\n'.join(map(str, res)))

Submission status: Accepted

Solution code:
def f(A,p,x):
  for i in range(len(A)):
    A[i][p//(2**i)] += x

def g(A,l,r):
  n = 0
  a = [int(i) for i in list(bin(r)[2:])]
  if l > 0:
    b = [int(i) for i in list(bin(l)[2:])]
  t = 0
  for i in range(len(a)):
    n += A[len(a)-i-1][t]*a[i]
    t += a[i]
    t *= 2
  t = 0
  if l == 0:
    print(n)
  else:
    for i in range(len(b)):
      n -= A[len(b)-i-1][t]*b[i]
      t += b[i]
      t *= 2
    print(n)
N,Q = [int(i) for i in input().split()]
L = [[int(i) for i in input().split()]]
L[0].append(0)
while N > 1:
  N = -(-N//2)
  A = [0]*(N+1)
  for i in range(N):
    A[i] = L[-1][2*i] + L[-1][2*i + 1]
  L.append(A)


for _ in range(Q):
  a,b,c = [int(i) for i in input().split()]
  if a == 0:
    f(L,b,c)
  if a == 1:
    g(L,b,c)

Submission status: Accepted

Solution code:
class FenwickTree():
    def __init__(self, n):
        self.n = n
        self.data = [0] * n

    def build(self, arr):
        #assert len(arr) <= n
        for i, a in enumerate(arr):
            self.data[i] = a
        for i in range(1, self.n + 1):
            if i + (i & -i) <= self.n:
                self.data[i + (i & -i) - 1] += self.data[i - 1]

    def add(self, p, x):
        #assert 0 <= p < self.n
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, r):
        s = 0
        while r:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def range_sum(self, l, r):
        #assert 0 <= l <= r <= self.n
        return self.sum(r) - self.sum(l)

import sys
input = sys.stdin.buffer.readline

N, Q = map(int, input().split())
A = map(int, input().split())

ft = FenwickTree(N)
ft.build(A)
res = []

for _ in range(Q):
    q, x, y = map(int, input().split())
    if q:
        res.append(str(ft.range_sum(x, y)))
    else:
        ft.add(x, y)

print('\n'.join(res))

Submission status: Accepted

Solution code:
class FenwickTree():
    def __init__(self, n):
        self.n = n
        self.data = [0] * n

    def build(self, arr):
        #assert len(arr) <= n
        for i, a in enumerate(arr):
            self.data[i] = a
        for i in range(1, self.n + 1):
            if i + (i & -i) <= self.n:
                self.data[i + (i & -i) - 1] += self.data[i - 1]

    def add(self, p, x):
        #assert 0 <= p < self.n
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, r):
        s = 0
        while r:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def range_sum(self, l, r):
        #assert 0 <= l <= r <= self.n
        return self.sum(r) - self.sum(l)

import sys
input = sys.stdin.buffer.readline

N, Q = map(int, input().split())
A = tuple(map(int, input().split()))

ft = FenwickTree(N)
ft.build(A)
res = []

for _ in range(Q):
    q, x, y = map(int, input().split())
    if q:
        res.append(str(ft.range_sum(x, y)))
    else:
        ft.add(x, y)

print('\n'.join(res))

Submission status: Accepted

Solution code:
class FenwickTree():
    def __init__(self, n):
        self.n = n
        self.data = [0] * n

    def add(self, p, x):
        #assert 0 <= p < self.n
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, r):
        s = 0
        while r:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def range_sum(self, l, r):
        #assert 0 <= l <= r <= self.n
        return self.sum(r) - self.sum(l)

import sys
input = sys.stdin.buffer.readline

N, Q = map(int, input().split())
A = tuple(map(int, input().split()))

ft = FenwickTree(N)
res = []

for i, a in enumerate(A):
    ft.add(i, a)

for _ in range(Q):
    q, x, y = map(int, input().split())
    if q:
        res.append(str(ft.range_sum(x, y)))

    else:
        ft.add(x, y)

print('\n'.join(res))

Submission status: Accepted

Solution code:
class FenwickTree():
    def __init__(self, n):
        self.n = n
        self.data = [0] * n

    def add(self, p, x):
        #assert 0 <= p < self.n
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, r):
        s = 0
        while r:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def range_sum(self, l, r):
        #assert 0 <= l <= r <= self.n
        return self.sum(r) - self.sum(l)

import sys
input = sys.stdin.buffer.readline

N, Q = map(int, input().split())
A = tuple(map(int, input().split()))

ft = FenwickTree(N)
res = []

for i in range(N):
    ft.add(i, A[i])

for _ in range(Q):
    q, x, y = map(int, input().split())
    if q:
        res.append(str(ft.range_sum(x, y)))

    else:
        ft.add(x, y)

print('\n'.join(res))

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

N, Q = map(int,input().split())
A = list(map(int, input().split()))


# 1-index
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0]*(n+1)
        self.el = [0]*(n+1)
        self.depth = n.bit_length()
        
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s
    
    def add(self, i, x):
        """assert i > 0"""
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i
            
    def get(self, i, j=None):
        if j is None:
            return self.el[i]
        return self.sum(j) - self.sum(i)
    
    def lower_bound(self, x):
        """ 累積和がx以上になる最小のindexと、その直前までの累積和 """
        sum_ = 0
        pos = 0
        for i in range(self.depth, -1, -1):
            k = pos + (1 << i)
            if k <= self.n and sum_ + self.data[k] < x:
                sum_ += self.data[k]
                pos += 1 << i
        return pos + 1, sum_

bit = BIT(N)
for idx, a in enumerate(A):
    bit.add(idx+1, a)

for i in range(Q):
    qry = list(input().split())
    if qry[0] == "0":
        bit.add(int(qry[1])+1, int(qry[2]))
    else:
        print(bit.get(int(qry[1]), int(qry[2])))

Submission status: Accepted

Solution code:
class BinaryIndexedTree():
    def __init__(self, n):
        self.n = n
        self.BIT = [0] * (self.n + 1)
    def add(self, i, x):
        while i <= self.n:
            self.BIT[i] += x
            i += i & -i
    def query(self, i):
        res = 0
        while i > 0:
            res += self.BIT[i]
            i -= i & -i
        return res
n, q = map(int, input().split())
BIT = BinaryIndexedTree(n)
a = list(map(int, input().split()))
for i in range(n):
    BIT.add(i + 1, a[i])
for i in range(q):
    x, y, z = map(int, input().split())
    if x == 0:
        BIT.add(y + 1, z)
    else:
        print(BIT.query(z) - BIT.query(y))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

class BIT:
    def __init__(self, li):
        self.n, self.data = len(li) + 1, [0] + li
        for i in range(1, self.n):
            if i + (i & -i) < self.n: self.data[i + (i & -i)] += self.data[i]

    def add(self, i, a):
        i += 1
        while i < self.n:
            self.data[i] += a
            i += i & -i

    # sum of [0, i)
    def acc(self, i):
        res = 0
        while i > 0:
            res += self.data[i]
            i -= i & -i
        return res

    # sum of [l, r)
    def get(self, l, r = None):
        if r is None: r = l+1
        return self.acc(r) - self.acc(l)


def main():
    N, Q = LI()
    A = LI()
    query = LIR(Q)
    bit = BIT(A)

    for a, b, c in query:
        if a == 0:
            bit.add(b, c)
        else:
            print(bit.get(b, c))




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

class BIT:
    def __init__(self, li):
        self.n, self.data = len(li) + 1, [0] + li
        for i in range(1, self.n):
            if i + (i & -i) < self.n: self.data[i + (i & -i)] += self.data[i]

    def add(self, i, a):
        i += 1
        while i < self.n:
            self.data[i] += a
            i += i & -i

    # sum of [0, i)
    def acc(self, i):
        res = 0
        while i > 0:
            res += self.data[i]
            i -= i & -i
        return res

    # sum of [l, r)
    def get(self, l, r = None):
        if r is None: r = l+1
        res = 0
        # return self.acc(r) - self.acc(l)

        while l < r:
            res += self.data[r]
            r -= r & -r
        while r < l:
            res -= self.data[l]
            l -= l & -l
        return res


def main():
    N, Q = LI()
    A = LI()
    query = LIR(Q)
    bit = BIT(A)
    # print([bit.get(i) for i in range(N)])

    for a, b, c in query:
        if a == 0:
            bit.add(b, c)
        else:
            print(bit.get(b, c))
        # print([bit.get(i) for i in range(N)])




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

class BIT:
    def __init__(self, li):
        self.n, self.data = len(li) + 1, [0] + li
        for i in range(1, self.n):
            if i + (i & -i) < self.n: self.data[i + (i & -i)] += self.data[i]

    def add(self, i, a):
        i += 1
        while i < self.n:
            self.data[i] += a
            i += i & -i

    # sum of [0, i)
    def acc(self, i):
        res = 0
        while i > 0:
            res += self.data[i]
            i -= i & -i
        return res

    # sum of [l, r)
    def get(self, l, r = None):
        if r is None: r = l+1
        return self.acc(r) - self.acc(l)

def main():
    N, Q = LI()
    A = LI()
    query = LIR(Q)
    bit = BIT(A)
    # print([bit.get(i) for i in range(N)])

    for a, b, c in query:
        if a == 0:
            bit.add(b, c)
        else:
            print(bit.get(b, c))
        # print([bit.get(i) for i in range(N)])




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#A1 ~ Aiまでの和 O(logN)
def BIT_query(idx):
    res_sum = 0
    while idx > 0:
        res_sum += BIT[idx]
        idx -= idx&(-idx)
    return res_sum

#Ai += x O(logN)
def BIT_update(idx,x):
    while idx <= n:
        BIT[idx] += x
        idx += (idx&(-idx))
    return

n,q=map(int,input().split())
A=list(map(int,input().split()))
#A1 ... AnのBIT(1-indexed)
BIT = [0]*(n+1)
for i,e in enumerate(A):
   BIT_update(i+1,e)
for i in range(q):
  B=list(map(int,input().split()))
  if B[0]==0:
    BIT_update(B[1]+1,B[2])
  else:
    print(BIT_query(B[2])-BIT_query(B[1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


class FenwickTree:
    def __init__(self, n):
        self.__data = [0] * n
        self.__n = n

    def add(self, i, x):
        i += 1
        while i <= self.__n:
            self.__data[i-1] += x
            i += i & -i

    def sum(self, l, r):
        return self.__sum(r) - self.__sum(l)

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.__data[r-1]
            r -= r & -r

        return s


@mt
def slv(N, A, Q):
    ft = FenwickTree(N)
    for i, a in enumerate(A):
        ft.add(i, a)

    ans = []
    for q in Q:
        if q[0] == 0:
            _, p, x = q
            ft.add(p, x)
        else:
            _, l, r = q
            ans.append(ft.sum(l, r))

    return ans


def main():
    N, Q = read_int_n()
    A = read_int_n()
    Q = [read_int_n() for _ in range(Q)]
    print(*slv(N, A, Q), sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


class FenwicTree:
    def __init__(self, n):
        self.__data = [0] * n
        self.__n = n

    def add(self, i, x):
        i += 1
        while i <= self.__n:
            self.__data[i-1] += x
            i += i & -i

    def sum(self, l, r):
        return self.__sum(r) - self.__sum(l)

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.__data[r-1]
            r -= r & -r

        return s


@mt
def slv(N, A, Q):
    ft = FenwicTree(N)
    for i, a in enumerate(A):
        ft.add(i, a)

    ans = []
    for q in Q:
        if q[0] == 0:
            _, p, x = q
            ft.add(p, x)
        else:
            _, l, r = q
            ans.append(ft.sum(l, r))

    return ans


def main():
    N, Q = read_int_n()
    A = read_int_n()
    Q = [read_int_n() for _ in range(Q)]
    print(*slv(N, A, Q), sep='\n')



if __name__ == '__main__':
    main()