前缀和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int num[100];
int arr[100][100];
int n,m;

int main(int argc, const char * argv[]) {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> num[i];
if (i > 0) {
num[i] += num[i - 1];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
for (int i = 1; i < m; i++) {
arr[0][i] += arr[0][i - 1];
}
for (int i = 1; i < n; i++) {
arr[i][0] += arr[i - 1][0];
for (int j = 1; j < n; j++) {
arr[i][j] += arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1];
}
}

return 0;
}
script>