Infinite arrays

4.2

37 votes
1-D Array, Arrays, Data Structures, Easy, One-dimensional
Problem

You are given an array A of size N. You have also defined an array B as the concatenation of array A for infinite number of times. 
For example, if A={A1,A2,A3}, then B={A1,A2,A3,A1,A2,A3,.....}.

Now, you are given Q queries. Each query consists of two integers, Li and Ri. Your task is to calculate the sum of the subarray of N from index Li to Ri.

Note: As the value of this output is very large, print the answer as modulus 109+7.

Input format

  • First line: T denoting the number of test cases
  • For each test case:
    • First line: Contains N, the size of the array
    • Second line: Contains N space-separated integers corresponding to Ai
    • Third line: Contains Q denoting the number of queries
    • Fourth line: Contains Q space-separated integers corresponding to Li
    • Fifth line: Contains Q space-separated integers corresponding to Ri

Output format

For each test case, print Q space-separated integers that denote the answers of the provided Q queries. Print the answer to each test case in a new line.

Constraints

1T1001N1041Q1041Ai1091LiRi1018

Sample Input
1
3
4 1 5
3
1 3 9
4 7 10
Sample Output
14 19 9 
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

A={4,1,5}B={4,1,5,4,1,5,4,1,5,4,1,5,...}

Query #1: 
L=1, R=4, elements are {4,1,5,4}. Sum=4+1+5+4=14.

Query #2:
L=3, R=7, elements are {5,4,1,5,4}. Sum=5+4+1+5+4=19.

Query #3:
L=9, R=10, elements are {5,4}. Sum=5+4=9.

Editor Image

?