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
#include <bits/stdc++.h>

#define st first
#define nd second

typedef long long int lli;
typedef std::pair<int, int> par;

const int MAXN = 300'005;

lli v[MAXN];
lli sum[MAXN];
lli dp[MAXN];
std::vector<par> req[MAXN];
int maxr[MAXN];
lli ans[MAXN];
lli interval[MAXN];  // sum on k-interval ending in i
int req_bounds[MAXN][2];

lli d[MAXN * 5];
lli dpow;

lli read_max(int x, int p, int k, int pp, int kk) {
	if (p == pp && k == kk)
		return d[x];
	
	int s = (pp+kk)/2;
	
	if (k <= s)
		return read_max(2*x, p, k, pp, s);
	if (p >= s)
		return read_max(2*x+1, p, k, s, kk);
	
	return std::max(read_max(2*x, p, s, pp, s), read_max(2*x+1, s, k, s, kk));
}

int main() {
	int n, k, q;
	scanf("%d%d%d", &n, &k, &q);
	
	for (int i=1; i<=n; i++) {
		scanf("%lld", &v[i]);
		sum[i] = sum[i-1] + v[i];
	}
	
	for (int i=k; i<=n; i++) {
		interval[i] = sum[i] - sum[i-k];
	}
	
	// Fill max tree. Useful when interval is smaller than 2k.
	dpow = 1;
	while (dpow <= n)
		dpow *= 2;
	
	for (int i=0; i<=n; i++)
		d[dpow+i] = interval[i];
	
	for (int i=dpow-1; i>0; i--)
		d[i] = std::max(d[2*i], d[2*i+1]);
	
	int a, b;
	for (int i=1; i<=q; i++) {
		scanf("%d%d", &a, &b);
		
		if (b-a+1 < 2*k) {
// 			fprintf(stderr, "used\n");
			// only a single interval fits.
			if (b-a+1 < k)
				ans[i] = 0;
			else
				ans[i] = read_max(1, a+k-1, b+1, 0, dpow);
			ans[i] = std::max(ans[i], 0LL);
			
			continue;
		}
		
		req_bounds[i][0] = a;
		req_bounds[i][1] = b;
		
		if ((a / k) % 2 == 0) {
			// push
			int rem = a%k;
			int base = a - rem;
			
			for (int j=rem; j<=k; j++) {
				if (base+j+k <= n && base+j+k <= b && j<k) {
					if (interval[base+j+k-1] <= 0)
						continue;
					
					req[base+j+k].push_back({b, -i});
					maxr[base+j+k] = std::max(maxr[base+j+k], b);
				}
				else {
					req[base+j].push_back({b, -MAXN-i});
					maxr[base+j] = std::max(maxr[base+j], b);
					continue;
				}
			}
			
		}
		else {
			req[a].push_back({b, i});
			maxr[a] = std::max(maxr[a], b);
		}
	}
	
	for (int beg=n; beg>0; beg--) {
		if (req[beg].empty())
			continue;
		
// 		printf("computing for %d\n", beg);
		
		for (int i=beg+k-1; i<=maxr[beg]; i++) {
			dp[i] = std::max(dp[i-1], dp[i-k] + interval[i]);
		}
		
		for (const auto &r : req[beg]) {
// 			printf("request %d %d\n", r.st, r.nd);
			if (r.nd > 0) {
				ans[r.nd] = dp[r.st];
			}
			else {
				if (r.nd < -MAXN) {
					ans[-(r.nd+MAXN)] = std::max(ans[-(r.nd+MAXN)], dp[r.st]);
				}
				else {
					ans[-r.nd] = std::max(ans[-r.nd], dp[r.st] + interval[beg - 1]);
// 					printf("dp %lld, interval %lld\n", dp[r.st], interval[beg - 1]);
// 					printf("ans %d -> %lld\n", -r.nd, ans[-r.nd]);
				}
			}
		}
	}
	
	for (int i=1; i<=q; i++) {
		printf("%lld\n", ans[i]);
	}
	
	return 0;
}