#P4842. Drazil and Park

Drazil and Park

Drazil and Park

题面翻译

有一只猴子,他生活在一个环形的公园里。有 nn 棵树围绕着公园。第ii棵树和第i+1i+1棵树之间的距离是 did_i ,而第n棵树和第一棵树之间的距离是 dnd_n 。第i棵树的高度是 hih_i

这只猴子每天要进行晨跑。晨跑的步骤如下:

· 他先选择两棵树;

· 然后爬上第一棵树;

· 再从第一棵树上下来,接着围绕着公园跑(有两个可能的方向)到第二棵树,然后爬上第二棵树;

· 最后从第二棵树上下来。

但是有一些小孩会在连续的一些树上玩耍。所以猴子不能经过这些树。

比如现在猴子选择的第xx棵和第yy棵树,那么该早晨他消耗的能量是 2(hx+hy)+dist(x,y)2(hx+hy)+dist(x,y) 。由于某一条路径是被小孩子占据的,所以他只能跑另外一条,因此 dist(x,y)dist(x,y) 是确定的。

现在给出第i天,孩子们会在第 aia_i 棵树和 bi 棵树之间玩耍。具体的,如果 aibiai≤bi ,那么孩子玩耍的区间就是 [ai,bi][ai,bi] ,否则孩子玩耍的区间就是 [ai,n][1,bi][ai,n]⋃[1,bi]

请帮助这只猴子找出两棵树,让他晨跑的时候他能够消耗最大的能量。

Powered by Tony102

题目描述

Drazil is a monkey. He lives in a circular park. There are n n trees around the park. The distance between the i i -th tree and ( i+1 i+1 )-st trees is di d_{i} , the distance between the n n -th tree and the first tree is dn d_{n} . The height of the i i -th tree is hi h_{i} .

Drazil starts each day with the morning run. The morning run consists of the following steps:

  • Drazil chooses two different trees
  • He starts with climbing up the first tree
  • Then he climbs down the first tree, runs around the park (in one of two possible directions) to the second tree, and climbs on it
  • Then he finally climbs down the second tree.

But there are always children playing around some consecutive trees. Drazil can't stand children, so he can't choose the trees close to children. He even can't stay close to those trees.

If the two trees Drazil chooses are x x -th and y y -th, we can estimate the energy the morning run takes to him as 2(hx+hy)+dist(x,y) 2(h_{x}+h_{y})+dist(x,y) . Since there are children on exactly one of two arcs connecting x x and y y , the distance dist(x,y) dist(x,y) between trees x x and y y is uniquely defined.

Now, you know that on the i i -th day children play between ai a_{i} -th tree and bi b_{i} -th tree. More formally, if ai<=bi a_{i}<=b_{i} , children play around the trees with indices from range [ai,bi] [a_{i},b_{i}] , otherwise they play around the trees with indices from .

Please help Drazil to determine which two trees he should choose in order to consume the most energy (since he wants to become fit and cool-looking monkey) and report the resulting amount of energy for each day.

输入格式

The first line contains two integer n n and m m ( 3<=n<=105 3<=n<=10^{5} , 1<=m<=105 1<=m<=10^{5} ), denoting number of trees and number of days, respectively.

The second line contains n n integers d1,d2,...,dn d_{1},d_{2},...,d_{n} ( 1<=di<=109 1<=d_{i}<=10^{9} ), the distances between consecutive trees.

The third line contains n n integers h1,h2,...,hn h_{1},h_{2},...,h_{n} ( 1<=hi<=109 1<=h_{i}<=10^{9} ), the heights of trees.

Each of following m m lines contains two integers ai a_{i} and bi b_{i} ( 1<=ai,bi<=n 1<=a_{i},b_{i}<=n ) describing each new day. There are always at least two different trees Drazil can choose that are not affected by children.

输出格式

For each day print the answer in a separate line.

样例 #1

样例输入 #1

5 3
2 2 2 2 2
3 5 2 1 4
1 3
2 2
4 5

样例输出 #1

12
16
18

样例 #2

样例输入 #2

3 3
5 1 4
5 1 4
3 3
2 2
1 1

样例输出 #2

17
22
11