#P4888. Dreamoon and Notepad

    ID: 2526 传统题 4000ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>基础算法二分数据结构ST 表CodeForces

Dreamoon and Notepad

Dreamoon and Notepad

题面翻译

你有一个 nn 行的文本,从上往下第 ii 行文本的长度是 aia_i。同时,文本还有一个光标,其位置记作 (r,c)(r,c),表示其在第 rr 行文本的第 cc 个字符的后面(假如 c=0c=0 则其在第 rr 行的开头)。

你可以操作如下按键来控制一个位置在 (r,c)(r,c) 的光标移动到位置 (r,c)(r',c')

  • up(r,c)=(r1,min{ar,c})(r',c')=\Big(r-1,\min\{a_{r'},c\}\Big)

  • down(r,c)=(r+1,min{ar,c})(r',c')=\Big(r+1,\min\{a_{r'},c\}\Big)

  • left(r,c)=(r,max{0,c1})(r',c')=\Big(r,\max\{0,c-1\}\Big)

  • right(r,c)=(r,min{ar,c+1})(r',c')=\Big(r,\min\{a_{r},c+1\}\Big)

  • HOME(r,c)=(r,0)(r',c')=\Big(r,0\Big)

  • END(r,c)=(r,ar)(r',c')=\Big(r,a_r\Big)

下面,给出 qq 个询问 (r1,c1),(r2,c2)(r_1,c_1),(r_2,c_2),询问如果光标在位置 (r1,c1)(r_1,c_1),要想移动到 (r2,c2)(r_2,c_2) 最少需要按多少次按键。

  • n,q4×105,1ai108n,q\leq4\times10^5,1\leq a_i\leq10^8

题目描述

Dreamoon has just created a document of hard problems using notepad.exe. The document consists of n n lines of text, ai a_{i} denotes the length of the i 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) (r,c) be a current cursor position, where r r is row number and c c is position of cursor in the row. We have 1<=r<=n 1<=r<=n and 0<=c<=ar 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) (r,c) :

  1. up key: the new cursor position (nr,nc)=(max(r1,1),min(anr,c)) (nr,nc)=(max(r-1,1),min(a_{nr},c))
  2. down key: the new cursor position (nr,nc)=(min(r+1,n),min(anr,c)) (nr,nc)=(min(r+1,n),min(a_{nr},c))
  3. left key: the new cursor position (nr,nc)=(r,max(0,c1)) (nr,nc)=(r,max(0,c-1))
  4. right key: the new cursor position (nr,nc)=(r,min(anr,c+1)) (nr,nc)=(r,min(a_{nr},c+1))
  5. HOME key: the new cursor position (nr,nc)=(r,0) (nr,nc)=(r,0)
  6. END key: the new cursor position (nr,nc)=(r,ar) (nr,nc)=(r,a_{r})

You're given the document description ( n n and sequence ai a_{i} ) and q q queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from (r1,c1) (r_{1},c_{1}) to (r2,c2) (r_{2},c_{2}) .

输入格式

Dreamoon has just created a document of hard problems using notepad.exe. The document consists of n n lines of text, ai a_{i} denotes the length of the i 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) (r,c) be a current cursor position, where r r is row number and c c is position of cursor in the row. We have 1<=r<=n 1<=r<=n and 0<=c<=ar 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) (r,c) :

  1. up key: the new cursor position (nr,nc)=(max(r1,1),min(anr,c)) (nr,nc)=(max(r-1,1),min(a_{nr},c))
  2. down key: the new cursor position (nr,nc)=(min(r+1,n),min(anr,c)) (nr,nc)=(min(r+1,n),min(a_{nr},c))
  3. left key: the new cursor position (nr,nc)=(r,max(0,c1)) (nr,nc)=(r,max(0,c-1))
  4. right key: the new cursor position (nr,nc)=(r,min(anr,c+1)) (nr,nc)=(r,min(a_{nr},c+1))
  5. HOME key: the new cursor position (nr,nc)=(r,0) (nr,nc)=(r,0)
  6. END key: the new cursor position (nr,nc)=(r,ar) (nr,nc)=(r,a_{r})

You're given the document description ( n n and sequence ai a_{i} ) and q q queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from (r1,c1) (r_{1},c_{1}) to (r2,c2) (r_{2},c_{2}) .

输出格式

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.