#P3889. [GDOI2014] 吃

[GDOI2014] 吃

Description

Senior Brother W had planned for a long time and finally succeeded in opening a sushi restaurant in BG.

Just as he was still excited, bad news came: foodie Senior Sister L actually learned about this and was on her way. Senior Brother W instantly felt a chill, but being clever, he immediately came up with a strategy to deal with Senior Sister L.

When Senior Sister L arrived at the sushi restaurant, she first looked around and found that she was the only customer. Below are her selection rules:

  1. The sushi in the restaurant is arranged in a single row of NN plates, numbered from 11 to NN from left to right.
  2. The number of sushi on each plate is fixed and protected by a glass cover.
  3. At each selection time, Senior Sister L may choose one plate from a continuous interval [l,r][l, r], and then choose another plate outside that interval (if there are plates outside the interval).

Senior Sister L finds that the chefs here work very fast and can always restore the sushi counts to their original numbers before the next selection time.

As a foodie with standards, after selecting two plates, Senior Sister L will decide to eat exactly DD pieces of sushi per bite, such that both plates can be finished exactly with no leftovers. For example, if the two plates contain 22 and 44 pieces, then D=1D = 1 or D=2D = 2 will finish both plates exactly. If the two plates contain 33 and 55 pieces, then only D=1D = 1 works.

Senior Sister L does not care about the total amount of food; she cares about the feeling of eating multiple pieces in one bite. Thus, if she can eat DD pieces per bite, her joy value is DD. If she cannot select two plates, her joy value is 00.

Given the number of sushi on each plate, Senior Sister L wants to know, after each selection time, what is the maximum joy value she can obtain.

Input Format

  • The first line contains an integer NN, the number of sushi plates.
  • The second line contains NN integers a1,a2,,aNa_1, a_2, \dots, a_N, where aia_i is the number of sushi on the ii-th plate.
  • The third line contains an integer MM, the number of selection times.
  • The next MM lines each contain two integers li,ril_i, r_i with 1liriN1 \le l_i \le r_i \le N, as described above.

Output Format

Output MM lines. The ii-th line contains the maximum possible joy value DD for the ii-th selection time.

5
1 2 3 4 5
2
2 3
2 4
2
1
5
2 4 8 16 32
2
3 4
2 3
16
8

Hint

Sample explanation:

  • In sample 1, for the first selection time, you can choose 22 and 44, so Senior Sister L can eat 22 pieces per bite and finish both plates exactly. For the second selection time, no matter which two plates she chooses, she can only eat 11 piece per bite.
  • In sample 2, for the first selection time, she can choose 1616 and 3232. For the second selection time, she can choose 88 and 1616 or 88 and 3232.

Constraints:

  • For 20% of the testdata, N100N \le 100, M100M \le 100, max(a1,a2,,aN)100\max(a_1, a_2, \dots, a_N) \le 100.
  • For 50% of the testdata, N10000N \le 10000, M10000M \le 10000, max(a1,a2,,aN)10000\max(a_1, a_2, \dots, a_N) \le 10000.
  • For 100% of the testdata, N100000N \le 100000, M100000M \le 100000, max(a1,a2,,aN)100000\max(a_1, a_2, \dots, a_N) \le 100000.

Translated by ChatGPT 5