#K37562. Range Operation Queries
Range Operation Queries
Range Operation Queries
You are given an array of n integers and an array of n operation strings. Each operation is either sum
or product
. Then, you are given q queries. For each query, you are given two integers L and R (1-indexed) that describe a subarray.
The twist is that the operation to be used for each query is determined by the operation at index L of the operations array. That is, if operations[L-1]
is sum
, then you need to compute the sum of the subarray from index L to R (inclusive). Otherwise, if the operation is product
, you must compute the product of the subarray. When calculating the product, the result should be computed modulo \(10^9+7\).
It is guaranteed that the queries are valid and the arrays are non-empty.
inputFormat
The input is read from standard input (stdin) and has the following format:
Line 1: An integer n, representing the size of the array. Line 2: n space-separated integers representing the array elements. Line 3: n space-separated strings, where each string is either "sum" or "product", representing the operation for each index. Line 4: An integer q, representing the number of queries. Next q lines: Each line contains two integers L and R (1-indexed), indicating the start and end indices for the query.
outputFormat
For each query, print the result on a new line. The result is either the sum of the subarray or the product modulo \(10^9+7\), depending on the corresponding operation specified in the operations array.
## sample5
1 2 3 4 5
sum product sum sum product
3
1 3
2 4
1 5
6
24
15
</p>