Use mathematical ideas to explain the inverse operation of prefix sum-difference, difference matrix

Use mathematics to explain the relationship between difference and prefix sum

Before learning the difference, you need to review the concept and definition of the prefix sum: click here to see the prefix and concept

We know the implementation of the prefix and code:s[ i ] = s[ i - 1] + a[ i ];(Equation 1)
That is, the array S stores the sum of the first i items of the array a; I believe this is easy to understand, but when it comes to the definition of difference, it is easy to confuse;
why is the difference in the inverse operation of the prefix sum?
Let's look at the definition of difference: difference is the difference between two adjacent numbers .
We can write directly according to its definition literally:s[ i ] = a[i] - a[i-1];(Equation 2)
That is, use the array S to record the difference between each adjacent number of the array a.

We observe these two formulas and reverse the array s and the array a in the prefix sum to obtain formula 2.
That is, in mathematics, let s = a, a = s, then a[i] = a[i-1] + s[i], put a[i-1] on the other side to become
s[1] = a[i]-a[i-1]. It can be seen that the prefix sum and difference are a kind of inverse operation.

We construct a set bn to satisfy:
av = b1 + b2 + b3 +… + bv;
construct bn so that the array n is called the prefix sum of the b array, and the b array is the difference of the a array

We can use difference to better solve the problem of interval sum:
given an array a, if we want to add a number c in one of the intervals [l, r], we may use for(int i = l ;i <= r; i++) a[i] += c;this time complexity for the first time It is O(n). If the interval [l,r] is very large, it is easy to process overtime. Therefore, we use differential operations s[l] += c; s[r + 1] -= c;to ensure that only c is added between the interval l and r.

Finally, we also need to restore the array a after adding c, directly "reverse", let's look atEquation 2, The way to get a[i], namelya[i] = s[i] + a[i -1]


Example: Difference
Enter a sequence of integers of length n.

Next, enter m operations, each operation contains three integers l, r, c, which means adding c to each number between [l, r] in the sequence.

Please output the sequence after all operations have been performed.

Input format The
first line contains two integers n and m.

The second line contains n integers, representing a sequence of integers.

The next m lines, each line contains three integers l, r, c, representing an operation.

The output format is
one line, containing n integers, representing the final sequence.

Data range
1≤n, m≤100000,
1≤l≤r≤n,
-1000≤c≤1000,
-1000≤The value of the element in the integer sequence ≤1000
Input example:

6 3
1 2 2 1 2 1
1 3 1
3 5 1
1 6 1

Sample output:

3 4 5 3 4 2

C++ implementation:

#include<iostream>

using namespace std;

const int N = 100010; 

int n,m;
int a[N],b[N];

void insert(int l, int r,int c)
{
    
    
	b[l] += c;
	b[r + 1] -=c;
}

int main()
{
    
    
	
	scanf("%d%d",&n,&m);
	for(int i = 1; i <= n;i ++ ) scanf("%d",&a[i]);
	
	for(int i = 1; i <= n;i ++ ) insert(i, i ,a[i]);  
//相当于	for(int i = 1; i <= n; i ++) b[i] = a[i] - a[i-1];
	
	while(m -- )
	{
    
    
		int l, r, c;
		scanf("%d%d%d",&l,&r,&c);
		insert(l,r,c);
	}	
	//求原数组差分后的情况: 
	for(int i = 1; i <= n; i ++) b[i] += b[i - 1];  //通过b[i] = b[i] + b[i-1]分解出原数组的a[]的情况 
	for(int i = 1; i <= n;i ++ ) printf("%d ", b[i]);
	
//或者  for(int i = 1; i <= n ; i ++) a[i] = b[i]+ a[i-1];
//   	for(int i = 1; i <= n ; i++) printf("%d ",a[i] );
	return 0;
 } 

Differential matrix
input an integer matrix with n rows and m columns, and then input q operations, each operation contains five integers x1, y1, x2, y2, c, where (x1, y1) and (x2, y2) represent a sub The coordinates of the upper left corner and the lower right corner of the matrix.

Each operation must add c to the value of each element in the selected sub-matrix.

Please output the matrix after all operations are completed.

Input format The
first line contains integers n, m, q.

The next n rows, each row contains m integers, representing a matrix of integers.

The next q lines, each line contains 5 integers x1, y1, x2, y2, c, representing an operation.

The output format
has n rows and m integers in each row, representing the final matrix after all operations are completed.

Data range
1≤n, m≤1000,
1≤q≤100000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤c≤1000,
−1000≤The value of the element in the matrix≤1000
input Example:

3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
1 1 2 2 1
1 3 2 3 2
3 1 3 4 1

Sample output:

2 3 4 1
4 3 4 1
2 2 2 2
#include<iostream>

using namespace std;

const int N = 1010;

int n, m, q;
int a[N][N], b[N][N]; 

void insert(int x1, int y1, int x2, int y2, int c)
{
    
    
	b[x1][y1] += c;
	b[x2 + 1][y1] -= c;
	b[x1][y2 + 1] -= c;
	b[x2 + 1][y2 + 1] += c;
}

int main()
{
    
    
	scanf("%d%d%d",&n, &m, &q);
	
	for(int i = 1; i <= n; i ++ )
	{
    
    
		for(int j = 1; j <= m; j ++)
			scanf("%d",&a[i][j]);
	}
	for(int i= 1; i <= n ;i ++)
	{
    
    
		for(int j = 1; j <= m ; j ++)
		{
    
    
			insert(i, j, i, j, a[i][j]);
		}
	}
	
	while(q--)
	{
    
    
		int x1, y1, x2, y2, c;
		cin >> x1 >> y1 >> x2 >> y2 >> c;
		insert(x1, y1, x2, y2, c);
	}
	
	for(int i =1; i <= n; i ++)
	{
    
    
		for(int j = 1; j <= m; j ++)
			b[i][j] += b[i - 1][j] + b[i][j -1] - b[i -1][j - 1];
	}
	
	for(int i = 1; i <= n; i ++)
	{
    
    
		for(int j = 1; j <= m; j ++)
			printf("%d ",b[i][j]);
		puts(" ");
	}
	
	return 0;
 }

Guess you like

Origin blog.csdn.net/diviner_s/article/details/107350117