All problems

Trail Elevation Queries

Easy · prefix sum

A hiking trail is split into segments, numbered from 0. The list climb gives the change in height across each segment: climb[i] is positive if segment i goes uphill, negative if it goes downhill and 0 if it is flat.

Each query is a pair [l, r] and asks: what is the net change in height after walking every segment from l to r, both included?

Return a list with one answer per query, in the same order as queries. The same query may appear more than once.

Examples

Example 1

Input: climb = [3, -2, 5, -4, 1], queries = [[0, 2], [1, 3], [4, 4]]

Output: [6, -1, 1]

Segments 0 to 2 give 3 - 2 + 5 = 6. Segments 1 to 3 give -2 + 5 - 4 = -1. Segment 4 alone gives 1.

Example 2

Input: climb = [-5, -5, 10], queries = [[0, 2], [0, 0]]

Output: [0, -5]

The whole trail ends at the height it started, so the net change is 0.

Constraints