Detailed explanation of difference method

Foreword
The difference algorithm is suitable for some problems that require addition, subtraction, query and update operations on arrays and sequences. It can improve computing efficiency and reduce storage space requirements. Today I will teach you how to use the difference method. I will use examples to teach you how to use the difference method to enhance your understanding. Without further ado, let’s get started!
Insert image description here

One-dimensional difference

First we need to create an array arr to represent the difference array, and then create an arrsum array to represent the prefix sum of arr.

即arr[i] = arrsum[i] - arrsum[i-1]
  arrsum[i] = arr[i] + arr[i-1] + ... + arr[1]

Suppose we operate on the arr array and each element from subscript l to subscript r is +1. As shown in the figure
Insert image description here
Here it will be very troublesome if we add 1 to each element of l-r, but it will be very simple if we perform the following operation on arrsum. We only need to let arrsum[ l ] + 1 arrsum[ r + 1 ] - 1 is enough. As shown in the figure
Insert image description here
Because arrsum is the prefix sum, as long as the l position is +1, the r+1 position is -1, and the +1 operation is performed in the group domain of l-r.
But the array we are looking for is not an arrsum array, so can we find a way to turn arrsum into arr? For example, there is 1 2 3 4 5 in arr[5]. Now let the initial value of arrsum be set to 0, -1, -2, -3, --4. Then when calculating the prefix sum, use this calculation formula arrsum[i] = arrsum[i] + arrsum[i -1] + arr[i] to turn arrsum into what the arr array looks like after the operation, then we use this question Let me show you how to use the difference method with an example.
Given the array arr, there are n elements in total. Perform q operations on it. Each operation will +1 the elements in the [l, r] interval. Please output the arr after the operation. array.
Input sample
The first line is n and q
The second line is the elements of the arr array
In the future, q acts as l and r
10 2
1 2 3 4 5 6 7 8 9 10
1 5
9 10
Output sample
2 3 4 5 6 6 7 8 10 11
Solution:

#include<stdio.h>
int main()
{
    
    
	int arr[100] = {
    
     0 };
	int arrsum[100] = {
    
     0 };//前缀和
	int n, q;
	scanf("%d %d", &n, &q);
	for (int i = 1; i <= n; i++)
	{
    
    
		scanf("%d", &arr[i]);
	}
	for (int i = 1; i <= n; i++)//使求出的前缀和数组刚好为操作后的arr数组
	{
    
    
		arrsum[i] += arr[i];
		arrsum[i + 1] -= arr[i];
	}
	while (q--)//q次操作
	{
    
    
		int l, r;
		scanf("%d %d", &l, &r);
		arrsum[l] += 1;
		arrsum[r + 1] -= 1;
	}
	for (int i = 2; i <= n; i++)//求前缀和(其实是造作后的arr数组)
	{
    
    
		arrsum[i] += arrsum[i - 1];
	}
	for (int i = 1; i <= n; i++)//打印出来
	{
    
    
		printf("%d ", arrsum[i]);
	}
	return 0;
}

This will solve the problem.

2D difference

Link here click me click me!

end

I believe that friends who have seen this must have mastered the essence of the difference method. If you think the blogger’s talk is good, please give the blogger a follow, like, and favorite~. That’s it for this issue. See you in the next issue. !

Guess you like

Origin blog.csdn.net/Tokai___Teio/article/details/135014484