#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct st_node {
ll mxv, mx;
ll lz;
};
const int MAXN = 5e5+5;
const ll INF = 1e18;
int input[MAXN];
vector<tuple<int, int, int> > qv; //{r, l, id}
vector<tuple<int, int, int> > jumps; //{r, l, val}
st_node seg[MAXN*4];
ll ans[MAXN];
int n, q;
void st_process() {
stack<int> st;
input[0] = 2e9;
st.push(0);
for(int i = 1; i <= n; i++) {
while(input[st.top()] < input[i]) st.pop();
if(st.top() != 0) {
int id = st.top();
jumps.push_back({ i+i-id, id, input[i]+input[id] });
}
st.push(i);
}
while(st.size()) st.pop();
input[n+1] = 2e9;
st.push(n+1);
for(int i = n; i >= 1; i--) {
while(input[st.top()] < input[i]) st.pop();
if(st.top() != n+1) {
int id = st.top();
jumps.push_back({ id+id-i, i, input[i]+input[id] });
}
st.push(i);
}
sort(jumps.begin(), jumps.end());
}
void refresh(int u, int l, int r) {
ll val = seg[u].lz;
seg[u].lz = 0;
seg[u].mx = max(seg[u].mx, seg[u].mxv+val);
if(l == r) return;
int vl = u*2;
int vr = vl|1;
seg[vl].lz = max(seg[vl].lz, val);
seg[vr].lz = max(seg[vr].lz, val);
}
ll query(int u, int l, int r, int ql, int qr) {
refresh(u, l, r);
if(ql > r || qr < l) return -INF;
if(ql <= l && qr >= r) return seg[u].mx;
int m = (l+r)/2;
int vl = u*2;
int vr = vl|1;
return max( query(vl, l, m, ql, qr), query(vr, m+1, r, ql, qr) );
}
void build(int u, int l, int r) {
seg[u].mx = seg[u].mxv = -INF;
if(l == r) return;
int m = (l+r)/2;
int vl = u*2;
int vr = vl|1;
build(vl, l, m);
build(vr, m+1, r);
}
void activate(int u, int l, int r, int qi, ll val) {
refresh(u, l, r);
if(qi < l || qi > r) return;
if(l == r) {
seg[u].mxv = max(seg[u].mxv, val);
seg[u].mx = max(seg[u].mx, val);
return;
}
int m = (l+r)/2;
int vl = u*2;
int vr = vl|1;
activate(vl, l, m, qi, val);
activate(vr, m+1, r, qi, val);
seg[u].mx = max(seg[vl].mx, seg[vr].mx);
seg[u].mxv = max(seg[vl].mxv, seg[vr].mxv);
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &input[i]);
scanf("%d", &q);
for(int i = 1; i <= q; i++) {
int a, b;
scanf("%d %d", &a, &b);
qv.push_back({b, a, i});
}
sort(qv.begin(), qv.end());
st_process();
// for(auto jump : jumps) printf("%d %d %d\n", get<0>(jump), get<1>(jump), get<2>(jump));
build(1, 1, n);
int pj = 0, pq = 0;
for(int i = 1; i <= n; i++) {
while(pj < jumps.size() && get<0>(jumps[pj]) <= i) {
int l, r, val;
tie(r, l, val) = jumps[pj];
// printf("activate %d %d\n", l, val);
activate(1, 1, n, l, val);
pj++;
}
refresh(1, 1, n);
seg[1].lz = input[i];
refresh(1, 1, n);
// printf("upd %d\n", input[i]);
// printf("ans %d...\n", i);
while(pq < qv.size() && get<0>(qv[pq]) <= i) {
int l, r, id;
tie(r, l, id) = qv[pq];
ans[id] = query(1, 1, n, l, r);
pq++;
}
}
for(int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:111:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pj < jumps.size() && get<0>(jumps[pj]) <= i) {
~~~^~~~~~~~~~~~~~
jumps.cpp:124:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pq < qv.size() && get<0>(qv[pq]) <= i) {
~~~^~~~~~~~~~~
jumps.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
jumps.cpp:95:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= n; i++) scanf("%d", &input[i]);
~~~~~^~~~~~~~~~~~~~~~~
jumps.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
jumps.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
454 ms |
16988 KB |
Output is correct |
12 |
Correct |
452 ms |
16780 KB |
Output is correct |
13 |
Correct |
447 ms |
16792 KB |
Output is correct |
14 |
Correct |
455 ms |
17112 KB |
Output is correct |
15 |
Correct |
455 ms |
16988 KB |
Output is correct |
16 |
Correct |
453 ms |
16344 KB |
Output is correct |
17 |
Correct |
448 ms |
16348 KB |
Output is correct |
18 |
Correct |
477 ms |
16344 KB |
Output is correct |
19 |
Correct |
455 ms |
16856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
19416 KB |
Output is correct |
2 |
Correct |
140 ms |
17804 KB |
Output is correct |
3 |
Correct |
149 ms |
17804 KB |
Output is correct |
4 |
Correct |
275 ms |
19416 KB |
Output is correct |
5 |
Correct |
254 ms |
19416 KB |
Output is correct |
6 |
Correct |
249 ms |
19416 KB |
Output is correct |
7 |
Correct |
255 ms |
19236 KB |
Output is correct |
8 |
Correct |
262 ms |
19288 KB |
Output is correct |
9 |
Correct |
250 ms |
19432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
454 ms |
16988 KB |
Output is correct |
12 |
Correct |
452 ms |
16780 KB |
Output is correct |
13 |
Correct |
447 ms |
16792 KB |
Output is correct |
14 |
Correct |
455 ms |
17112 KB |
Output is correct |
15 |
Correct |
455 ms |
16988 KB |
Output is correct |
16 |
Correct |
453 ms |
16344 KB |
Output is correct |
17 |
Correct |
448 ms |
16348 KB |
Output is correct |
18 |
Correct |
477 ms |
16344 KB |
Output is correct |
19 |
Correct |
455 ms |
16856 KB |
Output is correct |
20 |
Correct |
278 ms |
19416 KB |
Output is correct |
21 |
Correct |
140 ms |
17804 KB |
Output is correct |
22 |
Correct |
149 ms |
17804 KB |
Output is correct |
23 |
Correct |
275 ms |
19416 KB |
Output is correct |
24 |
Correct |
254 ms |
19416 KB |
Output is correct |
25 |
Correct |
249 ms |
19416 KB |
Output is correct |
26 |
Correct |
255 ms |
19236 KB |
Output is correct |
27 |
Correct |
262 ms |
19288 KB |
Output is correct |
28 |
Correct |
250 ms |
19432 KB |
Output is correct |
29 |
Correct |
1389 ms |
54848 KB |
Output is correct |
30 |
Correct |
1124 ms |
48844 KB |
Output is correct |
31 |
Correct |
1113 ms |
48840 KB |
Output is correct |
32 |
Correct |
1427 ms |
54848 KB |
Output is correct |
33 |
Correct |
1405 ms |
54976 KB |
Output is correct |
34 |
Correct |
1428 ms |
54208 KB |
Output is correct |
35 |
Correct |
1409 ms |
53952 KB |
Output is correct |
36 |
Correct |
1394 ms |
53912 KB |
Output is correct |
37 |
Correct |
1398 ms |
54720 KB |
Output is correct |
38 |
Correct |
1203 ms |
54488 KB |
Output is correct |
39 |
Correct |
1155 ms |
54548 KB |
Output is correct |
40 |
Correct |
1124 ms |
52928 KB |
Output is correct |
41 |
Correct |
1131 ms |
52672 KB |
Output is correct |
42 |
Correct |
1147 ms |
52672 KB |
Output is correct |
43 |
Correct |
1121 ms |
53536 KB |
Output is correct |
44 |
Correct |
1245 ms |
54464 KB |
Output is correct |
45 |
Correct |
1213 ms |
54244 KB |
Output is correct |
46 |
Correct |
1196 ms |
52848 KB |
Output is correct |
47 |
Correct |
1238 ms |
52672 KB |
Output is correct |
48 |
Correct |
1212 ms |
52544 KB |
Output is correct |
49 |
Correct |
1175 ms |
53700 KB |
Output is correct |
50 |
Correct |
1256 ms |
54288 KB |
Output is correct |
51 |
Correct |
1262 ms |
54212 KB |
Output is correct |
52 |
Correct |
1253 ms |
53304 KB |
Output is correct |
53 |
Correct |
1206 ms |
53184 KB |
Output is correct |
54 |
Correct |
1263 ms |
53340 KB |
Output is correct |
55 |
Correct |
1186 ms |
53828 KB |
Output is correct |