#include <algorithm> #include <set> #include <map> #include <vector> #include <unordered_map> #include <cstdio> #define ll long long #define PII pair<int, int> using namespace std; int n,k,qs; ll a[300000]; vector<PII> q; ll res[300000]; ll s[300000]; ll v[300000]; vector<int> clusters; unordered_map<int, ll*> dp; set<int> availableDp; void computeSum() { ll sum = 0; for(int i=0;i<k;i++) { s[i] = 0; sum += a[i]; } s[k-1] = sum; for(int i=k;i<n;i++) { sum += a[i]-a[i-k]; s[i] = sum; } } void selectClusters(int lastFrom) { set<int> selected; int chunks = 27; for(int i=0;i<chunks;i++) { selected.insert((i*lastFrom)/chunks); } int p = 0; for(auto sel : selected) { clusters.push_back(sel); } } void computeDp(ll* d, int start, int end) { int len = end - start + 1; d[k - 1] = max(0LL, s[start + k - 1]); for(int i=k;i<len;i++) { ll best1 = 0; for(int j=0;j<k;j++) { if(i - j - k >= 0) { best1 = max(best1, d[i - j - k]); } } best1 += max(0LL, s[start + i]); ll best2 = best1; for(int j=0;j<k-1;j++) { best2 = max(best2, d[i - j - 1]); } d[i] = max(best1, best2); } } void startDp() { int lastFrom = -1; int lastTo = -1; for(auto qe : q) { lastFrom = max(lastFrom, qe.first); lastTo = max(lastTo, qe.second); } selectClusters(lastFrom); for(auto lastLeft : clusters) { if(lastLeft + 2*k <= lastTo) { for(int i=0;i<k;i++) { int start = lastLeft + i; if(dp.find(start) == dp.end()) { ll* d = new ll[lastTo - start + 1]; fill(d, d+k-1, 0LL); computeDp(d, start, lastTo); dp[start] = d; } } } } } void computeAvailableDp() { vector<int> keys; for(auto it=dp.begin();it!=dp.end();it++) { keys.push_back(it->first); } sort(keys.begin(), keys.end(), greater<int>()); int prev = -1; int consecutive = 0; for(auto cur: keys) { if(cur == prev - 1) { consecutive++; if(consecutive >= k-1) { availableDp.insert(cur); } } else { consecutive = 0; } prev = cur; } } ll solveRange(int from, int to) { if (to - from + 1 < k) { return 0; } auto f = availableDp.lower_bound(from); bool useDp = false; int dpStart = -1; int last = 0; if(f != availableDp.end()) { useDp = (*f) < (to - k); dpStart = *f; } if(useDp) { last = (*f) + k - 1; } else { last = to; } computeDp(v, from, last); if(useDp) { ll res = 0; for(int i=0;i<k;i++) { ll* d = dp[dpStart+i]; ll d1 = d[to - (dpStart+i)]; int ix = last - from - k + i; ll d2 = (ix<0)?0:v[ix]; res = max(res, d1 + d2); } return res; } return v[last - from]; } bool qcmp(int a, int b) { return ((q[a].first < q[b].first) || ((q[a].first == q[b].first) && (q[a].second < q[b].second))); } void solve() { computeSum(); startDp(); computeAvailableDp(); int r[qs]; for(int i=0;i<qs;i++) { r[i] = i; } sort(r, r + qs, qcmp); for(int i=0;i<qs;i++) { res[r[i]] = solveRange(q[r[i]].first, q[r[i]].second); } } int main(int argc, char** argv) { int from, to; scanf("%d%d%d", &n, &k, &qs); for(int i=0;i<n;i++) { scanf("%lld", &(a[i])); } for(int i=0;i<qs;i++) { scanf("%d%d", &from, &to); q.push_back(make_pair(from-1, to-1)); } solve(); for(int i=0;i<qs;i++) { printf("%lld\n", res[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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <algorithm> #include <set> #include <map> #include <vector> #include <unordered_map> #include <cstdio> #define ll long long #define PII pair<int, int> using namespace std; int n,k,qs; ll a[300000]; vector<PII> q; ll res[300000]; ll s[300000]; ll v[300000]; vector<int> clusters; unordered_map<int, ll*> dp; set<int> availableDp; void computeSum() { ll sum = 0; for(int i=0;i<k;i++) { s[i] = 0; sum += a[i]; } s[k-1] = sum; for(int i=k;i<n;i++) { sum += a[i]-a[i-k]; s[i] = sum; } } void selectClusters(int lastFrom) { set<int> selected; int chunks = 27; for(int i=0;i<chunks;i++) { selected.insert((i*lastFrom)/chunks); } int p = 0; for(auto sel : selected) { clusters.push_back(sel); } } void computeDp(ll* d, int start, int end) { int len = end - start + 1; d[k - 1] = max(0LL, s[start + k - 1]); for(int i=k;i<len;i++) { ll best1 = 0; for(int j=0;j<k;j++) { if(i - j - k >= 0) { best1 = max(best1, d[i - j - k]); } } best1 += max(0LL, s[start + i]); ll best2 = best1; for(int j=0;j<k-1;j++) { best2 = max(best2, d[i - j - 1]); } d[i] = max(best1, best2); } } void startDp() { int lastFrom = -1; int lastTo = -1; for(auto qe : q) { lastFrom = max(lastFrom, qe.first); lastTo = max(lastTo, qe.second); } selectClusters(lastFrom); for(auto lastLeft : clusters) { if(lastLeft + 2*k <= lastTo) { for(int i=0;i<k;i++) { int start = lastLeft + i; if(dp.find(start) == dp.end()) { ll* d = new ll[lastTo - start + 1]; fill(d, d+k-1, 0LL); computeDp(d, start, lastTo); dp[start] = d; } } } } } void computeAvailableDp() { vector<int> keys; for(auto it=dp.begin();it!=dp.end();it++) { keys.push_back(it->first); } sort(keys.begin(), keys.end(), greater<int>()); int prev = -1; int consecutive = 0; for(auto cur: keys) { if(cur == prev - 1) { consecutive++; if(consecutive >= k-1) { availableDp.insert(cur); } } else { consecutive = 0; } prev = cur; } } ll solveRange(int from, int to) { if (to - from + 1 < k) { return 0; } auto f = availableDp.lower_bound(from); bool useDp = false; int dpStart = -1; int last = 0; if(f != availableDp.end()) { useDp = (*f) < (to - k); dpStart = *f; } if(useDp) { last = (*f) + k - 1; } else { last = to; } computeDp(v, from, last); if(useDp) { ll res = 0; for(int i=0;i<k;i++) { ll* d = dp[dpStart+i]; ll d1 = d[to - (dpStart+i)]; int ix = last - from - k + i; ll d2 = (ix<0)?0:v[ix]; res = max(res, d1 + d2); } return res; } return v[last - from]; } bool qcmp(int a, int b) { return ((q[a].first < q[b].first) || ((q[a].first == q[b].first) && (q[a].second < q[b].second))); } void solve() { computeSum(); startDp(); computeAvailableDp(); int r[qs]; for(int i=0;i<qs;i++) { r[i] = i; } sort(r, r + qs, qcmp); for(int i=0;i<qs;i++) { res[r[i]] = solveRange(q[r[i]].first, q[r[i]].second); } } int main(int argc, char** argv) { int from, to; scanf("%d%d%d", &n, &k, &qs); for(int i=0;i<n;i++) { scanf("%lld", &(a[i])); } for(int i=0;i<qs;i++) { scanf("%d%d", &from, &to); q.push_back(make_pair(from-1, to-1)); } solve(); for(int i=0;i<qs;i++) { printf("%lld\n", res[i]); } return 0; } |