#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>*/
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e18;
const ll mod=998244353;
const ll N=5e5+7;
const ld eps=1e-9;
struct node{
ll ans,ab,c;
node operator + (node b){
node x;
x.ans=max(ans,b.ans);
x.ab=max(ab,b.ab);
x.c=max(c,b.c);
return x;
}
};
vector<node> t(N*4);
vll add(N*4);
ll a[N];
void build(ll v,ll tl,ll tr){
if(tl==tr){
t[v]={a[tl],0ll,a[tl]};
return;
}
ll tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]=t[v*2]+t[v*2+1];
}
void push(ll v,ll tl,ll tr){
if(tl!=tr){
chmax(add[v*2],add[v]);
chmax(add[v*2+1],add[v]);
}
chmax(t[v].ab,add[v]);
chmax(t[v].ans,t[v].c+add[v]);
add[v]=0;
}
void upd(ll l,ll r,ll val,ll v,ll tl,ll tr){
push(v,tl,tr);
if(tl>r || tr<l)return ;
if(l<=tl && tr<=r){
add[v]=val;
push(v,tl,tr);
return;
}
ll tm=(tl+tr)/2;
upd(l,r,val,v*2,tl,tm);
upd(l,r,val,v*2+1,tm+1,tr);
t[v]=t[v]+t[v*2];
t[v]=t[v]+t[v*2+1];
}
ll get(ll l,ll r,ll v,ll tl,ll tr){
push(v,tl,tr);
if(tl>r || tr<l)return 0ll;
if(l<=tl && tr<=r){
//cout<<tl<<' '<<tr<<endl;
//cout<<t[v].ans<<' '<<t[v].ab<<' '<<t[v].c<<endl;
return t[v].ans;
}
ll tm=(tl+tr)/2;
return max(get(l,r,v*2,tl,tm),get(l,r,v*2+1,tm+1,tr));
}
void solve(){
ll i,j;
ll n,q;
cin>>n;
for(i=1;i<=n;i++) cin>>a[i];
build(1,1,n);
cin>>q;
vector<pll> qq(n+5);
for(i=0;i<q;i++){
ll l,r;
cin>>l>>r;
qq[l].pb({r,i});
}
ll ans[q];
pll cur;
for(i=n;i>0;i--){
while(cur.sz && cur.bc.fr<a[i]){
ll A=i,B=cur.bc.sc;
//cout<<a[A]<<' '<<a[B]<<endl;
if(2*B-A<=n)upd(2*B-A,n,a[i]+a[B],1,1,n);
cur.pob;
}
if(cur.sz){
ll A=i,B=cur.bc.sc;
//cout<<a[A]<<' '<<a[B]<<endl;
if(2*B-A<=n)upd(2*B-A,n,a[i]+a[B],1,1,n);
}
cur.pb({a[i],i});
for(auto j : qq[i]){
ans[j.sc]=get(i,j.fr,1,1,n);
}
}
for(i=0;i<q;i++) cout<<ans[i]<<endl;
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
*/
Compilation message
jumps.cpp: In function 'void solve()':
jumps.cpp:103:10: warning: unused variable 'j' [-Wunused-variable]
103 | ll i,j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
63068 KB |
Output is correct |
2 |
Correct |
21 ms |
63068 KB |
Output is correct |
3 |
Correct |
20 ms |
62988 KB |
Output is correct |
4 |
Correct |
18 ms |
63068 KB |
Output is correct |
5 |
Correct |
20 ms |
63068 KB |
Output is correct |
6 |
Correct |
21 ms |
63060 KB |
Output is correct |
7 |
Correct |
22 ms |
63068 KB |
Output is correct |
8 |
Correct |
20 ms |
63068 KB |
Output is correct |
9 |
Correct |
21 ms |
63064 KB |
Output is correct |
10 |
Correct |
21 ms |
63068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
63068 KB |
Output is correct |
2 |
Correct |
21 ms |
63068 KB |
Output is correct |
3 |
Correct |
20 ms |
62988 KB |
Output is correct |
4 |
Correct |
18 ms |
63068 KB |
Output is correct |
5 |
Correct |
20 ms |
63068 KB |
Output is correct |
6 |
Correct |
21 ms |
63060 KB |
Output is correct |
7 |
Correct |
22 ms |
63068 KB |
Output is correct |
8 |
Correct |
20 ms |
63068 KB |
Output is correct |
9 |
Correct |
21 ms |
63064 KB |
Output is correct |
10 |
Correct |
21 ms |
63068 KB |
Output is correct |
11 |
Correct |
258 ms |
85328 KB |
Output is correct |
12 |
Correct |
255 ms |
85656 KB |
Output is correct |
13 |
Correct |
258 ms |
85328 KB |
Output is correct |
14 |
Correct |
247 ms |
85328 KB |
Output is correct |
15 |
Correct |
257 ms |
85588 KB |
Output is correct |
16 |
Correct |
259 ms |
84820 KB |
Output is correct |
17 |
Correct |
234 ms |
84856 KB |
Output is correct |
18 |
Correct |
272 ms |
84560 KB |
Output is correct |
19 |
Correct |
251 ms |
85472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
69632 KB |
Output is correct |
2 |
Correct |
88 ms |
75076 KB |
Output is correct |
3 |
Correct |
94 ms |
70992 KB |
Output is correct |
4 |
Correct |
146 ms |
70996 KB |
Output is correct |
5 |
Correct |
145 ms |
70992 KB |
Output is correct |
6 |
Correct |
142 ms |
70436 KB |
Output is correct |
7 |
Correct |
136 ms |
70320 KB |
Output is correct |
8 |
Correct |
143 ms |
70320 KB |
Output is correct |
9 |
Correct |
143 ms |
70652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
63068 KB |
Output is correct |
2 |
Correct |
21 ms |
63068 KB |
Output is correct |
3 |
Correct |
20 ms |
62988 KB |
Output is correct |
4 |
Correct |
18 ms |
63068 KB |
Output is correct |
5 |
Correct |
20 ms |
63068 KB |
Output is correct |
6 |
Correct |
21 ms |
63060 KB |
Output is correct |
7 |
Correct |
22 ms |
63068 KB |
Output is correct |
8 |
Correct |
20 ms |
63068 KB |
Output is correct |
9 |
Correct |
21 ms |
63064 KB |
Output is correct |
10 |
Correct |
21 ms |
63068 KB |
Output is correct |
11 |
Correct |
258 ms |
85328 KB |
Output is correct |
12 |
Correct |
255 ms |
85656 KB |
Output is correct |
13 |
Correct |
258 ms |
85328 KB |
Output is correct |
14 |
Correct |
247 ms |
85328 KB |
Output is correct |
15 |
Correct |
257 ms |
85588 KB |
Output is correct |
16 |
Correct |
259 ms |
84820 KB |
Output is correct |
17 |
Correct |
234 ms |
84856 KB |
Output is correct |
18 |
Correct |
272 ms |
84560 KB |
Output is correct |
19 |
Correct |
251 ms |
85472 KB |
Output is correct |
20 |
Correct |
143 ms |
69632 KB |
Output is correct |
21 |
Correct |
88 ms |
75076 KB |
Output is correct |
22 |
Correct |
94 ms |
70992 KB |
Output is correct |
23 |
Correct |
146 ms |
70996 KB |
Output is correct |
24 |
Correct |
145 ms |
70992 KB |
Output is correct |
25 |
Correct |
142 ms |
70436 KB |
Output is correct |
26 |
Correct |
136 ms |
70320 KB |
Output is correct |
27 |
Correct |
143 ms |
70320 KB |
Output is correct |
28 |
Correct |
143 ms |
70652 KB |
Output is correct |
29 |
Correct |
940 ms |
111700 KB |
Output is correct |
30 |
Correct |
693 ms |
119476 KB |
Output is correct |
31 |
Correct |
736 ms |
111624 KB |
Output is correct |
32 |
Correct |
827 ms |
111552 KB |
Output is correct |
33 |
Correct |
901 ms |
111700 KB |
Output is correct |
34 |
Correct |
832 ms |
109180 KB |
Output is correct |
35 |
Correct |
844 ms |
108908 KB |
Output is correct |
36 |
Correct |
827 ms |
108912 KB |
Output is correct |
37 |
Correct |
798 ms |
110488 KB |
Output is correct |
38 |
Correct |
608 ms |
114156 KB |
Output is correct |
39 |
Correct |
616 ms |
114168 KB |
Output is correct |
40 |
Correct |
586 ms |
110928 KB |
Output is correct |
41 |
Correct |
599 ms |
110500 KB |
Output is correct |
42 |
Correct |
590 ms |
110416 KB |
Output is correct |
43 |
Correct |
555 ms |
112228 KB |
Output is correct |
44 |
Correct |
618 ms |
114512 KB |
Output is correct |
45 |
Correct |
649 ms |
114532 KB |
Output is correct |
46 |
Correct |
643 ms |
111264 KB |
Output is correct |
47 |
Correct |
700 ms |
111020 KB |
Output is correct |
48 |
Correct |
654 ms |
110868 KB |
Output is correct |
49 |
Correct |
638 ms |
112900 KB |
Output is correct |
50 |
Correct |
701 ms |
114768 KB |
Output is correct |
51 |
Correct |
690 ms |
114772 KB |
Output is correct |
52 |
Correct |
677 ms |
112476 KB |
Output is correct |
53 |
Correct |
675 ms |
111952 KB |
Output is correct |
54 |
Correct |
753 ms |
112000 KB |
Output is correct |
55 |
Correct |
708 ms |
113488 KB |
Output is correct |