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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> P;
struct Query : public P {
	Query(int idx, int left, int right) : P{left-1, right-1}, index(idx) {}
	int index;
	long long result{0};
};

typedef vector<long long> Solver;

int N, K, Q;

int main() {
	ios_base::sync_with_stdio(false);

	cin >> N;
	cin >> K;
	cin >> Q;

	vector<int> v;
	v.reserve(N);
	for (int i = 0; i < N; i++) {
		int a;
		cin >> a;
		v.push_back(a);
	}
	vector<long long> vsum;
	v.reserve(N);
	long long sum = 0;
	for (int i = 0; i < N; i++) {
		sum += v[i];
		if (i>=K)
			sum -= v[i-K];
		vsum.push_back(sum);
	}
	for (int i = 0; i < K-1; i++)
		vsum[i] = 0;

	vector<vector<Query>> queriesmap(N);
	vector<int> queriesmaxr(N, 0);
	for (int q = 0; q < Q; q++) {
		int l, r;
		cin >> l >> r;
		queriesmap[l-1].push_back(Query{q, l, r});
		queriesmaxr[l-1] = max(queriesmaxr[l-1], r-1);
	}

	vector<long long> results(Q, 0);
	Solver solver(N, 0);

	for (int qi = 0; qi < N; qi++) {
		int l = qi;
		if (queriesmap[l].empty()) continue;

		for (int i = max(0, l-1); i < l+K && i < queriesmaxr[l]+1; i++)
			solver[i] = 0;

		for (int i = l+K-1; i < queriesmaxr[l]+1; i++)
			solver[i] = max(solver[i-1], vsum[i] + (i >= K ? solver[i-K] : 0));

		for (auto &q: queriesmap[qi]) {
			int r = q.second;
			if (r - l + 1 < K)
				continue;

			results[q.index] = solver[r];
		}
	}

	for (auto r: results)
		cout << r << endl;

	return 0;
}