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
#include <bits/stdc++.h>
#include <random>
#define ll long long int
#define pb push_back
#define st first
#define nd second
#define pii pair<int,int>
#define mp make_pair
#define pll pair<long long,long long>
#define ld long double
#define ull unsigned long long

using namespace std;

const int nax = 3e5 + 5;
ll a[nax];
int n, k, q;
ll dp[nax];
ll pref[nax];
ll best[nax];
const ll inf = 1e18;
ll ans[nax];

ll dpForward[nax];
ll bestForward[nax];
ll dpBackward[nax];
ll bestBackward[nax];

int inter(int s1, int e1, int s2, int e2){
    int w1 = max(s1, s2);
    int w2 = min(e1, e2);
    return max(0, w2 - w1 + 1);
}

void reku(int lo, int hi, vector<pair<pii, int> > zap){
    if(zap.empty()) return;
    int len = hi - lo + 1;
    if(len < k){
        for(auto cur : zap){
            ans[cur.nd] = 0;
        }
        return;
    }
    if(len == 1){
        for(auto cur : zap){
            ans[cur.nd] = 0;
            if(k == 1) ans[cur.nd] = max(ans[cur.nd], a[lo]);
        }
        return;
    }
    int mid = (lo + hi) / 2;
    vector<pair<pii, int> > L, R, stay;
    for(auto cur : zap){
        if(cur.st.nd <= mid) L.pb(cur);
        else if(cur.st.st > mid) R.pb(cur);
        else stay.pb(cur);
    }
    reku(lo, mid, L);
    reku(mid + 1, hi, R);
    if(stay.empty()) return;
    int sp = mid - k - 1;
    sp = max(sp, lo - 1);
    int iteracje = 2 * k + 3;
    while(sp <= hi && iteracje){
        iteracje -= 1;
        int s1 = lo;
        int e1 = sp;
        int s2 = sp + 1;
        int e2 = hi;
        // [s2, e2]
        bestForward[s2 - 1] = 0;
        for(int i=s2;i<=e2;i++){
            bestForward[i] = -inf;
            int j = i - k + 1;
            if(j < s2){
                dpForward[i] = -inf;
            }
            else{
                dpForward[i] = pref[i] - pref[j - 1] + bestForward[j - 1];
            }
            bestForward[i] = max(dpForward[i], bestForward[i - 1]);
        }

        bestBackward[e1 + 1] = 0;
        for(int i=e1;i>=s1;i--){
            bestBackward[i] = -inf;
            int j = i + k - 1;
            if(j > e1){
                dpBackward[i] = -inf;
            }
            else{
                dpBackward[i] = pref[j] - pref[i - 1] + bestBackward[j + 1];
            }
            bestBackward[i] = max(dpBackward[i], bestBackward[i + 1]);
        }

        for(auto cur : stay){
            int L = cur.st.st;
            int R = cur.st.nd;
            int id = cur.nd;
            ll pos = 0;
            if(inter(L, R, s1, e1) > 0 && R >= e1){
                pos += bestBackward[L];
            }
            if(inter(L, R, s2, e2) > 0 && L <= s2){
                pos += bestForward[R];
            }
            ans[id] = max(ans[id], pos);
        }
        sp += 1;
    }
}


void solve2(){
    vector<pair<pii, int> > zap;
    for(int i=1;i<=q;i++){
        int l, r; cin >> l >> r;
        zap.pb(mp(mp(l, r), i));
    }
    reku(1, n, zap);
    for(int i=1;i<=q;i++) cout << ans[i] << "\n";
    return;
}

void solve(){
    cin >> n >> k >> q;
    for(int i=1;i<=n;i++) cin >> a[i];
    for(int i=1;i<=n;i++) pref[i] = pref[i - 1] + a[i];
    if(k <= 30){
        solve2();
        return;
    }
    while(q--){
        int l, r; cin >> l >> r;
        best[l - 1] = 0;
        for(int i=l;i<=r;i++){
            best[i] = -inf;
            int j = i - k + 1;
            if(j < l){
                dp[i] = -inf;
            }
            else{
                dp[i] = pref[i] - pref[j - 1] + best[j - 1];
            }
            best[i] = max(dp[i], best[i - 1]);
        }
        cout << best[r] << "\n";
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    int tt = 1;
    // cin >> tt;
    while(tt--) solve();

    return 0;
}