Bridges
InterviewTime limit1sMemory limit1024 MB
For each range L to R, find the bridge height M (floor average of the endpoints) and sum the scaffolding and tunnel costs over that range.
- Level
Medium5 of 10
- Topics
- Sorting, Prefix sum, Array
- Solved
- No attempts yet
Problem
Architect Marijonas wants to improve transportation in an area where mountains are lined up in a straight line. The area is described by an array of size , where is the height of the -th mountain.
Marijonas plans to build a bridge between two mountains. He has not yet decided which two, but he wants to know the construction cost of a bridge between mountains and .
The cost is computed as follows.
- The bridge height is the average of the two end heights, rounded down:
- The bridge passes over every mountain with .
- If , scaffolding must be built, and the cost is .
- If , a tunnel must be dug, and the cost is .
Marijonas can compute the cost of one bridge easily, but he wants the costs for pairs of mountains. Compute the construction cost for each query.
Input
The first line contains the array size . The second line contains the space-separated integers . The third line contains the number of queries . Each of the next lines contains two space-separated integers and .
Output
Print lines. Each line contains one integer, the construction cost of a bridge between mountains and , in the same order as the queries.
Constraints
- ()
- ()