#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = (1 << 20) + 1;
const int INF = -1e18;
int iDeb[MAXN], iFin[MAXN], lazyMax[MAXN], maxVal[MAXN], strengh[MAXN],
maxStrength[MAXN];
void buildTree(int iNoeud, int deb, int fin) {
iDeb[iNoeud] = deb, iFin[iNoeud] = fin;
lazyMax[iNoeud] = INF;
if (deb == fin) {
maxStrength[iNoeud] = strengh[deb];
maxVal[iNoeud] = INF;
return;
}
buildTree(2 * iNoeud, deb, (deb + fin) / 2);
buildTree(2 * iNoeud + 1, (deb + fin) / 2 + 1, fin);
maxVal[iNoeud] = INF;
maxStrength[iNoeud] =
max(maxStrength[2 * iNoeud], maxStrength[2 * iNoeud + 1]);
}
void push(int iNoeud) {
if (lazyMax[iNoeud] == INF)
return;
maxVal[iNoeud] = max(maxVal[iNoeud], maxStrength[iNoeud] + lazyMax[iNoeud]);
if (iDeb[iNoeud] < iFin[iNoeud]) {
lazyMax[iNoeud * 2] = max(lazyMax[2 * iNoeud], lazyMax[iNoeud]);
lazyMax[1 + iNoeud * 2] = max(lazyMax[2 * iNoeud], lazyMax[iNoeud]);
}
lazyMax[iNoeud] = INF;
}
void upd(int iNoeud, int deb, int fin, int v) {
push(iNoeud);
if (deb > iFin[iNoeud] or fin < iDeb[iNoeud])
return;
if (iDeb[iNoeud] >= deb and iFin[iNoeud] <= fin) {
lazyMax[iNoeud] = v;
push(iNoeud);
return;
}
upd(2 * iNoeud, deb, fin, v);
upd(1 + 2 * iNoeud, deb, fin, v);
maxVal[iNoeud] = max(maxVal[2 * iNoeud], maxVal[2 * iNoeud + 1]);
}
int query(int iNoeud, int deb, int fin) {
push(iNoeud);
if (deb > iFin[iNoeud] or fin < iDeb[iNoeud])
return INF;
if (iDeb[iNoeud] >= deb and iFin[iNoeud] <= fin)
return maxVal[iNoeud];
return max(query(2 * iNoeud, deb, fin), query(2 * iNoeud + 1, deb, fin));
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i < N; ++i)
cin >> strengh[i];
vector<vector<int>> interessants(N);
vector<pair<int, int>> curRecords;
for (int b = 1; b < N; ++b) {
while (!curRecords.empty() and curRecords.back().first <= strengh[b - 1])
curRecords.pop_back();
curRecords.emplace_back(strengh[b - 1], b - 1);
for (int i = (int)curRecords.size() - 1; i >= 0; --i) {
auto [val, pos] = curRecords[i];
if (b - pos + b < N)
interessants[pos].push_back(b);
if (val >= strengh[b])
break;
}
}
int Q;
cin >> Q;
vector<vector<pair<int, int>>> queries(N);
vector<int> ret(Q);
for (int i = 0; i < Q; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
queries[l].emplace_back(r, i);
}
buildTree(1, 0, N - 1);
for (int a = N - 1; a >= 0; --a) {
for (int b : interessants[a]) {
if (b + b - a < N)
upd(1, 2 * b - a, N - 1, strengh[a] + strengh[b]);
}
for (auto [r, id] : queries[a])
ret[id] = query(1, a + 2, r);
}
for (auto v : ret)
cout << v << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
405 ms |
28216 KB |
Output is correct |
12 |
Correct |
398 ms |
28356 KB |
Output is correct |
13 |
Correct |
412 ms |
28488 KB |
Output is correct |
14 |
Correct |
406 ms |
28356 KB |
Output is correct |
15 |
Correct |
407 ms |
28532 KB |
Output is correct |
16 |
Correct |
407 ms |
27980 KB |
Output is correct |
17 |
Correct |
404 ms |
27836 KB |
Output is correct |
18 |
Correct |
388 ms |
27844 KB |
Output is correct |
19 |
Correct |
400 ms |
28384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
39676 KB |
Output is correct |
2 |
Correct |
129 ms |
38340 KB |
Output is correct |
3 |
Correct |
139 ms |
41384 KB |
Output is correct |
4 |
Correct |
234 ms |
39524 KB |
Output is correct |
5 |
Correct |
234 ms |
39572 KB |
Output is correct |
6 |
Correct |
221 ms |
39440 KB |
Output is correct |
7 |
Correct |
224 ms |
39492 KB |
Output is correct |
8 |
Correct |
225 ms |
39616 KB |
Output is correct |
9 |
Correct |
223 ms |
39536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
405 ms |
28216 KB |
Output is correct |
12 |
Correct |
398 ms |
28356 KB |
Output is correct |
13 |
Correct |
412 ms |
28488 KB |
Output is correct |
14 |
Correct |
406 ms |
28356 KB |
Output is correct |
15 |
Correct |
407 ms |
28532 KB |
Output is correct |
16 |
Correct |
407 ms |
27980 KB |
Output is correct |
17 |
Correct |
404 ms |
27836 KB |
Output is correct |
18 |
Correct |
388 ms |
27844 KB |
Output is correct |
19 |
Correct |
400 ms |
28384 KB |
Output is correct |
20 |
Correct |
226 ms |
39676 KB |
Output is correct |
21 |
Correct |
129 ms |
38340 KB |
Output is correct |
22 |
Correct |
139 ms |
41384 KB |
Output is correct |
23 |
Correct |
234 ms |
39524 KB |
Output is correct |
24 |
Correct |
234 ms |
39572 KB |
Output is correct |
25 |
Correct |
221 ms |
39440 KB |
Output is correct |
26 |
Correct |
224 ms |
39492 KB |
Output is correct |
27 |
Correct |
225 ms |
39616 KB |
Output is correct |
28 |
Correct |
223 ms |
39536 KB |
Output is correct |
29 |
Correct |
1711 ms |
120448 KB |
Output is correct |
30 |
Correct |
1463 ms |
117288 KB |
Output is correct |
31 |
Correct |
1432 ms |
125080 KB |
Output is correct |
32 |
Correct |
1702 ms |
120432 KB |
Output is correct |
33 |
Correct |
1650 ms |
120384 KB |
Output is correct |
34 |
Correct |
1665 ms |
118168 KB |
Output is correct |
35 |
Correct |
1669 ms |
117828 KB |
Output is correct |
36 |
Correct |
1643 ms |
117764 KB |
Output is correct |
37 |
Correct |
1756 ms |
119248 KB |
Output is correct |
38 |
Correct |
1064 ms |
123008 KB |
Output is correct |
39 |
Correct |
1063 ms |
123076 KB |
Output is correct |
40 |
Correct |
1060 ms |
119728 KB |
Output is correct |
41 |
Correct |
1055 ms |
119208 KB |
Output is correct |
42 |
Correct |
1051 ms |
119204 KB |
Output is correct |
43 |
Correct |
1041 ms |
121124 KB |
Output is correct |
44 |
Correct |
1162 ms |
123460 KB |
Output is correct |
45 |
Correct |
1151 ms |
123440 KB |
Output is correct |
46 |
Correct |
1137 ms |
120124 KB |
Output is correct |
47 |
Correct |
1170 ms |
119656 KB |
Output is correct |
48 |
Correct |
1145 ms |
119740 KB |
Output is correct |
49 |
Correct |
1137 ms |
121940 KB |
Output is correct |
50 |
Correct |
1282 ms |
123588 KB |
Output is correct |
51 |
Correct |
1291 ms |
123584 KB |
Output is correct |
52 |
Correct |
1265 ms |
121024 KB |
Output is correct |
53 |
Correct |
1265 ms |
120820 KB |
Output is correct |
54 |
Correct |
1252 ms |
120772 KB |
Output is correct |
55 |
Correct |
1260 ms |
122432 KB |
Output is correct |