Scroll-a-palooza

4.2

5 votes
Binary Search, Algorithms
Problem

In a small town, there's a ritual where each day, every resident takes a series of steps, either to the right or left, over N days. Starting at the town center, each day, they move according to the guidance of a mystical scroll: a list of values, X which determines their direction. On the ith day, a positive X[i] sends them to the right by X[i] steps, a negative X[i] sends them to the left by X[i] steps, and a zero means they stay where they are. Over time, the townsfolk became curious about their wanderings.

They posed M queries, each asking: on which days did residents find themselves at least W steps away from the town center, both for the first time and the last time? If residents are never at least W steps away from the town center, the answer is 1.

Input format

  • The first line contains a single integer T, which denotes the number of test cases.
  • For each test case:
    • The first line contains N denoting the number of days.
    • The second line contains N space-separated integers, denoting the values of scroll X.
    • The next line contains M denoting the number of queries.
    • The next M lines contain an integer, denoting W.

Output format

For each test case, answer all the M queries. For each query, print two days on which residents are at least a W steps away from the town center, the first and last time in a new line.  If residents are never at least W steps away from the town center, print 1 in a new line. 

Constraints

1T1051N1061012X[i]1012  i[1,N]1M1061W1018The sum of all values of N over all test cases doesn't exceed 106The sum of all values of M over all test cases doesn't exceed 106

 

Sample Input
1
4
1 -3 5 -1
3
1
3
4
Sample Output
1 4
3 3
-1
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

The first line denotes T = 1.

For test case 1:

  • N = 4
  • X = [1, -3, 5, -1]
  • M = 4

So,

  • On the 1st day, the residents moved 1 step towards the right and ended up being 1 step away from the town center.
  • On the 2nd day, the residents moved 3 steps towards the left and ended up being 2 steps away from the town center.
  • On the 3rd day, the residents moved 5 steps towards the right and ended up 3 steps from the town center.
  • On the 4th day, the residents moved 1 step towards the left and ended up being 2 steps away from the town center.

Now,

  • For the first query, we are given W = 1; the first and the last days when the residents are at least 1 step away from the town center are 1 and 4. So, the answer to this query is 1 4.
  • For the second query, we are given W = 3; the first and the last day when the residents are at least 3 steps away from the town center is 3. So, the answer to this query is 3 3.
  • For the third query, we are given W = 4; residents are never at least 4 steps away from the town center. So, the answer to this query is -1.
Editor Image

?