卡常
2024-12-08 14:51:20
发布于:湖南
44阅读
0回复
0点赞
好好好
#include <iostream>
#include <vector>
#include <algorithm>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
const long long N = 2e5+5;
vector<long long> a;
vector<long long> diff_max;
void preprocess() {
diff_max.push_back(0);
long long size = a.size();
for (long long i = 1; i < size; i++) {
long long s = a[i] - a[i - 1];
diff_max.push_back(max(diff_max.back(), s));
}
}
long long s(long long x, long long y, const vector<long long>& pre_sum) {
if (y == 1) {
return diff_max[x];
}
long long m = 0;
for (long long i = 1; i <= x; i += y) {
long long l = i;
long long r = min(i + y - 1, x);
long long s = pre_sum[r] - pre_sum[l - 1];
m = max(m, s);
}
return m;
}
int main() {
cin.tie(nullptr) -> sync_with_stdio(0);
cout.tie(nullptr) -> sync_with_stdio(0);
long long n;
cin >> n;
a.push_back(0);
vector<long long> pre_sum = {0};
long long sum = 0;
for (long long i = 0; i < n; i++) {
long long num;
cin >> num;
sum += num;
a.push_back(sum);
pre_sum.push_back(sum);
}
preprocess();
long long m;
cin >> m;
while (m--) {
long long x, y;
cin >> x;
cin >> y;
cout << s(x, y, pre_sum) << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个