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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <bits/stdc++.h>

using namespace std;
#define LL long long
#define BIGMOD 1000000007LL

#define DBG(X) 



struct Query {
    int L, R;
    int originalIdx;
    LL answer = -777;
    Query(int left, int right, int _originalIdx) : L(left), R(right), originalIdx(_originalIdx)
    {

    }
};

LL solveSlowInterval(vector<int> &skills, int k, const vector<LL> &prefSum, int left, int right)
{
    int n = skills.size();
    vector<LL> dp(n+1, 0);
    DBG(printf("Answering query (%d,%d), k=\n", left, right));
    for (int i=left+k-1; i <= right; i++)
    {
        int poczatekPrzedzialu = i-k+1;
        LL intervalSum = prefSum[i+1] - prefSum[poczatekPrzedzialu];
        LL valWithPrevInterval = dp[i-k+1] + intervalSum;
        if (valWithPrevInterval > dp[i])
        {
           dp[i+1] = valWithPrevInterval;
        }
        else
        {
            dp[i+1] = dp[i];
        }
        DBG(printf("dp[%d]=%lld, dp[i-k]=%lld, invervalSum=%lld\n", i+1, dp[i+1], dp[i-k+1], intervalSum));
    }
    /*printf("Answer:\n");
    for (int v = last[right+1]; v; v = last[v])
    {
        printf("(");
        for (int i=0; i < k;i++)
        {
            if (i) printf(",");
            printf("%d", skills[v+i]);
        }
        printf(")");
    }
    printf("\n");*/
    return dp[right+1];
}

LL solveSlow2(const vector<LL> &intervals, vector<LL> &dp,  const vector<LL> &prefSum, int k, int left, int right)
{
    dp[0] = 0;
    dp[right+1] = 0;
    const int loopStop = min((int)dp.size()-1, left+k-1);
    for (int i=left; i <= loopStop; i++)
    {
        dp[i] = 0;
    }
    DBG(printf("Answering query (%d,%d), k=\n", left, right));
    bool useSmallCases = false;
    if ((right - (left+k-1)) < 6) useSmallCases = true;
    if (k < 5) useSmallCases = true;

    if (useSmallCases)
    {
        for (int i=left+k-1; i <= right; i+=1)
        {
            // block1
            {
                register const int ip1 = i+1;
                register const int poczatekPrzedzialu = ip1-k;
                
                const LL intervalSum = prefSum[ip1] - prefSum[poczatekPrzedzialu];
                const LL valWithPrevInterval = dp[poczatekPrzedzialu] + intervalSum;
                if (valWithPrevInterval > dp[i])
                {
                    dp[ip1] = valWithPrevInterval;
                }
                else
                {
                    dp[ip1] = dp[i];
                }
            }
            DBG(printf("dp[%d]=%lld, dp[i-k]=%lld, invervalSum=%lld\n", i+1, dp[i+1], dp[i-k+1], intervalSum));
        }
    }
    else
    {
        int i;
        for (i=left+k-1; i <= right-4; i+=4)
        {
            const LL dp0 = dp[i];

            LL dpRes0;
            LL dpRes1;
            LL dpRes2;
            LL dpRes3;

            // block0
            {
                register const int ibase = i;
                register const int ip1 = ibase+1;
                register const int poczatekPrzedzialu = ip1-k;
                
                const LL intervalSum = prefSum[ip1] - prefSum[poczatekPrzedzialu];
                const LL valWithPrevInterval = dp[poczatekPrzedzialu] + intervalSum;
                if (valWithPrevInterval > dp0)
                {
                    dpRes0 = valWithPrevInterval;
                }
                else
                {
                    dpRes0 = dp0;
                }
            }
            // block1
            {
                register const int ibase = i+1;

                register const int ip1 = ibase+1;
                register const int poczatekPrzedzialu = ip1-k;
 
                const LL intervalSum = prefSum[ip1] - prefSum[poczatekPrzedzialu];
                const LL valWithPrevInterval = dp[poczatekPrzedzialu] + intervalSum;
                if (valWithPrevInterval > dpRes0)
                {
                    dpRes1 = valWithPrevInterval;
                }
                else
                {
                    dpRes1 = dpRes0;
                }
            }
            // block2
            {
                register const int ibase = i+2;
                register const int ip1 = ibase+1;
                
                register const int poczatekPrzedzialu = ip1-k;
                
                const LL intervalSum = prefSum[ip1] - prefSum[poczatekPrzedzialu];
                const LL valWithPrevInterval = dp[poczatekPrzedzialu] + intervalSum;
                if (valWithPrevInterval > dpRes1)
                {
                    dpRes2 = valWithPrevInterval;
                }
                else
                {
                    dpRes2 = dpRes1;
                }
            }
            // block4
            {
                register const int ibase = i+3;
                register const int ip1 = ibase+1;
                
                register const int poczatekPrzedzialu = ip1-k;
                
                const LL intervalSum = prefSum[ip1] - prefSum[poczatekPrzedzialu];
                const LL valWithPrevInterval = dp[poczatekPrzedzialu] + intervalSum;
                if (valWithPrevInterval > dpRes2)
                {
                    dpRes3 = valWithPrevInterval;
                }
                else
                {
                    dpRes3 = dpRes2;
                }
            }
            dp[i+1] = dpRes0;
            dp[i+2] = dpRes1;
            dp[i+3] = dpRes2;
            dp[i+4] = dpRes3;
        }
        for (int j=i; j <= right; j+=1)
        {
            // block1
            {
                register const int ip1 = j+1;
                register const int poczatekPrzedzialu = ip1-k;
                
                const LL intervalSum = prefSum[ip1] - prefSum[poczatekPrzedzialu];
                const LL valWithPrevInterval = dp[poczatekPrzedzialu] + intervalSum;
                if (valWithPrevInterval > dp[j])
                {
                dp[ip1] = valWithPrevInterval;
                }
                else
                {
                    dp[ip1] = dp[j];
                }
            }
        }
    }
    
    return dp[right+1];
}

// maksymalna suma wartosci
void solve(vector<int> &skills, int k, vector<Query> &queries)
{
    int n = skills.size();
    vector<LL> prefSum(n+1);
    vector<LL> dp(n+1, 0);
    vector<LL> intervals;
    prefSum[0] = 0;
    for (int i=0; i < n; i++)
    {
        prefSum[i+1] = prefSum[i] + skills[i];
    }
    for (int i=k; i < n; i++)
    {
        intervals.push_back(prefSum[i] - prefSum[i-k]);
    }
    sort(queries.begin(), queries.end(), [](const Query &a, const Query &b) {
        return a.R < b.R;
    });

    for (int i=0; i < queries.size(); i++)
    {
        Query &q = queries[i];
        //q.answer = solveSlowInterval(skills, k, prefSum, q.L, q.R);
        q.answer = solveSlow2(intervals, dp, prefSum, k, q.L, q.R);
    }

    sort(queries.begin(), queries.end(), [](const Query &a, const Query &b) {
        return a.originalIdx < b.originalIdx;
    });
}

int main() {
    int n,k, q;
    scanf("%d%d%d", &n, &k, &q);
    vector<int> skills(n);
    vector<Query> queries;
    for (int i=0; i < n; i++)
    {
        scanf("%d", &skills[i]);
    }
    for (int i=0; i < q; i++)
    {
        int L,R;
        scanf("%d%d", &L, &R);
        queries.push_back(Query(L-1,R-1, i));
    }
    solve(skills, k, queries);
    for (int i=0; i < q; i++)
    {
        printf("%lld\n", queries[i].answer);
    }
    return 0;
}