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
//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
#pragma GCC optimize "O3"

using namespace std;

const int N = 3e5 + 7;
int n, m, q;
int t[N];
int zap[N][2];
long long dp[N];
long long sum[N];
long long odp[N];

void solve_square() {
	vector<int> zap(q + 1);
	vector<long long> dp(n + 1);
	vector<vector<int>> v(n + 1);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &t[i]);
		sum[i] = sum[i - 1] + t[i];
	}
	int a;
	for (int i = 1; i <= q; ++i) {
		scanf("%d%d", &a, &zap[i]);
		v[a].push_back(i);
	}
	for (int i = 1; i <= n - m + 1; ++i) {
		if (v[i].empty()) {
			continue;
		}
		int mx = i;
		for (int j : v[i]) {
			mx = max(mx, zap[j]);
		}
		int w = min(i + m - 2, mx);
		for (int j = i - 1; j <= w; ++j) {
			dp[j] = 0;
		}
		for (int j = w + 1; j <= mx; ++j) {
			dp[j] = max(dp[j - 1], dp[j - m] + sum[j] - sum[j - m]);
		}
		for (int j : v[i]) {
			odp[j] = dp[zap[j]];
		}
	}
	for (int i = 1; i <= q; ++i) {
		printf("%lld\n", odp[i]);
	}
	exit(0);
}

void wylicz(int l, int r, bool rev) {
	if (r - l + 1 < m) {
		for (int i = l; i <= r; ++i) {
			dp[i] = 0;
		}
		return;
	}
	if (rev) {
		for (int i = r; i > r - m + 1; --i) {
			dp[i] = 0;
		}
		if (r - m + 1 >= l) {
			dp[r - m + 1] = max(0ll, sum[r] - sum[r - m]);
		}
		for (int i = r - m; i >= l; --i) {
			dp[i] = max(dp[i + 1], dp[i + m] + sum[i + m - 1] - sum[i - 1]);
		}
	}
	else {
		for (int i = l; i < l + m - 1; ++i) {
			dp[i] = 0;
		}
		if (l + m - 1 <= r) {
			dp[l + m - 1] = max(0ll, sum[l + m - 1] - sum[l - 1]);
		}
		for (int i = l + m; i <= r; ++i) {
			dp[i] = max(dp[i - 1], dp[i - m] + sum[i] - sum[i - m]);
		}
	}
}

void rek(int l, int r, vector<int> v) {
	if (r - l + 1 < m || v.empty()) {
		return;
	}
	if (l == r) {
		for (int i : v) {
			if (m == 1) {
				odp[i] = max(0, t[l]);
			}
		}
		return;
	}
	int mid = (l + r) / 2;
	vector <int> lf, rg, vv;
	for (int i : v) {
		if (zap[i][1] <= mid) {
			lf.push_back(i);
		}
		else if (zap[i][0] > mid) {
			rg.push_back(i);
		}
		else {
			vv.push_back(i);
		}
	}
	rek(l, mid, lf);
	rek(mid + 1, r, rg);
	if (lf.size() + rg.size() == v.size()) {
		return;
	}
	wylicz(l, mid, true);
	wylicz(mid + 1, r, false);
	for (int i : vv) {
		odp[i] = max(odp[i], dp[zap[i][0]] + dp[zap[i][1]]);
	}
	for (int i = 1; i < m; ++i) { // ile z dodatkowego po lewej stronie
		if (l > mid - i + 1 || r < mid + m - i) {
			continue;
		}
		wylicz(l, mid - i, true);
		wylicz(mid + m - i + 1, r, false);
		for (int j = mid - i + 1; j <= mid + m - i; ++j) {
			dp[j] = 0;
		}
		for (int j : vv) {
			if (zap[j][0] <= mid - i + 1 && mid + m - i <= zap[j][1]) {
				odp[j] = max(odp[j], dp[zap[j][0]] + dp[zap[j][1]] + sum[mid + m - i] - sum[mid - i]);
			}
		}
	}
}

void solve_small() {
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &t[i]);
		sum[i] = sum[i - 1] + t[i];
	}
	vector <int> v;
	for (int i = 1; i <= q; ++i) {
		scanf("%d%d", &zap[i][0], &zap[i][1]);
		v.push_back(i);
	}
	rek(1, n, v);
	for (int i = 1; i <= q; ++i) {
		printf("%lld\n", odp[i]);
	}
	exit(0);
}

int main() {
	scanf("%d%d%d", &n, &m, &q);
	if ((long long)n * n <= (long long)(n * 20 + q) * m) {
		solve_square();
	}
	solve_small();
	return 0;
}