PREFPRO2 - Editorial

Problem Link - Optimization Using Prefix Array

Problem Statement

Given an array of length n, we need to perform m query over the array.

In each query you need to find the sum of the element in the range a to b.

Approach

The prefix sum technique preprocesses the array so that each element at index i in the prefix_sum array represents the sum of elements from the start to index i. For a query with range a to b, the sum is calculated as prefix_sum[b] - prefix_sum[a-1], with special handling when a = 0.

Time Complexity:

The time complexity is O(n \times m) because for each query, we might have to sum n elements in the worst case.

Space Complexity:

The space complexity is O(n) due to the storage of the array v1.