Problem 681
Range Sum Queries with Point Updates
Medium · 20 pts · Array, Fenwick Tree, Segment Tree
Problem
You are given an array of N integers (positions 1 to N) and Q operations. '1 i v' sets A[i] to v. '2 l r' asks for the sum A[l] + ... + A[r] using the current values. Print the answer to every query of type 2. Recomputing each sum takes O(N) per query, which is too slow for 100000 operations; a Fenwick tree (binary indexed tree) or a segment tree supports both operations in O(log N).
Input
The first line contains N and Q. The second line contains the N values. Each of the next Q lines contains one operation. There is at least one query of type 2.
Output
Print the answer to each type 2 query, one per line.
Constraints
1 <= N, Q <= 100000; -10^9 <= A[i], v <= 10^9
Example input
3 3 1 3 5 2 1 3 1 2 2 2 1 3
Example output
9 8
Explanation
With A = 1 3 5: the sum of positions 1 to 3 is 9. After setting A[2] = 2 the array is 1 2 5, so the same query now gives 8.