A range function
Advanced data structures
Segment trees
C++
Data structures
65.0% success
1,519 attempts
6s time
256MB memory
1024KB max code
You are given an array $$A$$ of $$N$$ integer elements.
You are also given $$Q$$ queries where each query is one of the following types:
- $$1 i val$$: Update value of element at i-th index to val i.e. A[i] = val
- $$2 L R$$: Find the value of function \(\sum_\limits{i = L}^{R}\sum_\limits{j = i}^{R}\sum_\limits{k = i}^{j} A[k]\) .
Note: Assume 1-based indexing.
Input format
- The first line contains a single integer $$T$$ that denotes the number of test cases.
- For each test case:
- The first line contains an integer $$N$$.
- The second line contains $$N$$ space-separated integers denoting array $$A$$.
- The third line contains an integer $$Q$$.
- Next $$Q$$ lines contain three space-separated integers denoting the queries.
Output format
For each test case, print the value of the function for queries of type 2 in the new line.
Constraints
\(1 \le T \le 10\)
\(1 \le N \le 10^5\)
\(1 \le Q \le 10^3\)
\(1 \le A[i], val \le 10^6\)
\(1 \le i \le N\)
\(1 \le L \le R \le N\)
Examples
Input
1 6 2 1 3 4 6 1 3 2 1 2 1 4 1 2 4 4
Output
6 1
Explanation
- First query is of Type 2, with L = 1, R = 2.
- Value of function is equal to A[1] + A[1] + A[2] + A[2] = 2 + 2 + 1 + 1 = 6.
- Thus, print 6
- Second query is of Type 1, Update A[4] = 1. Now array A becomes [2, 1, 3, 1, 6, 1].
- Third query of of Type 2 with L = 4, R = 4.
- Value of function is equal to A[4] = 1.
- Thus, print 1.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Loading Editor...