preface
Blue Bridge Cup Official Website: Blue Bridge Cup - National College Students TMT industry competition
✨ This blog explains the algorithm knowledge involved in the Blue Bridge Cup C/C + + preparation. This blog is the ninth lecture: difference [examples / exercises]
The exercises included in this blog are:
👊 Difference
👊 Difference matrix
For detailed explanation of the difference, see the blog: Difference
For the template of difference, see the blog: Difference algorithm template
The content of the blog is replaced by questions. By explaining the topics, we can help readers quickly understand the content of the algorithm. We need to pay attention to: learning the algorithm can't just go through the brain, but also practice. Please be sure to type and write the relevant code of this blog by yourself!!!
Difference
Title Requirements
Title Description:
Enter a length of n n An integer sequence of n.
Next enter m m m operations, each containing three integers l , r , c l,r,c l. R, C, indicates that the [ l , r ] [l,r] Each number between [l,r] plus c c c.
Please output the sequence after all operations.
Input format:
The first line contains two integers n n n and m m m.
The second line contains n n n integers, representing the sequence of integers.
next m m m lines, each line contains three integers l , r , c l,r,c l. r, c, indicates an operation.
Output format:
A total of one line, including n n n integers representing the final sequence.
Data range:
1
≤
n
,
m
≤
100000
,
1≤n,m≤100000,
1≤n,m≤100000,
1
≤
l
≤
r
≤
n
,
1≤l≤r≤n,
1≤l≤r≤n,
−
1000
≤
c
≤
1000
,
−1000≤c≤1000,
−1000≤c≤1000,
−
1000
≤
−1000≤
− 1000 ≤ value of element in integer sequence
≤
1000
≤1000
≤1000
Input sample:
6 3
1 2 2 1 2 1
1 3 1
3 5 1
1 6 1
Output example:
3 4 5 3 4 2
Train of thought analysis
The template problem of one-dimensional difference can be understood as the inverse operation of prefix sum. The construction method of difference array is as follows:
s Array is a differential array,a Array is the original array s[0] = 0, a[0] = 0; s[1] = a[1] - a[0]; s[2] = a[2] - a[1]; ... s[n] = a[n] - a[n - 1];
order a a a array in [ l , r ] [l,r] Add a number to the interval of [l,r] c c c you can use differential thinking:
s[l] += c; s[r + 1] -= c;
Then prefix and sum the difference group.
code
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100010; int a[N], s[N]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]); // Processing differential arrays for (int i = 1; i <= n; i ++ ) s[i] = a[i] - a[i - 1]; while (m -- ) { int l, r, c; scanf("%d%d%d", &l, &r, &c); s[l] += c, s[r + 1] -= c; } for (int i = 1; i <= n; i ++ ) { s[i] += s[i - 1]; printf("%d ", s[i]); } return 0; }
Difference matrix
Title Requirements
Title Description:
Enter a n n n line m m m column integer matrix, and then enter q q q operations, each containing five integers x 1 , y 1 , x 2 , y 2 , c x_1,y_1,x_2,y_2,c x1, y1, x2, y2, c, where ( x 1 , y 1 ) (x_1,y_1) (x1, y1) and ( x 2 , y 2 ) (x_2,y_2) (x2, y2) represents the coordinates of the upper left corner and the lower right corner of a sub matrix.
Each operation adds the value of each element in the selected sub matrix c c c.
Please output the matrix after all operations.
Input format:
The first line contains integers n , m , q n,m,q n,m,q.
next n n n rows, each containing m m m integers, representing the integer matrix.
next q q q rows, each containing 5 5 5 integers x 1 , y 1 , x 2 , y 2 , c x_1,y_1,x_2,y_2,c x1, y1, x2, y2, c represents an operation.
Output format:
common n n n rows, each row m m m integers, representing the final matrix after all operations are completed.
Data range:
1
≤
n
,
m
≤
1000
,
1≤n,m≤1000,
1≤n,m≤1000,
1
≤
q
≤
100000
,
1≤q≤100000,
1≤q≤100000,
1
≤
x
1
≤
x
2
≤
n
,
1≤x_1≤x_2≤n,
1≤x1≤x2≤n,
1
≤
y
1
≤
y
2
≤
m
,
1≤y_1≤y_2≤m,
1≤y1≤y2≤m,
−
1000
≤
c
≤
1000
,
−1000≤c≤1000,
−1000≤c≤1000,
−
1000
≤
−1000≤
− 1000 ≤ value of elements in the matrix
≤
1000
≤1000
≤1000
Input sample:
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
Output example:
2 3 4 1
4 3 4 1
2 2 2 2
Train of thought analysis
For the two-dimensional difference array, you can understand it this way: if a point on the two-dimensional matrix of prefix sum affects the number from the upper left corner of the matrix to that point, then the difference array matrix affects the number from this point to the lower right corner of the matrix a a a array (2D) ( x 1 , y 1 ) (x_1,y_1) (x1, y1) [top left] and ( x 2 , y 2 ) (x_2,y_2) (x2, y2) add a number to the matrix of [lower right corner] c c c available differential:
s[x1][y1] += c; s[x2 + 1][y1] -= c; s[x1][y2 + 1] -= c; s[x2 + 1][y2 + 1] += c;
Then calculate the prefix and array again to get the result. Note that our prefix and template should be:
s[i, j] = s[i, j - 1] + s[i - 1, j] - s[i - 1, j - 1] + a[i, j];
But at this time s [ i , j ] s[i, j] s[i,j] is actually a value, that is a [ i , j ] a[i,j] a[i,j], so the difference group is changed into prefix and array as follows:
s[i][j] += s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];
code
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 1010; int a[N][N], s[N][N]; void insert(int x1, int y1, int x2, int y2, int c) { s[x1][y1] += c; s[x2 + 1][y1] -= c; s[x1][y2 + 1] -= c; s[x2 + 1][y2 + 1] += c; } int main() { int n, m, q; 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]); // Processing differential arrays 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; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c); insert(x1, y1, x2, y2, c); } // Calculating prefixes and arrays for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= m; j ++ ) s[i][j] += s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1]; for (int i = 1; i <= n; i ++ ) { for (int j = 1; j <= m; j ++ ) printf("%d ", s[i][j]); puts(""); } return 0; }