//洛谷p8218求区间和
#include<iostream>
using namespace std;
const int N = 100010;
int n;
int m;
int a[N], s[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
cin >> m;
while (m--) {
int l, r;
cin >> l >> r;
cout << s[r] - s[l - 1]<<endl;
}
}
//洛谷p1719最大加权矩形(二维)
#include<iostream>
using namespace std;
const int N = 10010;
int a[N][N];
int s[N][N];
int main() {
int n;
cin >> n;
// 读取矩阵并计算前缀和
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
int tmp = a[1][1]; // 将 tmp 初始化为矩阵的左上角元素
// 使用四重循环找到最大子矩阵和
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = i; k <= n; k++) {
for (int l = j; l <= n; l++) {
int cur = s[k][l] - s[k][j - 1] - s[i - 1][l] + s[i - 1][j - 1];
if (cur > tmp) {
tmp = cur;
}
}
}
}
}
cout << tmp << endl; // 输出最大子矩阵和
return 0;
}
标签:tmp,洛谷,cout,int,矩阵,cin,离散
From: https://www.cnblogs.com/windzhao6/p/18315827