// {{{ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using LD = long double; using PII = pair<int,int>; using PLL = pair<LL,LL>; using PLD = pair<LD,LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VB = vector<bool>; using VS = vector<string>; template<class T> using V = vector<T>; template<class T1, class T2> using P = pair<T1,T2>; template<class T,class Comp=greater<T>> using PQ = priority_queue<T,V<T>,Comp>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } /* const size_t rseed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(rseed); template<class T> T randint(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } */ /* find_by_order(k) - k'th largest counting from 0; order_of_key(k) - number of items strictly smaller than k; join(other), split(k, other) - all keys in other greater than in *(this). */ template<class K,class V,class Comp=less<K>> using ordered_map = __gnu_pbds::tree<K,V,Comp, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>; template<class T,class Comp=less<T>> using ordered_set = ordered_map<T,__gnu_pbds::null_type,Comp>; const int inf = 1e9 + 1; const LL infl = 1e18 + 1; void solve(); // }}} int main() { int t = 1; // scanf("%d", &t); FOR(i, 1, t) { // printf("Case #%d: ", i); solve(); } } const int N = 3e5; int n, k, q; LL a[N + 1], dp[N + 1]; struct Q { int l, r; LL ans; }; Q qr[N + 1]; void rec(int lo, int hi, VI v) { if (hi - lo <= 2 * k) { FOR(i, 0, sz(v) - 1) { FOR(j, qr[v[i]].l, qr[v[i]].r) { dp[j] = max(0ll, a[j]); if (j > qr[v[i]].l) amax(dp[j], dp[j - 1]); if (j >= qr[v[i]].l + k) amax(dp[j], dp[j - k] + a[j]); } amax(qr[v[i]].ans, dp[qr[v[i]].r]); // fprintf(stderr, "A %d -> %lld\n", v[i], qr[v[i]].ans); } return; } int mid = (lo + hi) / 2; VI vl, vr; for (int i = 0; i < sz(v); ) { if (qr[v[i]].r <= mid) { vl.PB(v[i]); swap(v[i], v.back()); v.pop_back(); } else if (qr[v[i]].l >= mid + 1) { vr.PB(v[i]); swap(v[i], v.back()); v.pop_back(); } else { i++; } } FOR(d, 0, k - 1) { FOR(i, mid + 1 - d, hi) { dp[i] = max(0ll, a[i]); if (i > mid + 1 - d) amax(dp[i], dp[i - 1]); if (i > mid - d + k) amax(dp[i], dp[i - k] + a[i]); } FORD(i, lo, mid - d) { dp[i] = 0; if (i < mid - d) amax(dp[i], dp[i + 1]); if (i < mid - d - k + 1) amax(dp[i], dp[i + k] + a[i]); if (i == mid - d - k + 1) amax(dp[i], a[i]); } FOR(i, 0, sz(v) - 1) { if (qr[v[i]].l <= mid - d) { amax(qr[v[i]].ans, dp[qr[v[i]].l] + dp[qr[v[i]].r]); } else if (qr[v[i]].l == mid + 1 - d) { amax(qr[v[i]].ans, dp[qr[v[i]].r]); } } /* FOR(i, lo, hi) { printf("%lld ", dp[i]); } putchar('\n'); */ } /* FOR(i, 0, sz(v) - 1) { fprintf(stderr, "B %d -> %lld\n", v[i], qr[v[i]].ans); } */ rec(lo, mid, vl); rec(mid + 1, hi, vr); } void solve() { scanf("%d %d %d", &n, &k, &q); // fprintf(stderr, "n=%d k=%d q=%d\n", n, k, q); FOR(i, 1, n) { scanf("%lld", &a[i]); dp[i] = dp[i - 1] + a[i]; } n -= k - 1; FOR(i, 1, n) { a[i] = dp[i + k - 1] - dp[i - 1]; } VI v; FOR(i, 1, q) { scanf("%d %d", &qr[i].l, &qr[i].r); qr[i].r -= k - 1; if (qr[i].l <= qr[i].r) { v.PB(i); } } rec(1, n, v); FOR(i, 1, q) { printf("%lld\n", qr[i].ans); } }
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | // {{{ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using LD = long double; using PII = pair<int,int>; using PLL = pair<LL,LL>; using PLD = pair<LD,LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VB = vector<bool>; using VS = vector<string>; template<class T> using V = vector<T>; template<class T1, class T2> using P = pair<T1,T2>; template<class T,class Comp=greater<T>> using PQ = priority_queue<T,V<T>,Comp>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } /* const size_t rseed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(rseed); template<class T> T randint(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } */ /* find_by_order(k) - k'th largest counting from 0; order_of_key(k) - number of items strictly smaller than k; join(other), split(k, other) - all keys in other greater than in *(this). */ template<class K,class V,class Comp=less<K>> using ordered_map = __gnu_pbds::tree<K,V,Comp, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>; template<class T,class Comp=less<T>> using ordered_set = ordered_map<T,__gnu_pbds::null_type,Comp>; const int inf = 1e9 + 1; const LL infl = 1e18 + 1; void solve(); // }}} int main() { int t = 1; // scanf("%d", &t); FOR(i, 1, t) { // printf("Case #%d: ", i); solve(); } } const int N = 3e5; int n, k, q; LL a[N + 1], dp[N + 1]; struct Q { int l, r; LL ans; }; Q qr[N + 1]; void rec(int lo, int hi, VI v) { if (hi - lo <= 2 * k) { FOR(i, 0, sz(v) - 1) { FOR(j, qr[v[i]].l, qr[v[i]].r) { dp[j] = max(0ll, a[j]); if (j > qr[v[i]].l) amax(dp[j], dp[j - 1]); if (j >= qr[v[i]].l + k) amax(dp[j], dp[j - k] + a[j]); } amax(qr[v[i]].ans, dp[qr[v[i]].r]); // fprintf(stderr, "A %d -> %lld\n", v[i], qr[v[i]].ans); } return; } int mid = (lo + hi) / 2; VI vl, vr; for (int i = 0; i < sz(v); ) { if (qr[v[i]].r <= mid) { vl.PB(v[i]); swap(v[i], v.back()); v.pop_back(); } else if (qr[v[i]].l >= mid + 1) { vr.PB(v[i]); swap(v[i], v.back()); v.pop_back(); } else { i++; } } FOR(d, 0, k - 1) { FOR(i, mid + 1 - d, hi) { dp[i] = max(0ll, a[i]); if (i > mid + 1 - d) amax(dp[i], dp[i - 1]); if (i > mid - d + k) amax(dp[i], dp[i - k] + a[i]); } FORD(i, lo, mid - d) { dp[i] = 0; if (i < mid - d) amax(dp[i], dp[i + 1]); if (i < mid - d - k + 1) amax(dp[i], dp[i + k] + a[i]); if (i == mid - d - k + 1) amax(dp[i], a[i]); } FOR(i, 0, sz(v) - 1) { if (qr[v[i]].l <= mid - d) { amax(qr[v[i]].ans, dp[qr[v[i]].l] + dp[qr[v[i]].r]); } else if (qr[v[i]].l == mid + 1 - d) { amax(qr[v[i]].ans, dp[qr[v[i]].r]); } } /* FOR(i, lo, hi) { printf("%lld ", dp[i]); } putchar('\n'); */ } /* FOR(i, 0, sz(v) - 1) { fprintf(stderr, "B %d -> %lld\n", v[i], qr[v[i]].ans); } */ rec(lo, mid, vl); rec(mid + 1, hi, vr); } void solve() { scanf("%d %d %d", &n, &k, &q); // fprintf(stderr, "n=%d k=%d q=%d\n", n, k, q); FOR(i, 1, n) { scanf("%lld", &a[i]); dp[i] = dp[i - 1] + a[i]; } n -= k - 1; FOR(i, 1, n) { a[i] = dp[i + k - 1] - dp[i - 1]; } VI v; FOR(i, 1, q) { scanf("%d %d", &qr[i].l, &qr[i].r); qr[i].r -= k - 1; if (qr[i].l <= qr[i].r) { v.PB(i); } } rec(1, n, v); FOR(i, 1, q) { printf("%lld\n", qr[i].ans); } } |