CF BUDDY
← Problems·

1933E · Turtle vs. Rabbit Race: Optimal Trainings

1500 · binary search, implementation, math

Problem: Isaac begins his training. There are nn running tracks available, and the ii-th track (1in1 \le i \le n) consists of aia_i equal-length sections.

Given an integer uu (1u1091 \le u \le 10^9), finishing each section can increase Isaac's ability by a certain value, described as follows:

  • Finishing the 11-st section increases Isaac's performance by uu.
  • Finishing the 22-nd section increases Isaac's performance by u1u-1.
  • Finishing the 33-rd section increases Isaac's performance by u2u-2.
  • \ldots
  • Finishing the kk-th section (k1k \ge 1) increases Isaac's performance by u+1ku+1-k. (The value u+1ku+1-k can be negative, which means finishing an extra section decreases Isaac's performance.)

You are also given an integer ll. You must choose an integer rr such that lrnl \le r \le n and Isaac will finish each section of each track l,l+1,,rl, l + 1, \dots, r (that is, a total of i=lrai=al+al+1++ar\sum_{i=l}^r a_i = a_l + a_{l+1} + \ldots + a_r sections).

Answer the following question: what is the optimal rr you can choose that the increase in Isaac's performance is maximum possible?

If there are multiple rr that maximize the increase in Isaac's performance, output the smallest rr.

To increase the difficulty, you need to answer the question for qq different values of ll and uu.

Input Format: The first line of input contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The descriptions of the test cases follow.

The first line contains a single integer nn (1n1051 \le n \le 10^5).

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1041 \le a_i \le 10^4).

The third line contains a single integer qq (1q1051 \le q \le 10^5).

The next qq lines each contain two integers ll and uu (1ln,1u1091 \le l \le n, 1 \le u \le 10^9) — the descriptions to each query.

The sum of nn over all test cases does not exceed 21052 \cdot 10^5. The sum of qq over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output qq integers: the ii-th integer contains the optimal rr for the ii-th query. If there are multiple solutions, output the smallest one.

Note: For the 11-st query in the first test case:

  • By choosing r=3r = 3, Isaac finishes a1+a2+a3=3+1+4=8a_1 + a_2 + a_3 = 3 + 1 + 4 = 8 sections in total, hence his increase in performance is u+(u1)++(u7)=8+7+6+5+4+3+2+1=36u+(u-1)+\ldots+(u-7)=8+7+6+5+4+3+2+1 = 36.
  • By choosing r=4r = 4, Isaac finishes a1+a2+a3+a4=3+1+4+1=9a_1 + a_2 + a_3 + a_4 = 3 + 1 + 4 + 1 = 9 sections in total, hence his increase in performance is u+(u1)++(u8)=8+7+6+5+4+3+2+1+0=36u+(u-1)+\ldots+(u-8)=8+7+6+5+4+3+2+1+0 = 36.

Both choices yield the optimal increase in performance, however we want to choose the smallest rr. So we choose r=3r = 3.

For the 22-nd query in the first test case, by choosing r=4r = 4, Isaac finishes a2+a3+a4=1+4+1=6a_2 + a_3 + a_4 = 1 + 4 + 1 = 6 sections in total, hence his increase in performance is u+(u1)++(u5)=7+6+5+4+3+2=27u+(u-1)+\ldots+(u-5)=7+6+5+4+3+2 = 27. This is the optimal increase in performance.

For the 33-rd query in the first test case:

  • By choosing r=5r = 5, Isaac finishes a5=5a_5 = 5 sections in total, hence his increase in performance is u+(u1)++(u4)=9+8+7+6+5=35u+(u-1)+\ldots+(u-4)=9+8+7+6+5 = 35.
  • By choosing r=6r = 6, Isaac finishes a5+a6=5+9=14a_5 + a_6 = 5 + 9 = 14 sections in total, hence his increase in performance is u+(u1)++(u13)=9+8+7+6+5+4+3+2+1+0+(1)+(2)+(3)+(4)=35u+(u-1)+\ldots+(u-13)=9+8+7+6+5+4+3+2+1+0+(-1)+(-2)+(-3)+(-4) = 35.

Both choices yield the optimal increase in performance, however we want to choose the smallest rr. So we choose r=5r = 5.

Hence the output for the first test case is [3,4,5][3, 4, 5].

Sample Cases

Case 1

Input

5
6
3 1 4 1 5 9
3
1 8
2 7
5 9
1
10
1
1 1
9
5 10 9 6 8 3 10 7 3
5
8 56
1 12
9 3
1 27
5 45
5
7 9 2 5 2
10
1 37
2 9
3 33
4 32
4 15
2 2
4 2
2 19
3 7
2 7
10
9 1 6 7 6 3 10 7 3 10
5
10 43
3 23
9 3
6 8
5 14

Output

3 4 5 
1 
9 2 9 4 9 
5 2 5 5 5 2 4 5 4 2 
10 6 9 7 7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback