#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int MAXN=5e5+10;
#define endl '\n'
struct Query {
int l;
int r;
int ind;
bool friend operator < (Query a, Query b) {
return a.l>b.l;
}
};
struct Jump {
int a;
int b;
int sum;
bool friend operator < (Jump a, Jump b) {
return a.a>b.a;
}
};
Query que[MAXN];
vector <Jump> v;
stack <int> st;
int tree[4*MAXN], lazy[4*MAXN], mx[4*MAXN];
int a[MAXN], ans[MAXN];
void build_tree(int node, int l, int r) {
lazy[node]=0;
if (l==r) {
tree[node]=a[l];
mx[node]=a[l];
return;
}
int mid=(l+r)/2;
build_tree(node*2,l,mid);
build_tree(node*2+1,mid+1,r);
tree[node]=max(tree[node*2],tree[node*2+1]);
mx[node]=tree[node];
}
void add_lazy(int node, int ch) {
if (ch<=lazy[node]) return;
lazy[node]=ch;
tree[node]=max(tree[node],mx[node]+ch);
}
void push_lazy(int node, int l, int r) {
if (l!=r) {
add_lazy(node*2,lazy[node]);
add_lazy(node*2+1,lazy[node]);
}
lazy[node]=0;
}
void update(int node, int l, int r, int ql, int qr, int ch) {
if (ql>r || qr<l) return;
push_lazy(node,l,r);
if (l>=ql && r<=qr) {
add_lazy(node,ch);
return;
}
int mid=(l+r)/2;
update(node*2,l,mid,ql,qr,ch);
update(node*2+1,mid+1,r,ql,qr,ch);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
int query(int node, int l, int r, int ql, int qr) {
if (ql>r || qr<l) return 0;
push_lazy(node,l,r);
if (l>=ql && r<=qr) {
//cout << l << ' ' << r << ' ' << tree[node] << endl;
return tree[node];
}
int mid=(l+r)/2;
return max(query(node*2,l,mid,ql,qr),query(node*2+1,mid+1,r,ql,qr));
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n;
for (int i=1;i<=n;i++) {
cin >> a[i];
while (!st.empty() && a[i]>a[st.top()]) st.pop();
if (!st.empty()) v.push_back({st.top(),i,a[i]+a[st.top()]});
st.push(i);
}
while (!st.empty()) st.pop();
for (int i=n;i>=1;i--) {
while (!st.empty() && a[i]>a[st.top()]) st.pop();
if (!st.empty()) v.push_back({i,st.top(),a[i]+a[st.top()]});
st.push(i);
}
sort(v.begin(),v.end());
cin >> q;
for (int i=0;i<q;i++) {
cin >> que[i].l >> que[i].r;
que[i].ind=i;
}
sort(que,que+q);
int curq, curv;
curq=curv=0;
build_tree(1,1,n);
for (int i=n-2;i>=1;i--) {
while (curv<v.size() && v[curv].a>=i) {
int gr=v[curv].b*2-v[curv].a;
if (gr<=n) {
//cout << v[curv].a << ' ' << v[curv].b << ' ' << gr << endl;
update(1,1,n,gr,n,v[curv].sum);
}
curv++;
}
while (curq<q && que[curq].l==i) {
ans[que[curq].ind]=query(1,1,n,que[curq].l,que[curq].r);
curq++;
}
if (curq==q) break;
}
for (int i=0;i<q;i++) cout << ans[i] << endl;
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:104:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Jump>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | while (curv<v.size() && v[curv].a>=i) {
| ~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10712 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10712 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
253 ms |
25008 KB |
Output is correct |
12 |
Correct |
241 ms |
25044 KB |
Output is correct |
13 |
Correct |
278 ms |
24924 KB |
Output is correct |
14 |
Correct |
254 ms |
25148 KB |
Output is correct |
15 |
Correct |
244 ms |
25112 KB |
Output is correct |
16 |
Correct |
246 ms |
24548 KB |
Output is correct |
17 |
Correct |
244 ms |
24356 KB |
Output is correct |
18 |
Correct |
249 ms |
24344 KB |
Output is correct |
19 |
Correct |
243 ms |
25108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
136 ms |
23484 KB |
Output is correct |
2 |
Correct |
77 ms |
21728 KB |
Output is correct |
3 |
Correct |
80 ms |
21992 KB |
Output is correct |
4 |
Correct |
148 ms |
23544 KB |
Output is correct |
5 |
Correct |
137 ms |
24204 KB |
Output is correct |
6 |
Correct |
135 ms |
22972 KB |
Output is correct |
7 |
Correct |
136 ms |
23992 KB |
Output is correct |
8 |
Correct |
134 ms |
23748 KB |
Output is correct |
9 |
Correct |
132 ms |
24248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10712 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
253 ms |
25008 KB |
Output is correct |
12 |
Correct |
241 ms |
25044 KB |
Output is correct |
13 |
Correct |
278 ms |
24924 KB |
Output is correct |
14 |
Correct |
254 ms |
25148 KB |
Output is correct |
15 |
Correct |
244 ms |
25112 KB |
Output is correct |
16 |
Correct |
246 ms |
24548 KB |
Output is correct |
17 |
Correct |
244 ms |
24356 KB |
Output is correct |
18 |
Correct |
249 ms |
24344 KB |
Output is correct |
19 |
Correct |
243 ms |
25108 KB |
Output is correct |
20 |
Correct |
136 ms |
23484 KB |
Output is correct |
21 |
Correct |
77 ms |
21728 KB |
Output is correct |
22 |
Correct |
80 ms |
21992 KB |
Output is correct |
23 |
Correct |
148 ms |
23544 KB |
Output is correct |
24 |
Correct |
137 ms |
24204 KB |
Output is correct |
25 |
Correct |
135 ms |
22972 KB |
Output is correct |
26 |
Correct |
136 ms |
23992 KB |
Output is correct |
27 |
Correct |
134 ms |
23748 KB |
Output is correct |
28 |
Correct |
132 ms |
24248 KB |
Output is correct |
29 |
Correct |
794 ms |
55264 KB |
Output is correct |
30 |
Correct |
616 ms |
51332 KB |
Output is correct |
31 |
Correct |
541 ms |
51384 KB |
Output is correct |
32 |
Correct |
804 ms |
55476 KB |
Output is correct |
33 |
Correct |
789 ms |
55256 KB |
Output is correct |
34 |
Correct |
782 ms |
53168 KB |
Output is correct |
35 |
Correct |
769 ms |
53172 KB |
Output is correct |
36 |
Correct |
789 ms |
53324 KB |
Output is correct |
37 |
Correct |
825 ms |
54200 KB |
Output is correct |
38 |
Correct |
634 ms |
55340 KB |
Output is correct |
39 |
Correct |
646 ms |
55216 KB |
Output is correct |
40 |
Correct |
698 ms |
51892 KB |
Output is correct |
41 |
Correct |
667 ms |
51348 KB |
Output is correct |
42 |
Correct |
636 ms |
51408 KB |
Output is correct |
43 |
Correct |
621 ms |
53708 KB |
Output is correct |
44 |
Correct |
666 ms |
55364 KB |
Output is correct |
45 |
Correct |
663 ms |
55728 KB |
Output is correct |
46 |
Correct |
636 ms |
52400 KB |
Output is correct |
47 |
Correct |
650 ms |
51684 KB |
Output is correct |
48 |
Correct |
641 ms |
51680 KB |
Output is correct |
49 |
Correct |
646 ms |
53800 KB |
Output is correct |
50 |
Correct |
719 ms |
55296 KB |
Output is correct |
51 |
Correct |
690 ms |
55448 KB |
Output is correct |
52 |
Correct |
720 ms |
53168 KB |
Output is correct |
53 |
Correct |
697 ms |
52840 KB |
Output is correct |
54 |
Correct |
688 ms |
52928 KB |
Output is correct |
55 |
Correct |
695 ms |
54356 KB |
Output is correct |