#C2518. Array Rotation and Sum Queries
Array Rotation and Sum Queries
Array Rotation and Sum Queries
You are given an array \( A \) of size \( N \) and a series of rotation operations. Each rotation operation consists of an integer \( k \) which indicates that the array should be rotated to the right by \( k \) positions. Formally, a right rotation by \( k \) transforms the array \( A = [a_1, a_2, \ldots, a_N] \) into \( A' = [a_{N-k+1}, \ldots, a_N, a_1, \ldots, a_{N-k}] \) (using modulo arithmetic as necessary).
After performing all rotations (i.e. computing \( \text{total} = \sum{k_i} \) and then \( \text{total} \mod N \)), you need to answer several sum queries. Each query provides two indices \( L \) and \( R \) (1-based indexing), and you must output the sum of the subarray of the rotated array from index \( L \) to \( R \), inclusive.
For example, for the array \( [1, 2, 3, 4, 5] \) with two rotations of 1 each, the total rotation is \( 2 \) (since \( 1+1=2 \)) and the rotated array becomes \( [4, 5, 1, 2, 3] \). A query \( (1, 2) \) would then yield \( 4+5=9 \).
inputFormat
The input begins with an integer \( T \), representing the number of test cases. For each test case, the input format is as follows:
- The first line contains an integer \( N \) (the size of the array).
- The second line contains \( N \) space-separated integers representing the elements of the array.
- The third line contains an integer \( R \) (the number of rotation operations).
- The fourth line contains \( R \) space-separated integers where each integer represents the right-rotation value.
- The fifth line contains an integer \( S \) (the number of sum queries).
- Each of the next \( S \) lines contains two space-separated integers \( L \) and \( R \) denoting the start and end indices for the sum query (1-indexed).
outputFormat
For every query provided in the input, output a single line containing the sum of the subarray (from index \( L \) to \( R \) in the rotated array).
## sample1
5
1 2 3 4 5
2
1 1
3
1 2
2 3
3 5
9
6
6
</p>