Two Railroads solution codechef

Two Railroads solution codechef

There are n cities in Berland, and they are connected by two railroads — the Main railroad and the Auxiliary railroad. Each city has two railway stations, one connected to the Main railroad (called the Main station), and the other connected to the Auxiliary railroad.

The railroads are identical in their structure. The Main railroad consists of n1 railroad segments; the i-th railroad segment connects the Main station of the city i with the Main station of the city i+1. Similarly, the Auxiliary railroad consists of n1 railroad segments; the i-th railroad segment connects the Auxiliary station of the city i with the Auxiliary station of the city i+1.

Two Railroads solution codechef

These railroads are used to transfer different goods and resources from one city to another. In particular, the Ministry of Energetics is interested in using these railroads to transfer coal.

The Ministry has estimated the following capabilities of the railroads:

  • for every i[1,n1], at most ai tons of coal per day can be transferred from the Main station i to the Main station i+1 (only in this direction);
  • for every i[1,n1], at most bi tons of coal per day can be transferred from the Auxiliary station i to the Auxiliary station i+1 (only in this direction);
  • for every i[1,n], at most ci tons of coal per day can be transferred from the Main station i to the Auxiliary station ior in the opposite direction.

Two Railroads solution codechef

To analyze the capacity of the whole railroad network, the Ministry requires a software that would process and answer queries of the following format:

  • calculate the maximum number of tons of coal that can be transferred per day from the Main station li to the Main station ri.

Your task is to implement this software.

Input Two Railroads solution codechef

The first line contains one integer n (2n3105) — the number of cities.

The second line contains n1 integers a1,a2,,an1 (1ai109).

The third line contains n1 integers b1,b2,,bn1 (1bi109).

The fourth line contains n integers c1,c2,,cn (1ci109).

The fifth line contains one integer q (1q3105) — the number of queries.

Then q lines follow, the i-th line contains two integers li and ri (1li<rin) — the parameters of the i-th query.

Output Two Railroads solution codechef

Print q integers, where the i-th integer should be the answer to the i-th query, i. e. the maximum number of tons of coal that can be transferred per day from the Main station li to the Main station ri.

Example
input

Copy Two Railroads solution codechef

5
3 4 7 4
8 5 3 5
10 5 3 4 10
4
1 4
1 2
3 4
2 4
output

Copy Two Railroads solution codechef

9 8 10 9  

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*