Codeforces Round 272 (Div. 1)
E
Dreamoon and Notepad
Codeforces
3100
原题
← D→
翻译中…

Dreamoon has just created a document of hard problems using notepad.exe. The document consists of $*n*$ lines of text, $*a*_{*i*}$ denotes the length of the $*i*$-th line. He now wants to know what is the fastest way to move the cursor around because the document is really long.

翻译中…

Let $(*r*, *c*)$ be a current cursor position, where $*r*$ is row number and $*c*$ is position of cursor in the row. We have $1 ≤ *r* ≤ *n*$ and $0 ≤ *c* ≤ *a*_{*r*}$.

翻译中…

We can use following six operations in notepad.exe to move our cursor assuming the current cursor position is at $(*r*, *c*)$:

翻译中…
  1. up key: the new cursor position $(*nr*, *nc*) = (*max*(*r* - 1, 1), *min*(*a*_{*nr*}, *c*))$
  2. down key: the new cursor position $(*nr*, *nc*) = (*min*(*r* + 1, *n*), *min*(*a*_{*nr*}, *c*))$
  3. left key: the new cursor position $(*nr*, *nc*) = (*r*, *max*(0, *c* - 1))$
  4. right key: the new cursor position $(*nr*, *nc*) = (*r*, *min*(*a*_{*nr*}, *c* + 1))$
  5. HOME key: the new cursor position $(*nr*, *nc*) = (*r*, 0)$
  6. END key: the new cursor position $(*nr*, *nc*) = (*r*, *a*_{*r*})$
翻译中…

You're given the document description ($*n*$ and sequence $*a*_{*i*}$) and $*q*$ queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from $(*r*_{1}, *c*_{1})$ to $(*r*_{2}, *c*_{2})$.

翻译中…

Input

翻译中…

The first line contains an integer $*n*(1 ≤ *n* ≤ 400, 000)$ — the number of lines of text.

翻译中…

The second line contains $*n*$ integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}(1 ≤ *a*_{*i*} ≤ 10^{8})$.

翻译中…

The third line contains an integer $*q*(1 ≤ *q* ≤ 400, 000)$.

翻译中…

Each of the next $*q*$ lines contains four integers $*r*_{1}, *c*_{1}, *r*_{2}, *c*_{2}$ representing a query ($1 ≤ *r*_{1}, *r*_{2} ≤ *n*, 0 ≤ *c*_{1} ≤ *a*_{*r*1}, 0 ≤ *c*_{2} ≤ *a*_{*r*2}$).

翻译中…

Output

翻译中…

For each query print the result of the query.

翻译中…

Note

翻译中…

In the first sample, the first query can be solved with keys: HOME, right.

翻译中…

The second query can be solved with keys: down, down, down, END, down.

翻译中…

The third query can be solved with keys: down, END, down.

翻译中…

The fourth query can be solved with keys: END, down.

翻译中…

样例

Input #1
9
1 3 5 3 1 3 5 3 1
4
3 5 3 1
3 3 7 3
1 0 3 3
6 0 7 3
Output #1
2
5
3
2
InputCopy OutputCopy
Input #2
2
10 5
1
1 0 1 5
Output #2
3
InputCopy OutputCopy
题目信息
题号
477E
时间限制
3.5 s
内存限制
256 MB
标签
数据结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。