VZLA2019L - Learn You a Haskell for Great Good!
Gus is really smart guy. He really like challenges and he wants to be the best functional programmer in the world. He knows that the best way to start is reading the Learn You a Haskell for Great Good! book. But Gus isn’t so good at making decisions, nevertheless Gus is super good making “problem resolver” friends. So, let's solve this problem and help Gus!
There are n chapters in the book, the time that takes to move from the chapter i to the chapter (i+1) or vice versa, is ti. The time to move from the chapter n-th to the first one, or vice versa, is tn (yes, Gus sometimes is weird and starts to read from the end of the book). Also, all the chapters have different difficulty for Gus. The difficulty of the i-th chapter is di.
Gus tries to read some chapters each day in the morning. He does the following steps each morning:
- He chooses two different chapters i and j.
- He starts to read the first selected chapter.
- When he finished the first chapter he moves to the next chapter (in one of the two possible directions).
- He reads all chapters between i and j i.e. (i, i + 1 ... j) or (i, i − 1 ... j)
But Marcos is a competitive person and he wants to be the best functional programmer too. So each day in the morning he takes away some consecutive chapters of Gus’s book and he puts them back in the night. Gus can't stand the disorder, so he can't read the chapters in a non-consecutive order (remember that for Gus the consecutive of the chapter i is the chapter i+1 or the chapter i-1).
If the two chapters that Gus chooses are x-th and y-th, we can estimate the energy the morning reading takes to him as 2 * (dx + dy) + time(x, y). Since Marcos takes away chapters on exactly one of the two sequences connecting x and y, the time time(x, y) between chapters x and y is uniquely defined.
In the i-th day Marcos will take away the chapters between ai and bi. More formally, if ai ≤ bi, Marcos takes away the chapters with indices from range [ai, bi], otherwise he takes away the chapters with indices from [ai, n] ∪ [1, bi].
Please help Gus to decide which two chapters he should choose in order to consume the most energy.
Input
The first line contains two integer n and m, denoting number of chapters and number of days, respectively.
The second line contains n integers t1, t2 ... tn, the time between consecutive chapters.
The third line contains n integers d1, d2 ... dn, the difficulty of chapters.
Each of following m lines contains two integers ai and bi describing each new day. There are always at least two different chapters Gus can choose that are not affected by Marcos.
Output
For each day print the answer in a separate line.
Example
Input: 3 3 5 1 4 5 1 4 3 3 2 2 1 1 Output: 17 22 11
Constraints
- (3 ≤ n ≤ 105, 1 ≤ m ≤ 105)
- t1, t2 ... tn (1 ≤ ti ≤ 109)
- d1, d2 ... dn (1 ≤ di ≤ 109)
- ai and bi (1 ≤ ai, bi ≤ n)
hide comments
[Rampage] Blue.Mary:
2019-10-29 08:02:58
Thanks for this problem set. (Most problem is a bit easier, maybe due to the fact that this is a subregional contest problem set.) |
|
supernanren:
2019-10-28 18:06:16
@Samuel Nacache Thank you very much for the quick response and all these nice problems. |
|
Samuel Nacache:
2019-10-28 17:32:35
@supernanren sorry, forgot to include the constraints |
|
supernanren:
2019-10-28 17:31:42
what's the bound of n? |
Added by: | Samuel Nacache |
Date: | 2019-10-27 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All |
Resource: | Genesis Kufatty - Used for Venezuelan 2019 ICPC Local Contest |