【Acwing】795. Prefix and

1. Prefix and

In fact, it can be understood as the sum of the first n items of a mathematical sequence (the prefix sum for a one-dimensional array).

We define the prefix of an array a and an array s, s[i] = a[1]+a[2]+…+a[i].

2. Two-dimensional prefix sum

Similar to the one-dimensional prefix sum, let s[i][j] denote the sum of all a[i'][j']. (1≤i'≤i,1≤j'≤j)

One thing is like the "area of ​​a rectangle", which adds up the values ​​of an entire area.

3. The use of prefix sum

Generally used to find interval sums.

For the one-dimensional situation, now I give a sequence a, asking you to answer m queries, and each query is the sum of subscripts j to k. The naive approach is obviously to perform an addition operation for each query, and then output the result. This is correct, but when m is too large, it will cause too many calculations and may time out.

The reason for the timeout is clear at a glance, and the calculation is repeated. So how should we improve this method? Imagine if we first calculate the prefix sum of each position in advance, and then use s[k]-s[j], isn't the result the answer to our inquiry this time? This will greatly reduce the amount of calculation.

The same is true for two-dimensional interval sums.
Insert picture description hereLet's study it with the help of this picture. Assume that in this matrix (two-dimensional array), what we are asking for is the red area in the figure above. Now that we have preprocessed the prefix sum of all points, given two points (x1, y1), (x2, y2), we require the value of a sub-matrix with the line connecting these two points as the diagonal Sum. I will add this one by one to the violence method, I won't say more, anyway, sooner or later, you will have TLE, we will focus on the quick method of using the prefix and.

First, we can find s[x2][y2], which represents the prefix sum of the entire large rectangle, and then we subtract the prefix sum of the extra block on the left and the prefix sum of the extra block on the bottom. This is the final answer Up?

Not! This is not the final answer. It can be found that when we cut off the two extra areas, the small piece below was reduced twice, but it is obviously unreasonable to reduce it twice, and we should add it back. .

So for one query, the answer ans should be equal to s[x2][y2]-s[x2][y1-1]-s[x1-1][y2]+s[x1-1][y1-1].

This two-dimensional prefix sum is also called a difference sequence.

topic

输入一个长度为 n的整数序列。接下来再输入 m个询问,每个询问输入一对 l,r。对于每个询问,输出原序列中从第 l个数到第 r个数的和。

输入格式
第一行包含两个整数 n和 m。
第二行包含 n个整数,表示整数数列。
接下来 m行,每行包含两个整数 l 和 r,表示一个询问的区间范围。

输出格式
共 m行,每行输出一个询问的结果。

数据范围
1≤l≤r≤n,1≤n,m≤100000,−1000≤数列中元素的值≤1000

输入样例:
5 3
2 1 3 6 4
1 2
1 3
2 4

输出样例:
3
6
10

Calculation template

s[0]=0; a is assigned from a[1]

S[i] = a[1] + a[2] + ... a[i]
a[l] + ... + a[r] = S[r] - S[l - 1]
#include<iostream>
using namespace std;

const int N=100005;

int a[N],S[N];

int main(){
    
    
    int n,m,l,r;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
    
    
        cin>>a[i];
    }
    
    for(int i=1;i<=n;i++){
    
    
        S[i]=S[i-1]+a[i];
    }
    
    while(m--){
    
    
        cin>>l>>r;
        cout<<S[r]-S[l-1]<<endl;;
    }
    return 0;
}

For details, please see: https://www.acwing.com/solution/content/27238/

Guess you like

Origin blog.csdn.net/qq_43531919/article/details/115260683