# 题意
二维数组
m个操作,每个操作包含x1,y1,x2,y2,c 使得在矩阵中以x x1,y1,x2,y2中的所有值都加c
# 题解
给以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵中的所有元素加上c:
S[x1, y1] += c,
S[x2 + 1, y1] -= c,
S[x1, y2 + 1] -= c,
S[x2 + 1, y2 + 1] += c
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N=1e3+10; 5 int a[N][N],b[N][N]; 6 int n,m,q; 7 inline void insert(int x1,int y1,int x2,int y2,int c){ 8 b[x1][y1]+= c; 9 b[x2+1][y1]-=c; 10 b[x1][y2+1]-=c; 11 b[x2+1][y2+1]+=c; 12 } 13 int main(){ 14 ios::sync_with_stdio(0); 15 cin.tie(0); 16 cout.tie(0); 17 cin>>n>>m>>q; 18 b[0][0]=0; 19 for(int i=1;i<=n;i++) 20 for(int j=1;j<=m;j++) 21 cin>>a[i][j]; 22 for(int i=1;i<=n;i++) 23 for(int j=1;j<=m;j++) 24 insert(i,j,i,j,a[i][j]); 25 while(q--){ 26 int x1,y1,x2,y2,c; 27 cin>>x1>>y1>>x2>>y2>>c; 28 insert(x1,y1,x2,y2,c); 29 } 30 31 for(int i=1;i<=n;i++) { 32 for (int j = 1; j <= m; j++) { 33 b[i][j] = b[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1]; 34 cout << b[i][j] << ‘ ‘; 35 } 36 cout<<endl; 37 } 38 }
原文地址:https://www.cnblogs.com/hhyx/p/12411916.html
时间: 2024-10-01 00:06:40