#define _CRT_SECURE_NO_WARNINGS 1 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #include <iostream> #include <string> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> #include <cstring> using namespace std; long long wojaki[312345]; long long prefix[312345]; long long wyniki[312345]; long long dyn[312345]; struct Query { int l, r; int p; bool operator<(const Query& o) const { return (l < o.l || (l == o.l && r < o.r)); } }; int main() { int n, k, q; scanf("%d %d %d", &n, &k, &q); for (int i = 1; i <= n; ++i) { long long a = 0; scanf("%lld", &a); wojaki[i] = a; prefix[i] = prefix[i - 1] + a; } vector<Query> zap(q); for (int i = 0; i < q; ++i) { int l, r; scanf("%d %d", &l, &r); zap[i].l = l; zap[i].r = r; zap[i].p = i; } sort(zap.begin(), zap.end()); int lastL = -1; int j = 0; for (int i = 0; i < q; ++i) { int l = zap[i].l; int r = zap[i].r; int p = zap[i].p; if (l != lastL) { lastL = l; memset(dyn, 0, sizeof(dyn)); j = l + k - 1; } for (;j <= r; ++j) { long long oodzial = prefix[j] - prefix[j - k]; dyn[j] = max(dyn[j - 1], dyn[j - k] + oodzial); } wyniki[p] = dyn[r]; } for (int i = 0; i < q; ++i) { printf("%lld\n", wyniki[i]); } 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 87 88 | #define _CRT_SECURE_NO_WARNINGS 1 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #include <iostream> #include <string> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> #include <cstring> using namespace std; long long wojaki[312345]; long long prefix[312345]; long long wyniki[312345]; long long dyn[312345]; struct Query { int l, r; int p; bool operator<(const Query& o) const { return (l < o.l || (l == o.l && r < o.r)); } }; int main() { int n, k, q; scanf("%d %d %d", &n, &k, &q); for (int i = 1; i <= n; ++i) { long long a = 0; scanf("%lld", &a); wojaki[i] = a; prefix[i] = prefix[i - 1] + a; } vector<Query> zap(q); for (int i = 0; i < q; ++i) { int l, r; scanf("%d %d", &l, &r); zap[i].l = l; zap[i].r = r; zap[i].p = i; } sort(zap.begin(), zap.end()); int lastL = -1; int j = 0; for (int i = 0; i < q; ++i) { int l = zap[i].l; int r = zap[i].r; int p = zap[i].p; if (l != lastL) { lastL = l; memset(dyn, 0, sizeof(dyn)); j = l + k - 1; } for (;j <= r; ++j) { long long oodzial = prefix[j] - prefix[j - k]; dyn[j] = max(dyn[j - 1], dyn[j - k] + oodzial); } wyniki[p] = dyn[r]; } for (int i = 0; i < q; ++i) { printf("%lld\n", wyniki[i]); } return 0; } |