#include <bits/stdc++.h> #define S 300005 using namespace std; long long odp[S], wr[S], dp[S], prf[S]; int konu[S]; vector<pair<int, int>> zp[S]; vector<int> kn[S]; vector<int> kis[S]; int gis = 0; int main() { ios_base::sync_with_stdio(0); int n, k, q; cin >> n >> k >> q; for (int i = 1; i <= n; i++) { cin >> wr[i]; prf[i] = prf[i - 1] + wr[i]; } for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; zp[a].push_back({b, i}); } int ff = 0; for (int i = 1; i <= n; i++) { if (!zp[i].size()) continue; int lep = zp[i].size(); for (auto j : zp[i]) { if (konu[j.first] == 0) { gis++; konu[j.first] = gis; } kis[konu[j.first]].push_back(j.second); } for (int j = i - 1; j <= n; j++) { if (i + k - 1 > j) { dp[j] = 0; } else { dp[j] = max(dp[j - 1], dp[j - k] + prf[j] - prf[j - k]); } if (konu[j]) { for (auto k : kis[konu[j]]) { odp[k] = dp[j]; lep--; } konu[j] = 0; if (!lep) break; } // for(auto k:kn[j]){ // odp[k]=dp[j]; // } // kn[j].clear(); } } for (int i = 0; i < q; i++) { cout << odp[i] << '\n'; } // cout << ff << '\n'; }
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <bits/stdc++.h> #define S 300005 using namespace std; long long odp[S], wr[S], dp[S], prf[S]; int konu[S]; vector<pair<int, int>> zp[S]; vector<int> kn[S]; vector<int> kis[S]; int gis = 0; int main() { ios_base::sync_with_stdio(0); int n, k, q; cin >> n >> k >> q; for (int i = 1; i <= n; i++) { cin >> wr[i]; prf[i] = prf[i - 1] + wr[i]; } for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; zp[a].push_back({b, i}); } int ff = 0; for (int i = 1; i <= n; i++) { if (!zp[i].size()) continue; int lep = zp[i].size(); for (auto j : zp[i]) { if (konu[j.first] == 0) { gis++; konu[j.first] = gis; } kis[konu[j.first]].push_back(j.second); } for (int j = i - 1; j <= n; j++) { if (i + k - 1 > j) { dp[j] = 0; } else { dp[j] = max(dp[j - 1], dp[j - k] + prf[j] - prf[j - k]); } if (konu[j]) { for (auto k : kis[konu[j]]) { odp[k] = dp[j]; lep--; } konu[j] = 0; if (!lep) break; } // for(auto k:kn[j]){ // odp[k]=dp[j]; // } // kn[j].clear(); } } for (int i = 0; i < q; i++) { cout << odp[i] << '\n'; } // cout << ff << '\n'; } |