include<bits/stdc++.h>
using namespace std;
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("D:\\Hello world\\编程输入2\\in.txt", "r", stdin);
#endif
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> nums;
for (int i =0; i < n; i++) {
vector<int> temp;
nums.push_back(temp);
for (int j = 0; j < m; j++) {
int tempNum;
cin >> tempNum;
if (i == 0 && j != 0) {
tempNum = nums[i][j - 1] + tempNum;
}
else if (j == 0 && i != 0) {
tempNum = nums[i - 1][j] + tempNum;
}
else if (j != 0 && i != 0) {
tempNum = nums[i - 1][j] + nums[i][j - 1] + tempNum - nums[i - 1][j - 1];
}
nums[i].push_back(tempNum);
}
}
for (int i = 0; i < q; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 == 1 && y1 == 1) {
cout << nums[x2 - 1][y2 - 1] << endl;
}
else if (x1 == 1 && y1 != 1) {
cout << (nums[x2 - 1][y2 - 1] - nums[x2 - 1][y1 - 2]) << endl;
}
else if (x1 != 1 && y1 == 1) {
cout << (nums[x2 - 1][y2 - 1] - nums[x1 - 2][y2 - 1]) << endl;
}
else {
cout << (nums[x2 - 1][y2 - 1] - nums[x1 - 2][y2 - 1] - nums[x2 - 1][y1 - 2] + nums[x1 - 2][y1 - 2]) << endl;
}
}
}
标签:796,ac,nums,int,矩阵,tempNum,else,&&,y1
From: https://www.cnblogs.com/echoT/p/16621319.html