#P4888. Dreamoon and Notepad
Dreamoon and Notepad
Dreamoon and Notepad
题面翻译
你有一个 行的文本,从上往下第 行文本的长度是 。同时,文本还有一个光标,其位置记作 ,表示其在第 行文本的第 个字符的后面(假如 则其在第 行的开头)。
你可以操作如下按键来控制一个位置在 的光标移动到位置 :
-
up
: -
down
: -
left
: -
right
: -
HOME
: -
END
:
下面,给出 个询问 ,询问如果光标在位置 ,要想移动到 最少需要按多少次按键。
题目描述
Dreamoon has just created a document of hard problems using notepad.exe. The document consists of lines of text, denotes the length of the -th line. He now wants to know what is the fastest way to move the cursor around because the document is really long.
Let be a current cursor position, where is row number and is position of cursor in the row. We have and .
We can use following six operations in notepad.exe to move our cursor assuming the current cursor position is at :
- up key: the new cursor position
- down key: the new cursor position
- left key: the new cursor position
- right key: the new cursor position
- HOME key: the new cursor position
- END key: the new cursor position
You're given the document description ( and sequence ) and queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from to .
输入格式
Dreamoon has just created a document of hard problems using notepad.exe. The document consists of lines of text, denotes the length of the -th line. He now wants to know what is the fastest way to move the cursor around because the document is really long.
Let be a current cursor position, where is row number and is position of cursor in the row. We have and .
We can use following six operations in notepad.exe to move our cursor assuming the current cursor position is at :
- up key: the new cursor position
- down key: the new cursor position
- left key: the new cursor position
- right key: the new cursor position
- HOME key: the new cursor position
- END key: the new cursor position
You're given the document description ( and sequence ) and queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from to .
输出格式
For each query print the result of the query.
样例 #1
样例输入 #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
样例输出 #1
2
5
3
2
样例 #2
样例输入 #2
2
10 5
1
1 0 1 5
样例输出 #2
3
提示
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.