1 前缀和
题目链接
https://www.acwing.com/problem/content/description/797/
题目大意
输入一个长度为 n 的整数序列。
接下来再输入 m 个询问,每个询问输入一对 l,r。
对于每个询问,输出原序列中从第 l 个数到第 r 个数的和。
解题思路
一维前缀和序列中,求第l个数~第r个数的和 ,等于 a[r] - a[l - 1]
#include<stdio.h>
const int N = 100005;
long long a[N] = {0};
int main(){
int n,q;
scanf("%d %d",&n,&q);
for(int i = 1;i <= n;i++){
scanf("%lld",&a[i]);
a[i] += a[i - 1];
}
int l,r;
while(q--){
scanf("%d %d",&l,&r);
printf("%lld\n",a[r] -a[l - 1]);
}
return 0;
}
2 子矩阵的和(二维前缀和)
题目链接
https://www.acwing.com/problem/content/798/
题目大意
输入一个n行m列的整数矩阵,再输入q个询问,每个询问包含四个整数x1, y1, x2, y2,表示一个子矩阵的左上角坐标和右下角坐标。
对于每个询问输出子矩阵中所有数的和。
解题思路
二维前缀和
- 构建:a[i][j] += a[i][j - 1] + a[i - 1][j] - a[i - 1][j - 1];
- 求和: (x1,y1) ~ (x2,y2)的和是:(1≤x1≤x2,1≤y1≤y2)
a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1]
记忆 2 1 1 1 1 2
原理参考链接
https://www.acwing.com/solution/content/3797/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1005][1005];
int main(){
int n,m,q;
cin >> n>> m >> q;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
scanf("%lld",&a[i][j]);
a[i][j] += a[i][j - 1] + a[i - 1][j] - a[i - 1][j - 1];
}
}
//a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1]
//记忆 2 1 1 1 1 2
while(q--){
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1] << endl;
}
return 0;
}
3 差分
题目链接
https://www.acwing.com/problem/content/799/
题目大意
输入一个长度为n的整数序列。
接下来输入m个操作,每个操作包含三个整数l, r, c,表示将序列中[l, r]之间的每个数加上c。
请你输出进行完所有操作后的序列。
1≤n,m≤100000,
1≤l≤r≤n,
−1000≤c≤1000,
−1000≤整数序列中元素的值≤1000
解题思路
例如:
1,2,3,4,5 ,6
将第3~第5的数+2,变成:
1,2,5,6,7,6
(1)为了能将序列在线性时间内展开,我们可以从第一位开始,记录相邻两位的前后差值,这样一来,根据差值序列和第一位数字,我们就可以O(n)的时间,计算扩展得到整个序列
(2)1,2,3,4,5,6的差值序列:a[i] - a[i-1],序列下标从1开始,a[0]=0
1,1,1,1,1,1
1,2,5,6,7,6的差值序列:
1,1,3,1,1,-1
可以发现,操作”将第3~第5的数+2“,对差值序列的影响就是:
- b[ l ] += x
- b[r + 1] -= x
从而,我们就可以以O(q)的复杂度,处理q次操作,以O(n)的复杂度扩展整个序列
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
ll a[N],b[N];
int main(){
int n,q;
cin >> n >> q;
for(int i = 1;i <= n;i++){
scanf("%lld",&a[i]);
}
for(int i = 1;i <= n;i++){
b[i] = a[i] - a[i - 1];
}
int l,r,x;
while(q--){
scanf("%d %d %d",&l,&r,&x);
b[l] += x;
b[r + 1] -= x;
}
for(int i = 1;i <= n;i++){
a[i] = a[i - 1] + b[i];
cout << a[i] << " ";
}
return 0;
}
4 差分矩阵
题目链接
https://www.acwing.com/problem/content/800/
题目大意
输入一个n行m列的整数矩阵,再输入q个操作,每个操作包含五个整数x1, y1, x2, y2, c,其中(x1, y1)和(x2, y2)表示一个子矩阵的左上角坐标和右下角坐标。
每个操作都要将选中的子矩阵中的每个元素的值加上c。
请你将进行完所有操作后的矩阵输出。
1≤n,m≤1000,
1≤q≤100000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤c≤1000,
−1000≤矩阵内元素的值≤1000
解题思路
(1)构建二维差分矩阵(c = a[ i ][ j ])、处理操作(c = x)
b[x1][y1] += c;
b[x2 + 1][y1] -= c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y2 + 1] += c;
(2)还原矩阵
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];
}
}
推导请见
https://www.acwing.com/solution/content/2137/
#include<iostream>
#include<stdio.h>
using namespace std;
const int N = 1e3+5;
int a[N][N];
int 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(){
int n,m,q;
cin >> 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;
scanf("%d %d %d %d %d",&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++){
if(j == m)
printf("%d\n",b[i][j]);
else
printf("%d ",b[i][j]);
}
}
return 0;
}