Another Random Contest 1 P2 - Two Sequences

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

Given two sequences filled with non-negative integers, A and B, of length N, you will answer Q queries, each consisting of two numbers, li and ri. For each query, find a K where liKri1 that will minimize max(Ali+Ali+1++AK1+AK,BK+1+BK+2++Bri1+Bri) and output the minimized value.

Constraints

For all subtasks:

2N2×105

1Q2×105

1li<riN

0Ai,Bi109

Subtask 1 [20%]

2N5×103

1Q5×103

Subtask 2 [80%]

No additional constraints.

Input Specification

The first line consists of two integers N and Q, the length of the sequences and the number of queries.

The next two lines consist of N integers.

The following Q lines consist of two integers li and ri.

Output Specification

Output the minimized value for each query on Q separate lines.

Sample Input

Copy
4 4
7 5 3 5
6 2 9 1
1 2
2 3
3 4
1 2

Sample Output

Copy
7
9
3
7

Comments

There are no comments at the moment.