C/C++ prefix sum and difference

Personal homepage:There are still unknowns waiting to be explored_C language difficulties, data structures, algorithms-CSDN Blog

Special column:Algorithm_There are still unknown blogs waiting to be explored-CSDN Blog

Table of contents

I. Introduction

1. What is prefix sum?

2. What is difference?

3. Advantages

1. Simple approach:

2. Use differential arrays

2. Code implementation

1. Give an array to find its difference array

2. Given an array, find its prefix and array


I. Introduction

1. What is prefix sum?

Prefix sum is a kind of preprocessing used to reduce the time complexity of queries. It's like the sum of the first n terms in mathematics.

Given n integers, and then perform m queries, each query finds the sum of the values ​​in an interval.

If written in a violent way, each query needs to loop from the left endpoint of the interval to the right endpoint of the interval to sum up, which will cause a large time complexity.

In this case, the one-dimensional prefix sum of the array can be calculated in advance.

For example, if the array is [1, 1, 1, 1, 1], then the prefix sum array is [1, 2, 3, 4, 5].

2. What is difference?

The difference is more like a prefix and the original array of the array.

For example, if the prefix sum array is [1, 2, 3, 4, 5], then the difference array is [1, 1, 1, 1, 1].

To summarize: prefix sum and difference are reciprocal operations.

3. Advantages

If you are asked to add a number c to all subranges of the array, and sum the changed ranges.

1. Simple approach:

  • Find the interval and add each number c
  • Summarize after traversing once

If there are many intervals to add and sums to be added, the time will be too long.

2. Use differential arrays

  • Find the left endpoint of the interval to be added, and then add c to the left endpoint only (to the right of the left endpoint, its Both prefixes and arrays are added with c)
  • But the range of the addend we want to add is only within a certain interval. In this case, we will perform the -c operation on itsright endpoint +1 , that’s it

2. Code implementation

Note: The array must be stored starting from 1, otherwise special judgments will be made when searching for the prefix and array.

1. Give an array to find its difference array

#include<iostream>
using namespace std;

const int N = 1e5 + 5;
int a[N];//原数组
int b[N];//差分数组
int n;//数的个数

//可以将求一个差分数组,分成求1个元素的差分数组
void insert (int c,int i)
{
	b[i] += c;
	b[i + 1] -= c;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];

	for (int i = 1; i <= n; i++) insert(a[i], i);

	for (int i = 1; i <= n; i++) cout << b[i] << " ";
	return 0;
}

2. Given an array, find its prefix and array

#include<iostream>
using namespace std;

const int N = 1e5 + 5;
int n;//数的个数
int a[N];//原数组
int s[N];//前缀和数组

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];

	for (int i = 1; i <= n; i++) {
		s[i] = s[i - 1] + a[i];//每一个前缀和数组元素一定是它前一个前缀和数组元素加上自己的元素
	}

	for (int i = 1; i <= n; i++) {
		cout << s[i] << " ";
	}
	return 0;
}

thanks for your support!

Guess you like

Origin blog.csdn.net/qq_73435980/article/details/134828335