#include <iostream> #include <vector> #include <queue> using namespace std; const int maxN = 3e5+5; int n, k; int a[maxN]; long long dp[maxN]; vector<long long> oddzial; queue<long long> maxi_que; long long calculate_ans(int left, int right) { long long ans = 0; for(int i = left; i < left + k && i <= right; i++) { maxi_que.push(ans + oddzial[i]); } for(int i = left + k; i <= right; i++) { ans = max(ans, maxi_que.front()); maxi_que.pop(); maxi_que.push(ans + oddzial[i]); } while(!maxi_que.empty()) { ans = max(ans, maxi_que.front()); maxi_que.pop(); } return ans; } void solve() { int q, l, r; long long suma, ans; cin >> n >> k >> q; for(int i = 1; i <= n; i++) cin >> a[i]; suma = 0; for(int i = 1; i <= k; i++) suma += a[i]; oddzial.push_back(0); oddzial.push_back(suma); for(int i = 2; i <= n - k + 1; i++) { suma -= a[i-1]; suma += a[i+k-1]; oddzial.push_back(max(suma,(long long)0)); } for(int tc = 0; tc < q; tc++) { cin >> l >> r; if(r - l + 1 < k) { cout << 0 << '\n'; continue; } ans = calculate_ans(l, r - k + 1); cout << ans << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); 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 | #include <iostream> #include <vector> #include <queue> using namespace std; const int maxN = 3e5+5; int n, k; int a[maxN]; long long dp[maxN]; vector<long long> oddzial; queue<long long> maxi_que; long long calculate_ans(int left, int right) { long long ans = 0; for(int i = left; i < left + k && i <= right; i++) { maxi_que.push(ans + oddzial[i]); } for(int i = left + k; i <= right; i++) { ans = max(ans, maxi_que.front()); maxi_que.pop(); maxi_que.push(ans + oddzial[i]); } while(!maxi_que.empty()) { ans = max(ans, maxi_que.front()); maxi_que.pop(); } return ans; } void solve() { int q, l, r; long long suma, ans; cin >> n >> k >> q; for(int i = 1; i <= n; i++) cin >> a[i]; suma = 0; for(int i = 1; i <= k; i++) suma += a[i]; oddzial.push_back(0); oddzial.push_back(suma); for(int i = 2; i <= n - k + 1; i++) { suma -= a[i-1]; suma += a[i+k-1]; oddzial.push_back(max(suma,(long long)0)); } for(int tc = 0; tc < q; tc++) { cin >> l >> r; if(r - l + 1 < k) { cout << 0 << '\n'; continue; } ans = calculate_ans(l, r - k + 1); cout << ans << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } |