#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; #define WEZ(a, b) if((a) < (b)) a = b; constexpr int N = 300'001; constexpr int THRESH = 800; constexpr int S = 400; constexpr int MAX_M = 400; int n, k, q; ll big[S][N]; // TODO: czy mozna jakos zreinterpretowac pamiec na [N][S]? wtedy lepszy cache dla duzych k ll(&arr)[N][S] = *reinterpret_cast<ll(*)[N][S]>(&big); ll dp[N]; ll pref[N]; int A[N]; ll C[N]; PR<PII, int> Q[N]; ll ans[N]; ll straz_dp[N]; int straz[N]; void jazda(const int ind, int a){ if(a > n) return; big[ind][a-1] = 0; REP(i, a, n+1){ // TODO: opt big[ind][i] = C[i] + (i-k >= a ? big[ind][i-k] : 0ll); // TODO: bez ifa powinno byc git if(big[ind][i-1] > big[ind][i]) big[ind][i] = big[ind][i-1]; } } void jazda_straz(int a){ if(a > n) return; straz_dp[a-1] = 0; REP(i, a, n+1){ // TODO: opt straz_dp[i] = C[i] + (i-k >= a ? straz_dp[i-k] : 0ll); // TODO: bez ifa powinno byc git if(straz_dp[i-1] > straz_dp[i]) straz_dp[i] = straz_dp[i-1]; } } void jazda_dp(int a, int b){ dp[a-1] = 0; REP(i, a, b+1){ // TODO: opt dp[i] = C[i] + (i-k >= a ? dp[i-k] : 0ll); // TODO: bez ifa powinno byc git if(dp[i-1] > dp[i]) dp[i] = dp[i-1]; } } void duze(){ // k > THRESH std::sort(Q, Q + q, [&](PR<PII, int> a, PR<PII, int> b){ return (a.X.X - 1 + k) % k < (b.X.X - 1 + k) % k; }); int M = std::min(MAX_M - 2, std::max(1, (int)std::sqrt(k) - 5)); for(int s = 1; s <= n; s += k){ // TODO: od k int e = std::min(s + k - 1, n); for(int i = s; i <= e; ++i){ if((i % k) % M == 0 || i == e){ straz[i] = 1; } } for(int kt = 0; kt < q; ++kt){ int a = Q[kt].X.X; int b = Q[kt].X.Y; int ind = Q[kt].Y; if(a > b) continue; if(a > e) continue; ll mx = 0; for(int i = a; i <= b; ++i){ if(straz[i]){ if(straz[i] != 2){ jazda_straz(i); straz[i] = 2; } WEZ(ans[ind], straz_dp[b] + mx); break; } mx = arr[ind][i-a]; if(C[i] > 0) arr[ind][i-a] = arr[ind][i-a] + C[i]; if(i > a && arr[ind][i-a-1] > arr[ind][i-a]) arr[ind][i-a] = arr[ind][i-a-1]; WEZ(ans[ind], arr[ind][i-a]); } Q[kt].X.X += k; } } } void male(){ // k <= THRESH std::sort(Q, Q + q); int M = std::max(1, std::max(1, (int)std::sqrt(n) + 1) * std::max(1, (int)std::sqrt(k) + 1)); int qind = q-1; for(int s = n; s >= 1; s -= M){ // TODO: do k while(qind >= 0 && Q[qind].X.X > s) qind--; for(int ADD = 0; ADD < k; ADD += S){ int UPTO = std::min(S, k - ADD); for(int off = 0; off < UPTO; ++off){ jazda(off, s + 1 + ADD + off); } int qq = qind; for(int i = 0; i < M && s - i >= 1; ++i){ int a = s - i; jazda_dp(a, s); while(qq >= 0 && Q[qq].X.X == a){ int ind = Q[qq].Y; int b = Q[qq].X.Y; qq--; if(a > b) continue; if(b <= s){ WEZ(ans[ind], dp[b]); }else{ WEZ(ans[ind], dp[s]); if(s + 1 + ADD <= b){ WEZ(ans[ind], big[0][b]); } // TODO: for (od, do) zamiast ifow for(int j = 0; j < UPTO && s + 1 + ADD + j <= b; ++j){ int from = s + 1 + ADD + j; int moge = from - k; if(moge >= a){ WEZ(ans[ind], big[j][b] + dp[moge]); } } } } } } // ADD } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> k >> q; FOR(i, n) std::cin >> A[i]; FOR(i, n) pref[i+1] = pref[i] + A[i]; REP(i, k, n+1) C[i] = pref[i] - pref[i-k]; FOR(i, q){ std::cin >> Q[i].X.X >> Q[i].X.Y; Q[i].X.X += k-1; Q[i].Y = i; } if(k <= THRESH){ male(); }else{ duze(); } FOR(i, q) std::cout << ans[i] << "\n"; 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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; #define WEZ(a, b) if((a) < (b)) a = b; constexpr int N = 300'001; constexpr int THRESH = 800; constexpr int S = 400; constexpr int MAX_M = 400; int n, k, q; ll big[S][N]; // TODO: czy mozna jakos zreinterpretowac pamiec na [N][S]? wtedy lepszy cache dla duzych k ll(&arr)[N][S] = *reinterpret_cast<ll(*)[N][S]>(&big); ll dp[N]; ll pref[N]; int A[N]; ll C[N]; PR<PII, int> Q[N]; ll ans[N]; ll straz_dp[N]; int straz[N]; void jazda(const int ind, int a){ if(a > n) return; big[ind][a-1] = 0; REP(i, a, n+1){ // TODO: opt big[ind][i] = C[i] + (i-k >= a ? big[ind][i-k] : 0ll); // TODO: bez ifa powinno byc git if(big[ind][i-1] > big[ind][i]) big[ind][i] = big[ind][i-1]; } } void jazda_straz(int a){ if(a > n) return; straz_dp[a-1] = 0; REP(i, a, n+1){ // TODO: opt straz_dp[i] = C[i] + (i-k >= a ? straz_dp[i-k] : 0ll); // TODO: bez ifa powinno byc git if(straz_dp[i-1] > straz_dp[i]) straz_dp[i] = straz_dp[i-1]; } } void jazda_dp(int a, int b){ dp[a-1] = 0; REP(i, a, b+1){ // TODO: opt dp[i] = C[i] + (i-k >= a ? dp[i-k] : 0ll); // TODO: bez ifa powinno byc git if(dp[i-1] > dp[i]) dp[i] = dp[i-1]; } } void duze(){ // k > THRESH std::sort(Q, Q + q, [&](PR<PII, int> a, PR<PII, int> b){ return (a.X.X - 1 + k) % k < (b.X.X - 1 + k) % k; }); int M = std::min(MAX_M - 2, std::max(1, (int)std::sqrt(k) - 5)); for(int s = 1; s <= n; s += k){ // TODO: od k int e = std::min(s + k - 1, n); for(int i = s; i <= e; ++i){ if((i % k) % M == 0 || i == e){ straz[i] = 1; } } for(int kt = 0; kt < q; ++kt){ int a = Q[kt].X.X; int b = Q[kt].X.Y; int ind = Q[kt].Y; if(a > b) continue; if(a > e) continue; ll mx = 0; for(int i = a; i <= b; ++i){ if(straz[i]){ if(straz[i] != 2){ jazda_straz(i); straz[i] = 2; } WEZ(ans[ind], straz_dp[b] + mx); break; } mx = arr[ind][i-a]; if(C[i] > 0) arr[ind][i-a] = arr[ind][i-a] + C[i]; if(i > a && arr[ind][i-a-1] > arr[ind][i-a]) arr[ind][i-a] = arr[ind][i-a-1]; WEZ(ans[ind], arr[ind][i-a]); } Q[kt].X.X += k; } } } void male(){ // k <= THRESH std::sort(Q, Q + q); int M = std::max(1, std::max(1, (int)std::sqrt(n) + 1) * std::max(1, (int)std::sqrt(k) + 1)); int qind = q-1; for(int s = n; s >= 1; s -= M){ // TODO: do k while(qind >= 0 && Q[qind].X.X > s) qind--; for(int ADD = 0; ADD < k; ADD += S){ int UPTO = std::min(S, k - ADD); for(int off = 0; off < UPTO; ++off){ jazda(off, s + 1 + ADD + off); } int qq = qind; for(int i = 0; i < M && s - i >= 1; ++i){ int a = s - i; jazda_dp(a, s); while(qq >= 0 && Q[qq].X.X == a){ int ind = Q[qq].Y; int b = Q[qq].X.Y; qq--; if(a > b) continue; if(b <= s){ WEZ(ans[ind], dp[b]); }else{ WEZ(ans[ind], dp[s]); if(s + 1 + ADD <= b){ WEZ(ans[ind], big[0][b]); } // TODO: for (od, do) zamiast ifow for(int j = 0; j < UPTO && s + 1 + ADD + j <= b; ++j){ int from = s + 1 + ADD + j; int moge = from - k; if(moge >= a){ WEZ(ans[ind], big[j][b] + dp[moge]); } } } } } } // ADD } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> k >> q; FOR(i, n) std::cin >> A[i]; FOR(i, n) pref[i+1] = pref[i] + A[i]; REP(i, k, n+1) C[i] = pref[i] - pref[i-k]; FOR(i, q){ std::cin >> Q[i].X.X >> Q[i].X.Y; Q[i].X.X += k-1; Q[i].Y = i; } if(k <= THRESH){ male(); }else{ duze(); } FOR(i, q) std::cout << ans[i] << "\n"; return 0; } |