CF1709B.Also Try Minecraft

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

Also Try Minecraft

You are beta testing the new secret Terraria update. This update will add quests to the game!

Simply, the world map can be represented as an array of length nn, where the ii-th column of the world has height aia_i.

There are mm quests you have to test. The jj-th of them is represented by two integers sjs_j and tjt_j. In this quest, you have to go from the column sjs_j to the column tjt_j. At the start of the quest, you are appearing at the column sjs_j.

In one move, you can go from the column xx to the column x1x-1 or to the column x+1x+1. In this version, you have Spectre Boots, which allow you to fly. Since it is a beta version, they are bugged, so they only allow you to fly when you are going up and have infinite fly duration. When you are moving from the column with the height pp to the column with the height qq, then you get some amount of fall damage. If the height pp is greater than the height qq, you get pqp - q fall damage, otherwise you fly up and get 00 damage.

For each of the given quests, determine the minimum amount of fall damage you can get during this quest.

Input

The first line of the input contains two integers nn and mm (2n105;1m1052 \le n \le 10^5; 1 \le m \le 10^5) — the number of columns in the world and the number of quests you have to test, respectively.

The second line of the input contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9), where aia_i is the height of the ii-th column of the world.

The next mm lines describe quests. The jj-th of them contains two integers sjs_j and tjt_j (1sj,tjn;sjtj1 \le s_j, t_j \le n; s_j \ne t_j), which means you have to move from the column sjs_j to the column tjt_j during the jj-th quest.

Note that sjs_j can be greater than tjt_j.

Output

Print mm integers. The jj-th of them should be the minimum amount of fall damage you can get during the jj-th quest completion.

Samples

7 6
10 8 9 6 8 12 7
1 2
1 7
4 6
7 1
3 5
4 2
2
10
0
7
3
1

在线编程 IDE

建议全屏模式获得最佳体验