#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb emplace_back
const int maxn = 5e5 + 3;
const int INF = 1e9;
void MAX(int &a, int b) {
if (a < b) a = b;
}
int a[maxn];
struct seg_tree {
vector<int> t, t2, t3, lazy;
seg_tree() {
t.resize(4 * maxn);
t2.resize(4 * maxn);
t3.resize(4 * maxn);
lazy.resize(4 * maxn);
}
void pushdown(int v) {
int val = lazy[v];
MAX(t[2 * v], val);
MAX(t[2 * v + 1], val);
MAX(t3[2 * v], t[2 * v] + t2[2 * v]);
MAX(t3[2 * v + 1], t[2 * v + 1] + t2[2 * v + 1]);
MAX(lazy[2 * v], val);
MAX(lazy[2 * v + 1], val);
}
void build(int v = 1, int l = 1, int r = maxn - 1) {
if (l == r) {
t2[v] = t3[v] = a[l];
return;
}
int m = (l + r) / 2;
build(2 * v, l, m);
build(2 * v + 1, m + 1, r);
t2[v] = max(t2[2 * v], t2[2 * v + 1]);
t3[v] = max(t3[2 * v], t3[2 * v + 1]);
}
void update(int l, int r, int val, int v = 1, int tl = 1, int tr = maxn - 1) {
if (tl > r || tr < l) return;
if (tl >= l && tr <= r) {
MAX(t[v], val);
MAX(lazy[v], val);
MAX(t3[v], t[v] + t2[v]);
return;
}
pushdown(v);
int m = (tl + tr) / 2;
update(l, r, val, 2 * v, tl, m);
update(l, r, val, 2 * v + 1, m + 1, tr);
t3[v] = max(t3[2 * v], t3[2 * v + 1]);
}
int get(int l, int r, int v = 1, int tl = 1, int tr = maxn - 1) {
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t3[v];
pushdown(v);
int m = (tl + tr) / 2;
return max(get(l, r, 2 * v, tl, m), get(l, r, 2 * v + 1, m + 1, tr));
}
} IT;
int n, q, R[maxn], res[maxn];
vector<pair<int,int>> que[maxn];
stack<int> st;
vector<int> L[maxn];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("TRIPLE.INP", "r")) {
freopen("TRIPLE.INP", "r", stdin);
freopen("TRIPLE.OUT", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
IT.build();
st.push(0); a[0] = INF;
for (int i = 1; i <= n; i++) {
while (a[i] > a[st.top()])
st.pop();
L[st.top()].pb(i);
st.push(i);
}
cin >> q;
for (int i = 1, l, r; i <= q; i++) {
cin >> l >> r;
que[l].pb(r, i);
}
stack<int>().swap(st);
st.push(n + 1); a[n + 1] = INF;
for (int i = n; i >= 1; i--) {
while (a[i] > a[st.top()])
st.pop();
R[i] = st.top();
IT.update(2 * R[i] - i, n, a[i] + a[R[i]]);
for (int j : L[i])
IT.update(2 * j - i, n, a[i] + a[j]);
st.push(i);
for (auto it : que[i])
res[it.second] = IT.get(i, it.first);
}
for (int i = 1; i <= q; i++)
cout << res[i] << '\n';
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:77:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen("TRIPLE.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:78:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen("TRIPLE.OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
91992 KB |
Output is correct |
2 |
Correct |
18 ms |
91996 KB |
Output is correct |
3 |
Correct |
18 ms |
91992 KB |
Output is correct |
4 |
Correct |
20 ms |
91996 KB |
Output is correct |
5 |
Correct |
17 ms |
91752 KB |
Output is correct |
6 |
Correct |
17 ms |
91996 KB |
Output is correct |
7 |
Correct |
18 ms |
91996 KB |
Output is correct |
8 |
Correct |
17 ms |
91956 KB |
Output is correct |
9 |
Correct |
18 ms |
91996 KB |
Output is correct |
10 |
Correct |
18 ms |
91996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
91992 KB |
Output is correct |
2 |
Correct |
18 ms |
91996 KB |
Output is correct |
3 |
Correct |
18 ms |
91992 KB |
Output is correct |
4 |
Correct |
20 ms |
91996 KB |
Output is correct |
5 |
Correct |
17 ms |
91752 KB |
Output is correct |
6 |
Correct |
17 ms |
91996 KB |
Output is correct |
7 |
Correct |
18 ms |
91996 KB |
Output is correct |
8 |
Correct |
17 ms |
91956 KB |
Output is correct |
9 |
Correct |
18 ms |
91996 KB |
Output is correct |
10 |
Correct |
18 ms |
91996 KB |
Output is correct |
11 |
Correct |
257 ms |
114948 KB |
Output is correct |
12 |
Correct |
251 ms |
115024 KB |
Output is correct |
13 |
Correct |
258 ms |
115172 KB |
Output is correct |
14 |
Correct |
256 ms |
114896 KB |
Output is correct |
15 |
Correct |
253 ms |
115024 KB |
Output is correct |
16 |
Correct |
252 ms |
114380 KB |
Output is correct |
17 |
Correct |
254 ms |
114328 KB |
Output is correct |
18 |
Correct |
261 ms |
114124 KB |
Output is correct |
19 |
Correct |
262 ms |
115244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
97612 KB |
Output is correct |
2 |
Correct |
119 ms |
98296 KB |
Output is correct |
3 |
Correct |
146 ms |
102972 KB |
Output is correct |
4 |
Correct |
223 ms |
98812 KB |
Output is correct |
5 |
Correct |
219 ms |
98808 KB |
Output is correct |
6 |
Correct |
225 ms |
98640 KB |
Output is correct |
7 |
Correct |
224 ms |
98492 KB |
Output is correct |
8 |
Correct |
220 ms |
98384 KB |
Output is correct |
9 |
Correct |
226 ms |
98900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
91992 KB |
Output is correct |
2 |
Correct |
18 ms |
91996 KB |
Output is correct |
3 |
Correct |
18 ms |
91992 KB |
Output is correct |
4 |
Correct |
20 ms |
91996 KB |
Output is correct |
5 |
Correct |
17 ms |
91752 KB |
Output is correct |
6 |
Correct |
17 ms |
91996 KB |
Output is correct |
7 |
Correct |
18 ms |
91996 KB |
Output is correct |
8 |
Correct |
17 ms |
91956 KB |
Output is correct |
9 |
Correct |
18 ms |
91996 KB |
Output is correct |
10 |
Correct |
18 ms |
91996 KB |
Output is correct |
11 |
Correct |
257 ms |
114948 KB |
Output is correct |
12 |
Correct |
251 ms |
115024 KB |
Output is correct |
13 |
Correct |
258 ms |
115172 KB |
Output is correct |
14 |
Correct |
256 ms |
114896 KB |
Output is correct |
15 |
Correct |
253 ms |
115024 KB |
Output is correct |
16 |
Correct |
252 ms |
114380 KB |
Output is correct |
17 |
Correct |
254 ms |
114328 KB |
Output is correct |
18 |
Correct |
261 ms |
114124 KB |
Output is correct |
19 |
Correct |
262 ms |
115244 KB |
Output is correct |
20 |
Correct |
222 ms |
97612 KB |
Output is correct |
21 |
Correct |
119 ms |
98296 KB |
Output is correct |
22 |
Correct |
146 ms |
102972 KB |
Output is correct |
23 |
Correct |
223 ms |
98812 KB |
Output is correct |
24 |
Correct |
219 ms |
98808 KB |
Output is correct |
25 |
Correct |
225 ms |
98640 KB |
Output is correct |
26 |
Correct |
224 ms |
98492 KB |
Output is correct |
27 |
Correct |
220 ms |
98384 KB |
Output is correct |
28 |
Correct |
226 ms |
98900 KB |
Output is correct |
29 |
Correct |
1109 ms |
131416 KB |
Output is correct |
30 |
Correct |
756 ms |
130008 KB |
Output is correct |
31 |
Correct |
834 ms |
141584 KB |
Output is correct |
32 |
Correct |
1044 ms |
131548 KB |
Output is correct |
33 |
Correct |
1043 ms |
131516 KB |
Output is correct |
34 |
Correct |
1058 ms |
129956 KB |
Output is correct |
35 |
Correct |
1060 ms |
129732 KB |
Output is correct |
36 |
Correct |
1049 ms |
129736 KB |
Output is correct |
37 |
Correct |
1038 ms |
130736 KB |
Output is correct |
38 |
Correct |
842 ms |
133972 KB |
Output is correct |
39 |
Correct |
833 ms |
133972 KB |
Output is correct |
40 |
Correct |
831 ms |
131668 KB |
Output is correct |
41 |
Correct |
807 ms |
131296 KB |
Output is correct |
42 |
Correct |
828 ms |
131244 KB |
Output is correct |
43 |
Correct |
838 ms |
132424 KB |
Output is correct |
44 |
Correct |
910 ms |
134288 KB |
Output is correct |
45 |
Correct |
874 ms |
134112 KB |
Output is correct |
46 |
Correct |
879 ms |
131804 KB |
Output is correct |
47 |
Correct |
871 ms |
131820 KB |
Output is correct |
48 |
Correct |
838 ms |
131700 KB |
Output is correct |
49 |
Correct |
861 ms |
133084 KB |
Output is correct |
50 |
Correct |
920 ms |
134488 KB |
Output is correct |
51 |
Correct |
951 ms |
134736 KB |
Output is correct |
52 |
Correct |
948 ms |
132764 KB |
Output is correct |
53 |
Correct |
931 ms |
132948 KB |
Output is correct |
54 |
Correct |
900 ms |
132768 KB |
Output is correct |
55 |
Correct |
926 ms |
134048 KB |
Output is correct |