A range function

2.1

10 votes
Advanced Data Structures, Segment Trees, C++, Data Structures
Problem

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:

  • 1ival: Update value of element at i-th index to val i.e. A[i] = val
  • 2LR: Find the value of function Ri=LRj=ijk=iA[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

1T101N1051Q1031A[i],val1061iN1LRN

 

Time Limit: 6
Memory Limit: 256
Source Limit:
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.
Editor Image

?