#K7491. Range Update Operations and Queries

    ID: 34302 Type: Default 1000ms 256MiB

Range Update Operations and Queries

Range Update Operations and Queries

You are given an array \(A\) of size \(n\) and \(m\) update operations. Each operation is described by three integers \(l\), \(r\), and \(x\) and it means that you should add \(x\) to every element in the subarray \(A[l]...A[r]\) (1-indexed). After performing all the update operations, you need to answer \(q\) queries. For each query, output the value at a given index in the updated array.

Input Format:

  • The first line contains two integers \(n\) and \(m\), representing the size of the array and the number of operations respectively.
  • The second line contains \(n\) space-separated integers representing the array \(A\).
  • The next \(m\) lines each contain three integers \(l\), \(r\), and \(x\) describing an operation.
  • The following line contains a single integer \(q\) - the number of queries.
  • The last line contains \(q\) space-separated integers representing the indices to query.

Output Format:

  • Output \(q\) lines, each containing the updated value at the queried index of the array.

Note: Use 1-indexed positions for operations and queries.

One efficient approach to solve this problem is to use a difference array. The idea is to update a difference array \(\text{diff}\), where for every operation \(l, r, x\), you add \(x\) to \(\text{diff}[l-1]\) and subtract \(x\) from \(\text{diff}[r]\) (if \(r < n\)). Then, compute the prefix sums over \(\text{diff}\) to obtain the final updates to the array \(A\).

inputFormat

The input is read from standard input and is formatted as follows:

n m
A[1] A[2] ... A[n]
(l r x) for m lines
q
query1 query2 ... queryq

Here, \(A[i]\) represents the \(i\)-th element of the array, and each operation applies an update on a subarray.

outputFormat

For each query, output the value at that index in the updated array. Each result should be printed on a new line.

## sample
5 3
1 2 3 4 5
1 3 2
2 5 3
1 2 -1
4
1 2 3 5
2

6 8 8

</p>