//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 5e5+10;
int n;
int a[maxn];
deque <int> q;
vector <int> good[maxn];
pii tree[maxn*4];
int add[maxn*4];
vector <pair<pii,int> > que;
int ans[maxn];
void init(int c,int cl,int cr) {
if (cl==cr) tree[c] = {a[cl],a[cl]},add[c] = 0;
else {
int mid=cl+cr>>1;
init(c<<1,cl,mid);
init(c<<1|1,mid+1,cr);
int tmp = max(tree[c<<1].fi,tree[c<<1|1].fi);
tree[c] = {tmp,tmp};
}
}
void push_down(int c) {
tree[c<<1].fi = max(tree[c<<1].fi,tree[c<<1].se+add[c]);
add[c<<1] = max(add[c<<1],add[c]);
tree[c<<1|1].fi = max(tree[c<<1|1].fi,tree[c<<1|1].se+add[c]);
add[c<<1|1] = max(add[c<<1|1],add[c]);
add[c] = 0;
}
void update(int c,int cl,int cr,int l,int r,int val) {
if (l<=cl and cr<=r) {
tree[c].fi = max(tree[c].fi,tree[c].se+val);
add[c] = max(add[c],val);
return;
}
int mid=cl+cr>>1;
if (add[c]!=0) push_down(c);
if (l<=mid) update(c<<1,cl,mid,l,r,val);
if (r>mid) update(c<<1|1,mid+1,cr,l,r,val);
tree[c].fi = max(tree[c<<1].fi,tree[c<<1|1].fi);
return;
}
int query(int c,int cl,int cr,int l,int r) {
if (l<=cl and cr<=r) return tree[c].fi;
int mid=cl+cr>>1;
if (add[c]!=0) push_down(c);
int ret = 0;
if (l<=mid) ret = query(c<<1,cl,mid,l,r);
if (r>mid) ret = max(ret,query(c<<1|1,mid+1,cr,l,r));
return ret;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n;
rep(i,1,n+1) cin>>a[i];
for (int i=n;i>0;i--) {
while (!q.empty() and a[q.back()]<=a[i]) good[i].pb(q.back()),q.pop_back();
if (!q.empty()) good[i].pb(q.back());
q.push_back(i);
}
// rep(i,1,n+1) for (auto it:good[i]) cout<<i<<" "<<it<<endl;
init(1,1,n);
int _;cin>>_;
rep(i,0,_) {
int l,r;
cin>>l>>r;
que.pb({{l,r},i});
}
sort(ALL(que));reverse(ALL(que));
int l=n;
rep(i,0,_) {
while (l>=que[i].fi.fi) {
for (auto r:good[l]) if (r+r-l<=n) update(1,1,n,r+r-l,n,a[l]+a[r]);
l--;
}
ans[que[i].se] = query(1,1,n,que[i].fi.fi,que[i].fi.se);
}
rep(i,0,_) cout<<ans[i]<<"\n";
return 0;
}
Compilation message
jumps.cpp: In function 'void init(int, int, int)':
jumps.cpp:38:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid=cl+cr>>1;
| ~~^~~
jumps.cpp: In function 'void update(int, int, int, int, int, int)':
jumps.cpp:61:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
61 | int mid=cl+cr>>1;
| ~~^~~
jumps.cpp: In function 'int query(int, int, int, int, int)':
jumps.cpp:71:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid=cl+cr>>1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12008 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
12076 KB |
Output is correct |
7 |
Correct |
6 ms |
12080 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
7 ms |
12076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12008 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
12076 KB |
Output is correct |
7 |
Correct |
6 ms |
12080 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
7 ms |
12076 KB |
Output is correct |
11 |
Correct |
270 ms |
27692 KB |
Output is correct |
12 |
Correct |
255 ms |
27564 KB |
Output is correct |
13 |
Correct |
252 ms |
27584 KB |
Output is correct |
14 |
Correct |
255 ms |
27516 KB |
Output is correct |
15 |
Correct |
252 ms |
27688 KB |
Output is correct |
16 |
Correct |
257 ms |
27020 KB |
Output is correct |
17 |
Correct |
273 ms |
26944 KB |
Output is correct |
18 |
Correct |
256 ms |
26932 KB |
Output is correct |
19 |
Correct |
260 ms |
27564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
25480 KB |
Output is correct |
2 |
Correct |
69 ms |
27712 KB |
Output is correct |
3 |
Correct |
65 ms |
26956 KB |
Output is correct |
4 |
Correct |
109 ms |
27212 KB |
Output is correct |
5 |
Correct |
108 ms |
27192 KB |
Output is correct |
6 |
Correct |
104 ms |
26484 KB |
Output is correct |
7 |
Correct |
106 ms |
26396 KB |
Output is correct |
8 |
Correct |
106 ms |
26448 KB |
Output is correct |
9 |
Correct |
108 ms |
26788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12008 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
12076 KB |
Output is correct |
7 |
Correct |
6 ms |
12080 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
6 ms |
12080 KB |
Output is correct |
10 |
Correct |
7 ms |
12076 KB |
Output is correct |
11 |
Correct |
270 ms |
27692 KB |
Output is correct |
12 |
Correct |
255 ms |
27564 KB |
Output is correct |
13 |
Correct |
252 ms |
27584 KB |
Output is correct |
14 |
Correct |
255 ms |
27516 KB |
Output is correct |
15 |
Correct |
252 ms |
27688 KB |
Output is correct |
16 |
Correct |
257 ms |
27020 KB |
Output is correct |
17 |
Correct |
273 ms |
26944 KB |
Output is correct |
18 |
Correct |
256 ms |
26932 KB |
Output is correct |
19 |
Correct |
260 ms |
27564 KB |
Output is correct |
20 |
Correct |
104 ms |
25480 KB |
Output is correct |
21 |
Correct |
69 ms |
27712 KB |
Output is correct |
22 |
Correct |
65 ms |
26956 KB |
Output is correct |
23 |
Correct |
109 ms |
27212 KB |
Output is correct |
24 |
Correct |
108 ms |
27192 KB |
Output is correct |
25 |
Correct |
104 ms |
26484 KB |
Output is correct |
26 |
Correct |
106 ms |
26396 KB |
Output is correct |
27 |
Correct |
106 ms |
26448 KB |
Output is correct |
28 |
Correct |
108 ms |
26788 KB |
Output is correct |
29 |
Correct |
658 ms |
66388 KB |
Output is correct |
30 |
Correct |
553 ms |
67876 KB |
Output is correct |
31 |
Correct |
567 ms |
65932 KB |
Output is correct |
32 |
Correct |
661 ms |
66448 KB |
Output is correct |
33 |
Correct |
655 ms |
66576 KB |
Output is correct |
34 |
Correct |
652 ms |
64112 KB |
Output is correct |
35 |
Correct |
633 ms |
63904 KB |
Output is correct |
36 |
Correct |
645 ms |
63804 KB |
Output is correct |
37 |
Correct |
666 ms |
65256 KB |
Output is correct |
38 |
Correct |
529 ms |
66460 KB |
Output is correct |
39 |
Correct |
532 ms |
66564 KB |
Output is correct |
40 |
Correct |
512 ms |
63192 KB |
Output is correct |
41 |
Correct |
504 ms |
62572 KB |
Output is correct |
42 |
Correct |
532 ms |
62704 KB |
Output is correct |
43 |
Correct |
519 ms |
64312 KB |
Output is correct |
44 |
Correct |
568 ms |
66552 KB |
Output is correct |
45 |
Correct |
550 ms |
66548 KB |
Output is correct |
46 |
Correct |
539 ms |
63264 KB |
Output is correct |
47 |
Correct |
551 ms |
62960 KB |
Output is correct |
48 |
Correct |
528 ms |
62872 KB |
Output is correct |
49 |
Correct |
552 ms |
65084 KB |
Output is correct |
50 |
Correct |
588 ms |
66656 KB |
Output is correct |
51 |
Correct |
595 ms |
66548 KB |
Output is correct |
52 |
Correct |
596 ms |
64016 KB |
Output is correct |
53 |
Correct |
561 ms |
63840 KB |
Output is correct |
54 |
Correct |
595 ms |
63744 KB |
Output is correct |
55 |
Correct |
572 ms |
65412 KB |
Output is correct |