QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 2048 MB

#22848. Longest Symmetry

Statistics

There are $N$ houses numbered $1, 2, \ldots, N$ on a number line. House $i$ is located at coordinate $X_i$. It is guaranteed that $X_i < X_{i+1}$ for every $i$ ($1 \le i \le N - 1$).

For integers $a, b$ with $1 \le a < b \le N$, we say that the consecutive houses $a, a + 1, \ldots, b$ form a symmetric interval of houses if the following condition holds:

  • For every $i = 0, 1, \ldots, b - a$, $X_a + X_b = X_{a+i} + X_{b-i}$ holds.

You are given $Q$ queries. Process them in order.

  • In the $i$-th query, you are given integers $l_i, r_i$ ($1 \le l_i < r_i \le N$). Among all pairs of integers $(s, t)$ satisfying $l_i \le s < t \le r_i$ such that the consecutive houses $s, s + 1, \ldots, t$ form a symmetric interval of houses, output the maximum possible width $X_t - X_s$ of the interval.

Input

The input consists of a single test case of the following format:

$$ \begin{aligned} &N \\ &X_1 \ X_2 \ \ldots \ X_N \\ &Q \\ &l_1 \ r_1 \\ &l_2 \ r_2 \\ &\vdots \\ &l_Q \ r_Q \end{aligned} $$

The first line contains an integer $N$, representing the number of houses ($2 \le N \le 2 \times 10^5$).

The second line contains $N$ integers $X_1, X_2, \ldots, X_N$, representing the coordinates of the houses ($-10^9 \le X_i \le 10^9$). It is guaranteed that $X_i < X_{i+1}$ for every $i$ ($1 \le i \le N - 1$).

The third line contains an integer $Q$, representing the number of queries ($1 \le Q \le 2 \times 10^5$).

Each of the following $Q$ lines contains two integers $l_i$ and $r_i$, representing the left and right endpoints of the range specified in the $i$-th query ($1 \le l_i < r_i \le N$).

Output

For each query, output the maximum possible width.

Example

Input 1

7
-5 -2 0 4 6 9 12
5
1 7
2 6
3 7
4 7
3 6

Output 1

14
8
6
6
4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.