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
// Karol Kosinski 2021
#include <bits/stdc++.h>
// #define ENABLE_DEBUG
#define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) int((c).size())
#define TIE(x...) int x;tie(x)
#define X first
#define Y second
#ifndef ENABLE_DEBUG
#define DEB(k,f,x...)
#else
#define DEB(k,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);f(x);}
#endif
#define DEBL DEB(1,void,0)
#define DEBF(f,x...) DEB(1,f,x)
#define DEBUG(x...) DEB(0,printf,x)
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;

constexpr int NX = 300'005;

int A[NX];
LL S[NX], R[NX], Res[NX];
TIII Q[NX], P[NX];
vector<LL> Front[NX], Back[NX];

LL query_front(int k, int x, int y)
{
    auto& T = Front[x];
    int aim = y - x + 1;
    if ( T.empty() )
    {
        DEBUG("(%3d, %3d)\n", x, y);
        T.resize( aim + 1 );
        int base = x - k - 1;
        FR_(i,k,aim)
        {
            DEBUG("\t--> %3lld | %3lld+%3lld-%3lld\n", T[ i - 1 ] , T[ i - k ], S[ base + k + i ], S[ base + i ]);
            T[i] = max( T[ i - 1 ], T[ i - k ] + S[ base + k + i ] - S[ base + i ] );
        }
    }
    return T[aim];
}

LL query_back(int k, int x, int y)
{
    auto& T = Back[x];
    int aim = y - x + 1;
    if ( T.empty() )
    {
        DEBUG("(%3d, %3d)\n", x, y);
        T.resize( aim + 1 );
        int base = x - k - 1;
        FR_(i,k,aim)
        {
            DEBUG("\t--> %3lld | %3lld+%3lld-%3lld\n", T[ i - 1 ] , T[ i - k ], R[ base + k + i ], R[ base + i ]);
            T[i] = max( T[ i - 1 ], T[ i - k ] + R[ base + k + i ] - R[ base + i ] );
        }
    }
    return T[aim];
}

int main()
{
    int n, k, q;
    scanf("%d%d%d", &n, &k, &q);
    FR_(i,1,n) scanf("%d", A + i);
    FR_(i,1,n) S[i] = S[ i - 1 ] + A[i];
    reverse( A + 1, A + n + 1 );
    FR_(i,1,n) R[i] = R[ i - 1 ] + A[i];
    FOR(i,0,q)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        Q[i] = { a, b, i };
        P[i] = { n + 1 - b, n + 1 - a, i };
    }
    sort( Q, Q + q, greater<TIII>() );
    sort( P, P + q, greater<TIII>() );
    int last = -1;
    LL qsum = 0, psum = 0;
    FOR(i,0,q)
    {
        TIE(x, y, ind) = Q[i];
        if ( last != x ) qsum += max(0, y - x - k + 2);
        last = x;
    }
    last = -1;
    FOR(i,0,q)
    {
        TIE(x, y, ind) = P[i];
        if ( last != x ) psum += max(0, y - x - k + 2);
        last = x;
    }
    DEBUG("-- qsum(%lld) : psum(%lld)\n", qsum, psum);
    if ( qsum < psum )
    {
        FOR(i,0,q)
        {
            TIE(x, y, ind) = Q[i];
            DEBUG("**  %d  %d\n", x, y);
            if ( y - x >= k - 1 )
            {
                Res[ind] = query_front(k, x, y);
            }
        }
    }
    else
    {
        FOR(i,0,q)
        {
            TIE(x, y, ind) = P[i];
            DEBUG("**  %d  %d\n", x, y);
            if ( y - x >= k - 1 )
            {
                Res[ind] = query_back(k, x, y);
            }
        }
    }
    FOR(i,0,q) printf("%lld\n", Res[i]);
    return 0;
}