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

Solution in Kotlin (1.3.71) (judge verdict: Wrong Answer)

@OptIn(ExperimentalStdlibApi::class)
fun main(args : Array<String>) {
    val (n, q) = readLine()!!.split(" ").map { it.toInt() }
    val aa = readLine()!!.split(" ").map { it.toInt() }
    val bits = BIT<Int, ULong>(n)

    aa.forEachIndexed { idx, a ->
        bits.add(idx, a)
    }

    repeat(q) {
        val (t, u, v) = readLine()!!.split(" ").map { it.toInt() }
        if (t == 0) {
            bits.add(u, v)
        } else {
            println(bits.sum(u, v))
        }
    }
}

// NOTE: T1 -> original parameter type, T2 -> summarized type
@Suppress("UNCHECKED_CAST")
private data class BIT<T1, T2>(val size: Int) {
    private val data = (0 until this.size).map { 0 as T2 }.toMutableList()

    fun add(idx: Int, value: T1) {
        var i = idx+1
        while (i <= this.size) {
            data[i-1] = this.plus(data[i-1], value as T2)
            i += i and -i
        }
    }

    // NOTE: data[rIdx] is not included
    fun sum(rIdx: Int): T2 {
        var i = rIdx
        var sum = 0 as T2

        while (i > 0) {
            sum = this.plus(sum, data[i-1])
            i -= i and -i
        }

        return sum
    }

    // NOTE: data[rIdx] is not included
    fun sum(lIdx: Int, rIdx: Int): T2 {
        return this.minus(sum(rIdx), sum(lIdx))
    }

    private fun plus(x1: T2, x2: T2): T2 {
        val result = when {
            x1 is Byte && x2 is Byte -> x1.plus(x2)
            x1 is Double && x2 is Double -> x1.plus(x2)
            x1 is Float && x2 is Float -> x1.plus(x2)
            x1 is Int && x2 is Int -> x1.plus(x2)
            x1 is Long && x2 is Long -> x1.plus(x2)
            x1 is Short && x2 is Short -> x1.plus(x2)
            x1 is UByte && x2 is UByte -> x1.plus(x2)
            x1 is UInt && x2 is UInt -> x1.plus(x2)
            x1 is ULong && x2 is ULong -> x1.plus(x2)
            x1 is UShort && x2 is UShort -> x1.plus(x2)
            else -> throw Exception("Invalid type")
        }
        return result as T2
    }

    private fun minus(x1: T2, x2: T2): T2 {
        val result = when {
            x1 is Byte && x2 is Byte -> x1.minus(x2)
            x1 is Double && x2 is Double -> x1.minus(x2)
            x1 is Float && x2 is Float -> x1.minus(x2)
            x1 is Int && x2 is Int -> x1.minus(x2)
            x1 is Long && x2 is Long -> x1.minus(x2)
            x1 is Short && x2 is Short -> x1.minus(x2)
            x1 is UByte && x2 is UByte -> x1.minus(x2)
            x1 is UInt && x2 is UInt -> x1.minus(x2)
            x1 is ULong && x2 is ULong -> x1.minus(x2)
            x1 is UShort && x2 is UShort -> x1.minus(x2)
            else -> throw Exception("Invalid type")
        }
        return result as T2
    }
}