#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 update(int u, int l, int r, int ql, int qr, ll val) {
refresh(u, l, r);
if(ql > r || qr < l) return;
if(ql <= l && qr >= r) {
seg[u].lz = val;
refresh(u, l, r);
return;
}
int m = (l+r)/2;
int vl = u*2;
int vr = vl|1;
update(vl, l, m, ql, qr, val);
update(vr, m+1, r, ql, qr, val);
seg[u].mx = max(seg[vl].mx, seg[vr].mx);
seg[u].mxv = max(seg[vl].mxv, seg[vr].mxv);
}
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++;
}
update(1, 1, n, 1, n, input[i]);
// 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:128:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pj < jumps.size() && get<0>(jumps[pj]) <= i) {
~~~^~~~~~~~~~~~~~
jumps.cpp:138:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pq < qv.size() && get<0>(qv[pq]) <= i) {
~~~^~~~~~~~~~~
jumps.cpp:111:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
jumps.cpp:112: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:113:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
jumps.cpp:116: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 |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
384 KB |
Output is correct |
11 |
Correct |
454 ms |
20620 KB |
Output is correct |
12 |
Correct |
460 ms |
20500 KB |
Output is correct |
13 |
Correct |
431 ms |
20312 KB |
Output is correct |
14 |
Correct |
458 ms |
20700 KB |
Output is correct |
15 |
Correct |
442 ms |
20696 KB |
Output is correct |
16 |
Correct |
459 ms |
19952 KB |
Output is correct |
17 |
Correct |
435 ms |
19928 KB |
Output is correct |
18 |
Correct |
452 ms |
19932 KB |
Output is correct |
19 |
Correct |
478 ms |
20508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
20056 KB |
Output is correct |
2 |
Correct |
144 ms |
18316 KB |
Output is correct |
3 |
Correct |
150 ms |
18312 KB |
Output is correct |
4 |
Correct |
263 ms |
20060 KB |
Output is correct |
5 |
Correct |
257 ms |
20280 KB |
Output is correct |
6 |
Correct |
246 ms |
19284 KB |
Output is correct |
7 |
Correct |
252 ms |
19288 KB |
Output is correct |
8 |
Correct |
256 ms |
19288 KB |
Output is correct |
9 |
Correct |
253 ms |
19544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
384 KB |
Output is correct |
11 |
Correct |
454 ms |
20620 KB |
Output is correct |
12 |
Correct |
460 ms |
20500 KB |
Output is correct |
13 |
Correct |
431 ms |
20312 KB |
Output is correct |
14 |
Correct |
458 ms |
20700 KB |
Output is correct |
15 |
Correct |
442 ms |
20696 KB |
Output is correct |
16 |
Correct |
459 ms |
19952 KB |
Output is correct |
17 |
Correct |
435 ms |
19928 KB |
Output is correct |
18 |
Correct |
452 ms |
19932 KB |
Output is correct |
19 |
Correct |
478 ms |
20508 KB |
Output is correct |
20 |
Correct |
259 ms |
20056 KB |
Output is correct |
21 |
Correct |
144 ms |
18316 KB |
Output is correct |
22 |
Correct |
150 ms |
18312 KB |
Output is correct |
23 |
Correct |
263 ms |
20060 KB |
Output is correct |
24 |
Correct |
257 ms |
20280 KB |
Output is correct |
25 |
Correct |
246 ms |
19284 KB |
Output is correct |
26 |
Correct |
252 ms |
19288 KB |
Output is correct |
27 |
Correct |
256 ms |
19288 KB |
Output is correct |
28 |
Correct |
253 ms |
19544 KB |
Output is correct |
29 |
Correct |
1417 ms |
64736 KB |
Output is correct |
30 |
Correct |
1069 ms |
58700 KB |
Output is correct |
31 |
Correct |
1228 ms |
58876 KB |
Output is correct |
32 |
Correct |
1371 ms |
64576 KB |
Output is correct |
33 |
Correct |
1419 ms |
64704 KB |
Output is correct |
34 |
Correct |
1449 ms |
62636 KB |
Output is correct |
35 |
Correct |
1403 ms |
62144 KB |
Output is correct |
36 |
Correct |
1399 ms |
62020 KB |
Output is correct |
37 |
Correct |
1427 ms |
63556 KB |
Output is correct |
38 |
Correct |
1154 ms |
64736 KB |
Output is correct |
39 |
Correct |
1171 ms |
64576 KB |
Output is correct |
40 |
Correct |
1133 ms |
61504 KB |
Output is correct |
41 |
Correct |
1153 ms |
60864 KB |
Output is correct |
42 |
Correct |
1126 ms |
60864 KB |
Output is correct |
43 |
Correct |
1093 ms |
62672 KB |
Output is correct |
44 |
Correct |
1207 ms |
64668 KB |
Output is correct |
45 |
Correct |
1222 ms |
64868 KB |
Output is correct |
46 |
Correct |
1175 ms |
61504 KB |
Output is correct |
47 |
Correct |
1175 ms |
61120 KB |
Output is correct |
48 |
Correct |
1166 ms |
61248 KB |
Output is correct |
49 |
Correct |
1182 ms |
63108 KB |
Output is correct |
50 |
Correct |
1226 ms |
64840 KB |
Output is correct |
51 |
Correct |
1219 ms |
64960 KB |
Output is correct |
52 |
Correct |
1219 ms |
62276 KB |
Output is correct |
53 |
Correct |
1234 ms |
62144 KB |
Output is correct |
54 |
Correct |
1189 ms |
61888 KB |
Output is correct |
55 |
Correct |
1226 ms |
63680 KB |
Output is correct |