#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef double K; constexpr int INF = 0x3f3f3f3f; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define TRAV(x, a) for(auto &x: (a)) #define SZ(x) ((int)(x).size()) #define PB push_back #define X first #define Y second constexpr int N = 3e5 + 5; int t[N]; ll pref[N], dp[N]; void solve() { int n, k, q; cin >> n >> k >> q; FOR(i, 1, n + 1) cin >> t[i]; FOR(i, 1, n + 1) pref[i] = pref[i - 1] + t[i]; FOR(qq, 0, q) { int a, b; cin >> a >> b; dp[a - 1] = 0; if(k > b - a + 1) { cout << 0 << '\n'; continue; } FOR(i, a, a + k - 1) dp[i] = 0; FOR(i, a + k - 1, b + 1) dp[i] = max(dp[i - 1], dp[i - k] + pref[i] - pref[i - k]); cout << dp[b] << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) { // // cout << "Case #" << te + 1 << ": "; // solve(); // } 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef double K; constexpr int INF = 0x3f3f3f3f; #define FOR(i, b, e) for(int i = (b); i < (e); i++) #define TRAV(x, a) for(auto &x: (a)) #define SZ(x) ((int)(x).size()) #define PB push_back #define X first #define Y second constexpr int N = 3e5 + 5; int t[N]; ll pref[N], dp[N]; void solve() { int n, k, q; cin >> n >> k >> q; FOR(i, 1, n + 1) cin >> t[i]; FOR(i, 1, n + 1) pref[i] = pref[i - 1] + t[i]; FOR(qq, 0, q) { int a, b; cin >> a >> b; dp[a - 1] = 0; if(k > b - a + 1) { cout << 0 << '\n'; continue; } FOR(i, a, a + k - 1) dp[i] = 0; FOR(i, a + k - 1, b + 1) dp[i] = max(dp[i - 1], dp[i - k] + pref[i] - pref[i - k]); cout << dp[b] << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) { // // cout << "Case #" << te + 1 << ": "; // solve(); // } solve(); return 0; } |