#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int N = 5e5+10;
const int INF = INT_MAX;
const int mod = 1e9+7;
int n,a[N];
int t;
vector<pair<int,int>> q[N];
stack<int> st;
ll ans[N],lazy[4*N];
struct Node{
ll best,mx,Mx;
Node() {};
Node(ll _best, ll _mx, ll _Mx) {
best = _best;
mx = _mx;
Mx = _Mx;
};
}tree[4*N];
Node operator + (Node u, Node v) {
Node res = u;
res.best = max({res.best,u.mx+v.Mx,v.best});
res.mx = max(res.mx,v.mx);
res.Mx = max(res.Mx,v.Mx);
return res;
}
void build(int id = 1, int l = 1, int r = n) {
if(l == r) {
tree[id].Mx = a[l];
tree[id].best = a[l];
return;
}
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid+1,r);
tree[id] = tree[id << 1] + tree[id << 1 | 1];
}
void down(int id, int l, int r) {
ll t = lazy[id];
if(t == 0) {
return;
}
tree[id].mx = max(tree[id].mx,t);
tree[id].best = tree[id].mx+tree[id].Mx;
if(l < r) {
lazy[id << 1] = max(lazy[id << 1],t);
lazy[id << 1 | 1] = max(lazy[id << 1 | 1],t);
}
t = 0;
}
void update(int pos, ll val, int id = 1, int l = 1, int r = n) {
// down(id,l,r);
// if(pos > n) {
// re
// }
if(l == r) {
tree[id].mx = max(tree[id].mx,val);
tree[id].best = tree[id].mx + tree[id].Mx;
return;
}
int mid = l + r >> 1;
if(pos <= mid) {
update(pos,val,id << 1,l,mid);
}
else {
update(pos,val,id << 1 | 1,mid+1,r);
}
tree[id] = tree[id << 1] + tree[id << 1 | 1];
}
Node get(int u, int v, int id = 1, int l = 1, int r = n) {
if(u > v || l > r || r < u || v < l) {
return {0,0,0};
}
if(u <= l && r <= v) {
return tree[id];
}
int mid = l + r >> 1;
return get(u,v,id << 1,l,mid)+get(u,v,id << 1 | 1,mid+1,r);
}
signed main()
{
if (fopen("triplejump.inp", "r")) {
freopen("triplejump.inp", "r", stdin);
freopen("triplejump.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
cin >> t;
for(int i = 1 ; i <= t ; i++) {
int l,r; cin >> l >> r;
q[l].push_back({r,i});
}
build();
for(int i = n ; i > 0 ; i--) {
while(!st.empty() && a[st.top()] < a[i]) {
if(2*st.top()-i <= n) {
update(2*st.top()-i,a[i]+a[st.top()]);
}
st.pop();
}
if(!st.empty()) {
if(2*st.top()-i <= n) {
update(2*st.top()-i,a[i]+a[st.top()]);
}
}
st.push(i);
for(auto p:q[i]) {
ans[p.second] = get(i,p.first).best;
}
}
for(int i = 1 ; i <= t ; i++) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:42:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(int, long long int, int, int, int)':
jumps.cpp:72:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'Node get(int, int, int, int, int)':
jumps.cpp:89:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("triplejump.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen("triplejump.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16872 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16852 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16872 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16852 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
234 ms |
35668 KB |
Output is correct |
12 |
Correct |
237 ms |
35412 KB |
Output is correct |
13 |
Correct |
237 ms |
35560 KB |
Output is correct |
14 |
Correct |
233 ms |
35408 KB |
Output is correct |
15 |
Correct |
234 ms |
35412 KB |
Output is correct |
16 |
Correct |
243 ms |
34860 KB |
Output is correct |
17 |
Correct |
236 ms |
34836 KB |
Output is correct |
18 |
Correct |
235 ms |
34896 KB |
Output is correct |
19 |
Correct |
239 ms |
35412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
30172 KB |
Output is correct |
2 |
Correct |
72 ms |
31872 KB |
Output is correct |
3 |
Correct |
71 ms |
31060 KB |
Output is correct |
4 |
Correct |
122 ms |
31076 KB |
Output is correct |
5 |
Correct |
125 ms |
31320 KB |
Output is correct |
6 |
Correct |
118 ms |
30292 KB |
Output is correct |
7 |
Correct |
126 ms |
30292 KB |
Output is correct |
8 |
Correct |
119 ms |
30292 KB |
Output is correct |
9 |
Correct |
121 ms |
30804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
3 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16872 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16852 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
234 ms |
35668 KB |
Output is correct |
12 |
Correct |
237 ms |
35412 KB |
Output is correct |
13 |
Correct |
237 ms |
35560 KB |
Output is correct |
14 |
Correct |
233 ms |
35408 KB |
Output is correct |
15 |
Correct |
234 ms |
35412 KB |
Output is correct |
16 |
Correct |
243 ms |
34860 KB |
Output is correct |
17 |
Correct |
236 ms |
34836 KB |
Output is correct |
18 |
Correct |
235 ms |
34896 KB |
Output is correct |
19 |
Correct |
239 ms |
35412 KB |
Output is correct |
20 |
Correct |
121 ms |
30172 KB |
Output is correct |
21 |
Correct |
72 ms |
31872 KB |
Output is correct |
22 |
Correct |
71 ms |
31060 KB |
Output is correct |
23 |
Correct |
122 ms |
31076 KB |
Output is correct |
24 |
Correct |
125 ms |
31320 KB |
Output is correct |
25 |
Correct |
118 ms |
30292 KB |
Output is correct |
26 |
Correct |
126 ms |
30292 KB |
Output is correct |
27 |
Correct |
119 ms |
30292 KB |
Output is correct |
28 |
Correct |
121 ms |
30804 KB |
Output is correct |
29 |
Correct |
787 ms |
69708 KB |
Output is correct |
30 |
Correct |
641 ms |
71484 KB |
Output is correct |
31 |
Correct |
630 ms |
69596 KB |
Output is correct |
32 |
Correct |
797 ms |
69712 KB |
Output is correct |
33 |
Correct |
788 ms |
69508 KB |
Output is correct |
34 |
Correct |
787 ms |
67356 KB |
Output is correct |
35 |
Correct |
776 ms |
67040 KB |
Output is correct |
36 |
Correct |
775 ms |
66900 KB |
Output is correct |
37 |
Correct |
770 ms |
68688 KB |
Output is correct |
38 |
Correct |
598 ms |
75348 KB |
Output is correct |
39 |
Correct |
600 ms |
75344 KB |
Output is correct |
40 |
Correct |
580 ms |
71896 KB |
Output is correct |
41 |
Correct |
576 ms |
71504 KB |
Output is correct |
42 |
Correct |
582 ms |
71468 KB |
Output is correct |
43 |
Correct |
588 ms |
73092 KB |
Output is correct |
44 |
Correct |
621 ms |
74580 KB |
Output is correct |
45 |
Correct |
626 ms |
74736 KB |
Output is correct |
46 |
Correct |
609 ms |
71628 KB |
Output is correct |
47 |
Correct |
616 ms |
71140 KB |
Output is correct |
48 |
Correct |
616 ms |
70988 KB |
Output is correct |
49 |
Correct |
616 ms |
73044 KB |
Output is correct |
50 |
Correct |
724 ms |
72696 KB |
Output is correct |
51 |
Correct |
672 ms |
72688 KB |
Output is correct |
52 |
Correct |
664 ms |
70280 KB |
Output is correct |
53 |
Correct |
665 ms |
70028 KB |
Output is correct |
54 |
Correct |
665 ms |
70220 KB |
Output is correct |
55 |
Correct |
671 ms |
71764 KB |
Output is correct |