Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n, m;
- int a[102][102];
- int sum[102][102]; // sum[x][y] 表示第x行y列左上角的和
- int x_1, y_1, x_2, y_2;
- int main() {
- cin >> n >> m;
- for (int i = 1; i <= n; i ++) {
- for (int j = 1; j <= m; j ++) {
- cin >> a[i][j];
- }
- }
- // 预处理二维前缀和
- for (int i = 1; i <= n; i ++) {
- int tmp = 0;
- for (int j = 1; j <= n; j ++) {
- tmp += a[i][j];
- sum[i][j] = sum[i - 1][j] + tmp;
- }
- }
- cin >> x_1 >> y_1 >> x_2 >> y_2;
- int s1 = sum[x_2][y_2]; // 绿色矩形
- int s2 = sum[x_1 - 1][y_2]; // 橙色矩形1
- int s3 = sum[x_2][y_1 - 1]; // 橙色矩形2
- int s4 = sum[x_1 - 1][y_1 - 1]; // 青色矩形
- int s = s1 - (s2 + s3) + s4; //红色目标子矩阵
- cout << s;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement