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
#include <algorithm>
#include <cstdio>
#include <set>

struct Question {
    int q, l, r;

    bool operator<(const Question& other) const {
        return (l < other.l) || (l == other.l && r < other.r);
    }
};

int A[300000];

long C[300000]; // C[n] = Σ_{i=0}^n A[i]
long S[300000]; // S[n] = Σ_{i=n}^{n+K-1} A[i] = C[n+K-1] - C[n-1]
long buf[300000];

int Spos[300000]; // Spos[n] = pierwszy index i≥n taki że S[i]>0

int unique[300000];
Question questions[300000];
long answers[300000];

int N, K, Q;

long long compute(const Question& question) {
    static int last_l = -1;
    static int last_r = -1;

    const int l = question.l, r = question.r - K + 1;
    if (l > r) {
        return 0;
    }

    int i;
    if (last_l >= l) {
        i = last_r + 1;
    } else {
        i = l + 1;
        buf[l] = S[l];
    }
    int i1_max = std::min(r, l + K - 1);
    for (; i <= i1_max; ++i) {
        buf[i] = std::max(S[i], buf[i-1]);
    }
    for (; i <= r; ++i) {
        buf[i] = std::max(S[i] + buf[i-K], buf[i-1]);
    }

    last_l = Spos[l];
    last_r = r;
    return buf[r];
}

int main() {
    scanf("%d%d%d", &N, &K, &Q);
    const int MAX = N - K;
    for (int i=0; i<N; ++i) {
        scanf("%d", &A[i]);
    }
    for (int q=0; q<Q; ++q) {
        int l, r;
        scanf("%d%d", &l, &r);
        questions[q].q = q;
        questions[q].l = l-1;
        questions[q].r = r-1;
    }

    int unique_l = 0;
    for (int q=0; q<Q; ++q) {
        unique[q] = questions[q].l;
    }
    std::sort(unique, unique+Q);
    for (int q=1; q<Q; ++q) {
        if (unique[q] != unique[q-1]) {
            ++unique_l;
        }
    }

    int unique_r = 0;
    for (int q=0; q<Q; ++q) {
        unique[q] = questions[q].r;
    }
    std::sort(unique, unique+Q);
    for (int q=1; q<Q; ++q) {
        if (unique[q] != unique[q-1]) {
            ++unique_r;
        }
    }

    if (unique_r < unique_l) {
        // zamieniamy
        std::reverse(A, A+N);
        for (int q=0; q<Q; ++q) {
            int l = questions[q].l, r = questions[q].r;
            questions[q].l = N-1-r;
            questions[q].r = N-1-l;
        }
    }

    C[0] = A[0];
    for (int i=1; i<N; ++i) {
        C[i] = C[i-1] + static_cast<long long>(A[i]);
    }
    S[0] = std::max(0l, C[K-1]);
    for (int i=1; i<=MAX; ++i) {
        S[i] = std::max(0l, C[i+K-1] - C[i-1]);
    }
    int last_positive_index = N;
    for (int i=N-1; i>=0; --i) {
        if (S[i] > 0) {
            last_positive_index = i;
        }
        Spos[i] = last_positive_index;
    }

    std::sort(questions, questions+Q);
    for (int iq=0; iq<Q; ++iq) {
        answers[questions[iq].q] = compute(questions[iq]);
    }
    for (int q=0; q<Q; ++q) {
        printf("%ld\n", answers[q]);
    }
}