Acwing_795前缀和 【一维前缀和】
题目:
代码:
cpp
#include <bits/stdc++.h>
#define int long long
#define INF 0X3f3f3f3f
#define endl '\n'
using namespace std;
const int N = 100010;
int arr[N];
int n,m;
int l,r;
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
int sum[N];
for(int i = 1; i <= n; i++){
cin>>arr[i];
sum[i]= sum[i-1] + arr[i];
}
while(m--){
cin>>l>>r;
cout<<sum[r]- sum[l-1]<<endl;
}
return 0;
}
Acwing_795前缀和 【一维前缀和】
cpp
#include <bits/stdc++.h>
#define int long long
#define INF 0X3f3f3f3f
#define endl '\n'
using namespace std;
const int N = 1010;
int arr[N][N];
int n, m, q;
signed main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int sum[N][N];
cin>>n>>m>>q;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin>>arr[i][j];
sum[i][j] = sum[i-1][j] +sum[i][j-1]-sum[i-1][j-1]+arr[i][j];
}
}
while(q--){
int x1, y1, x2, y2;
cin>>x1>>y1>>x2>>y2;
cout<<sum[x2][y2] - sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]<<endl;
}
return 0;
}