A park ranger patrols a straight ridge trail lined with n numbered checkpoints, positioned in order 1 through n. Inspecting checkpoint i takes c_i minutes. Walking directly from checkpoint i to checkpoint i+1 (for 1 <= i <= n-1) takes h_i minutes. Over the course of a day the ranger receives q separate tour requests. For the j-th request, given as a range [l, r] with l <= r, the ranger starts already standing at checkpoint l, inspects checkpoints l, l+1, ..., r one after another in increasing order (walking between each consecutive pair of checkpoints along the way), and stops immediately once checkpoint r has been inspected — no walk back and no other checkpoints are visited. For every request, report how many minutes that tour takes in total: the sum of the inspection times of every checkpoint from l to r, plus the sum of the walking times between every pair of consecutive checkpoints from l to r.
n.n space-separated integers c_1 ... c_n.n - 1 space-separated integers h_1 ... h_{n-1} (this line contributes no numbers, and may be blank, when n = 1).q.q lines contains two space-separated integers l and r describing one tour request.Print q lines. The j-th line must contain the total duration, in minutes, of the j-th tour request.
Example 1
Input
4 5 3 8 2 1 4 2 2 1 4 2 3
Expected
25 15
Explanation
Checkpoints 1..4 have inspection times 5,3,8,2 and walking times between them 1,4,2. Query (1,4) covers all checkpoints: inspection sum 5+3+8+2=18 plus walking sum 1+4+2=7, giving 25. Query (2,3) covers checkpoints 2 and 3: inspection sum 3+8=11 plus the single walking segment between them, 4, giving 15.
Example 2
Input
1 7 1 1 1
Expected
7
Explanation
There is only one checkpoint, so there are no walking times at all. The single query (1,1) just inspects checkpoint 1, taking its inspection time of 7 minutes with no walking, so the answer is 7.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →