#include <cstdio> #include <cstdlib> #include <cassert> #include <algorithm> #include <vector> // Brute-force solution struct query { int idx; int begin; int end; }; int main() { int n, k, q; assert(3 == scanf("%d %d %d", &n, &k, &q)); std::vector<long long int> soldiers; soldiers.reserve(n); std::vector<query> queries; queries.reserve(q); for (int i = 0; i < n; i++) { int x; assert(1 == scanf("%d", &x)); soldiers.push_back(x); } for (int i = 0; i < q; i++) { int a, b; assert(2 == scanf("%d %d", &a, &b)); queries.push_back({i, a - 1, b}); } std::vector<long long int> d; std::sort(queries.begin(), queries.end(), [] (const query& q1, const query& q2) { if (q1.begin != q2.begin) { return q1.begin < q2.begin; } return q1.end < q2.end; }); int last_begin = -1; long long int rolling_sum = 0; std::vector<long long int> answers; answers.resize(queries.size()); for (const query& q : queries) { if (last_begin != q.begin) { last_begin = q.begin; d.clear(); d.push_back(0); rolling_sum = 0; // printf("CLEARING\n"); } while (q.begin + (int)d.size() <= q.end) { const int i = q.begin + (int)d.size(); if ((int)d.size() > k) { rolling_sum -= soldiers[i - k - 1]; // printf("REM SOLDIER %lld\n", soldiers[i - k - 1]); } rolling_sum += soldiers[i - 1]; // printf("ADD SOLDIER %lld\n", soldiers[i - 1]); long long int best = d.back(); if ((int)d.size() >= k) { best = std::max(best, rolling_sum + d[d.size() - k]); } // printf("ADDING %lld\n", best); d.push_back(best); } answers[q.idx] = d.back(); } for (long long int a : answers) { printf("%lld\n", a); } 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 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 78 79 80 81 82 83 84 85 86 | #include <cstdio> #include <cstdlib> #include <cassert> #include <algorithm> #include <vector> // Brute-force solution struct query { int idx; int begin; int end; }; int main() { int n, k, q; assert(3 == scanf("%d %d %d", &n, &k, &q)); std::vector<long long int> soldiers; soldiers.reserve(n); std::vector<query> queries; queries.reserve(q); for (int i = 0; i < n; i++) { int x; assert(1 == scanf("%d", &x)); soldiers.push_back(x); } for (int i = 0; i < q; i++) { int a, b; assert(2 == scanf("%d %d", &a, &b)); queries.push_back({i, a - 1, b}); } std::vector<long long int> d; std::sort(queries.begin(), queries.end(), [] (const query& q1, const query& q2) { if (q1.begin != q2.begin) { return q1.begin < q2.begin; } return q1.end < q2.end; }); int last_begin = -1; long long int rolling_sum = 0; std::vector<long long int> answers; answers.resize(queries.size()); for (const query& q : queries) { if (last_begin != q.begin) { last_begin = q.begin; d.clear(); d.push_back(0); rolling_sum = 0; // printf("CLEARING\n"); } while (q.begin + (int)d.size() <= q.end) { const int i = q.begin + (int)d.size(); if ((int)d.size() > k) { rolling_sum -= soldiers[i - k - 1]; // printf("REM SOLDIER %lld\n", soldiers[i - k - 1]); } rolling_sum += soldiers[i - 1]; // printf("ADD SOLDIER %lld\n", soldiers[i - 1]); long long int best = d.back(); if ((int)d.size() >= k) { best = std::max(best, rolling_sum + d[d.size() - k]); } // printf("ADDING %lld\n", best); d.push_back(best); } answers[q.idx] = d.back(); } for (long long int a : answers) { printf("%lld\n", a); } return 0; } |