Prefix sum [C language]

prefix and

introduce

The prefix sum algorithm is an optimization technique that is often used to solve query operations in array problems, such as interval summation, interval maximum/minimum, etc. The basic idea is to preprocess a prefix and an array first, and then calculate the difference between the prefix and the array to obtain the query result when a query is required. This algorithm can answer many queries in O(1) time, so it is widely used in practical programming.come on

origin

The origin of the prefix sum algorithm can be traced back to the continuous subsequence problem posed by Cattelan mathematician Eugène Charles Catalan in 1878. The problem requires finding the sum of all consecutive subsequences in an array of length n, and counting the number of subsequences equal to a given value. Catalan found that if the prefix sum of the array is calculated first, the subsequence sum between any two positions can be calculated by the difference between the prefix and the array. With this idea, he successfully solved the problem and extended the prefix and algorithm to a wider range of applications.

usage

The prefix sum algorithm is an optimization technique commonly used in array problems, which can answer many query questions in O(1) time, such as interval summation, interval maximum/minimum, etc.

The basic idea of ​​the C language prefix sum algorithm is: first preprocess a prefix sum array prefix_sum, where prefix_sum[i] represents the sum of the first i elements of the original array nums; Get the query result.1

the code

#include <stdio.h>

const int MAXN = 100005;
int nums[MAXN], prefix_sum[MAXN];

int main() {
    
    
    int n;
    scanf("%d", &n);

    // 输入原始数组
    for (int i = 1; i <= n; i++) {
    
    
        scanf("%d", &nums[i]);
    }

    // 预处理前缀和数组
    for (int i = 1; i <= n; i++) {
    
    
        prefix_sum[i] = prefix_sum[i-1] + nums[i];
    }

    // 查询区间 [l, r] 的和
    int l, r;
    scanf("%d %d", &l, &r);
    printf("区间 [%d, %d] 的和为:%d\n", l, r, prefix_sum[r] - prefix_sum[l-1]);

    return 0;
}

analyze

In the above code, we first define an integer array nums and prefix_sum with a length of MAXN, where nums stores the original array, and prefix_sum stores the prefix and the array. In the main function, we first read in the original array nums, and calculate the prefix and the array prefix_sum. It should be noted here that for the convenience of calculation, we initialize prefix_sum[0] to 0, so that it is not necessary to judge whether i is equal to 0 to solve prefix_sum[i].

When querying the sum of the interval [l, r], we can directly use prefix_sum[r] - prefix_sum[l-1] to calculate the answer. This formula means that the sum of the interval [l, r] is equal to the sum prefix_sum[r] of the first r elements minus the sum prefix_sum[l-1] of the first l-1 elements. Since prefix_sum[0] has been preprocessed to 0, we can use prefix_sum[l-1] without worrying about out of bounds.

In addition to summation, the prefix sum algorithm can also be used to answer many other types of query questions, such as the maximum/minimum value of the query interval, the number of elements in the statistical interval, and so on. By ingeniously designing prefixes and arrays, we can preprocess all the results that need to be queried in O(n) time, and answer each query in O(1) time, thus greatly improving the efficiency of the program.insert image description here

Guess you like

Origin blog.csdn.net/m0_73879806/article/details/130331208