#include <bits/stdc++.h>
using namespace std;
long long a[300001], pref[300001];
int x, y, n, k, q;
unordered_map <long long, long long> m;
long long f(int tx)
{
if(m[tx*1000000+y]) return m[tx*1000000+y]-1;
long long maks = 0;
for(int i = tx; i+k-1 <= y; ++i)
{
maks = max(maks, pref[i+k-1]-pref[i-1]+f(i+k));
}
//cout << tx << " " << maks << "\n";
m[tx*1000000+y] = maks+1;
return maks;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> q;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i) pref[i] = pref[i-1]+a[i];
for(int e = 0; e < q; ++e)
{
cin >> x >> y;
long long wyn = f(x);
cout << wyn << "\n";
}
return 0;
}
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 33 34 35 36 37 38 39 40 41 42 43 | #include <bits/stdc++.h> using namespace std; long long a[300001], pref[300001]; int x, y, n, k, q; unordered_map <long long, long long> m; long long f(int tx) { if(m[tx*1000000+y]) return m[tx*1000000+y]-1; long long maks = 0; for(int i = tx; i+k-1 <= y; ++i) { maks = max(maks, pref[i+k-1]-pref[i-1]+f(i+k)); } //cout << tx << " " << maks << "\n"; m[tx*1000000+y] = maks+1; return maks; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> q; for(int i = 1; i <= n; ++i) cin >> a[i]; for(int i = 1; i <= n; ++i) pref[i] = pref[i-1]+a[i]; for(int e = 0; e < q; ++e) { cin >> x >> y; long long wyn = f(x); cout << wyn << "\n"; } return 0; } |
English