//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,q;
ll arr[500005];
vector<int> stk;
vector<ii> good;
struct Q{
int l,r;
int idx;
Q(int a,int b,int c){
l=a,r=b,idx=c;
}
};
vector<Q> queries;
ll ans[500005];
struct dat{
ll ab=-1e18,c=0,abc=0;
};
dat merge(dat i,dat j){
dat res;
res.c=max(i.c,j.c);
res.ab=max(i.ab,j.ab);
res.abc=MAX(i.abc,j.abc,i.ab+j.c);
return res;
}
struct node{
int s,e,m;
dat val;
node *l,*r;
node (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new node(s,m);
r=new node(m+1,e);
val=merge(l->val,r->val);
}
else{
val.c=arr[s];
}
}
void update(int i,ll j){
if (s==e) val.ab=max(val.ab,j);
else{
if (i<=m) l->update(i,j);
else r->update(i,j);
val=merge(l->val,r->val);
}
}
dat query(int i,int j){
if (s==i && e==j) return val;
else if (j<=m) return l->query(i,j);
else if (m<i) return r->query(i,j);
else return merge(l->query(i,m),r->query(m+1,j));
}
} *root;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
rep(x,0,n) cin>>arr[x];
root=new node(0,500005);
rep(x,0,n){
while (!stk.empty() && arr[stk.back()]<=arr[x]){
good.push_back({stk.back(),x});
stk.pop_back();
}
if (!stk.empty()) good.push_back({stk.back(),x});
stk.push_back(x);
}
cin>>q;
int a,b;
rep(x,0,q){
cin>>a>>b;
a--,b--;
queries.push_back(Q(a,b,x));
}
sort(all(good),[](ii i,ii j){
return i.fi<j.fi;
});
sort(all(queries),[](Q &i,Q &j){
return i.l>j.l;
});
for (auto &it:queries){
while (!good.empty() && it.l<=good.back().fi){
int pos=good.back().se*2-good.back().fi-1;
//cout<<pos<<" "<<good.back().fi<<" "<<good.back().se<<endl;
if (pos<200005) root->update(pos,arr[good.back().fi]+arr[good.back().se]);
good.pop_back();
}
ans[it.idx]=root->query(0,it.r).abc;
}
rep(x,0,q) cout<<ans[x]<<endl;
}
Compilation message
jumps.cpp: In constructor 'node::node(int, int)':
jumps.cpp:74:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
s=_s,e=_e,m=s+e>>1;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
62968 KB |
Output is correct |
2 |
Correct |
60 ms |
62968 KB |
Output is correct |
3 |
Correct |
58 ms |
62968 KB |
Output is correct |
4 |
Correct |
62 ms |
62968 KB |
Output is correct |
5 |
Correct |
57 ms |
62968 KB |
Output is correct |
6 |
Correct |
60 ms |
62968 KB |
Output is correct |
7 |
Correct |
58 ms |
62968 KB |
Output is correct |
8 |
Correct |
62 ms |
62968 KB |
Output is correct |
9 |
Correct |
62 ms |
62968 KB |
Output is correct |
10 |
Correct |
70 ms |
62968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
62968 KB |
Output is correct |
2 |
Correct |
60 ms |
62968 KB |
Output is correct |
3 |
Correct |
58 ms |
62968 KB |
Output is correct |
4 |
Correct |
62 ms |
62968 KB |
Output is correct |
5 |
Correct |
57 ms |
62968 KB |
Output is correct |
6 |
Correct |
60 ms |
62968 KB |
Output is correct |
7 |
Correct |
58 ms |
62968 KB |
Output is correct |
8 |
Correct |
62 ms |
62968 KB |
Output is correct |
9 |
Correct |
62 ms |
62968 KB |
Output is correct |
10 |
Correct |
70 ms |
62968 KB |
Output is correct |
11 |
Correct |
307 ms |
78296 KB |
Output is correct |
12 |
Correct |
293 ms |
78064 KB |
Output is correct |
13 |
Correct |
322 ms |
78068 KB |
Output is correct |
14 |
Correct |
305 ms |
78168 KB |
Output is correct |
15 |
Correct |
289 ms |
78168 KB |
Output is correct |
16 |
Correct |
293 ms |
77504 KB |
Output is correct |
17 |
Correct |
291 ms |
77528 KB |
Output is correct |
18 |
Correct |
310 ms |
77528 KB |
Output is correct |
19 |
Correct |
291 ms |
78044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
72932 KB |
Output is correct |
2 |
Correct |
126 ms |
68840 KB |
Output is correct |
3 |
Correct |
136 ms |
69736 KB |
Output is correct |
4 |
Correct |
184 ms |
72932 KB |
Output is correct |
5 |
Correct |
202 ms |
72932 KB |
Output is correct |
6 |
Correct |
185 ms |
72932 KB |
Output is correct |
7 |
Correct |
177 ms |
72932 KB |
Output is correct |
8 |
Correct |
179 ms |
72936 KB |
Output is correct |
9 |
Correct |
187 ms |
72932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
62968 KB |
Output is correct |
2 |
Correct |
60 ms |
62968 KB |
Output is correct |
3 |
Correct |
58 ms |
62968 KB |
Output is correct |
4 |
Correct |
62 ms |
62968 KB |
Output is correct |
5 |
Correct |
57 ms |
62968 KB |
Output is correct |
6 |
Correct |
60 ms |
62968 KB |
Output is correct |
7 |
Correct |
58 ms |
62968 KB |
Output is correct |
8 |
Correct |
62 ms |
62968 KB |
Output is correct |
9 |
Correct |
62 ms |
62968 KB |
Output is correct |
10 |
Correct |
70 ms |
62968 KB |
Output is correct |
11 |
Correct |
307 ms |
78296 KB |
Output is correct |
12 |
Correct |
293 ms |
78064 KB |
Output is correct |
13 |
Correct |
322 ms |
78068 KB |
Output is correct |
14 |
Correct |
305 ms |
78168 KB |
Output is correct |
15 |
Correct |
289 ms |
78168 KB |
Output is correct |
16 |
Correct |
293 ms |
77504 KB |
Output is correct |
17 |
Correct |
291 ms |
77528 KB |
Output is correct |
18 |
Correct |
310 ms |
77528 KB |
Output is correct |
19 |
Correct |
291 ms |
78044 KB |
Output is correct |
20 |
Correct |
179 ms |
72932 KB |
Output is correct |
21 |
Correct |
126 ms |
68840 KB |
Output is correct |
22 |
Correct |
136 ms |
69736 KB |
Output is correct |
23 |
Correct |
184 ms |
72932 KB |
Output is correct |
24 |
Correct |
202 ms |
72932 KB |
Output is correct |
25 |
Correct |
185 ms |
72932 KB |
Output is correct |
26 |
Correct |
177 ms |
72932 KB |
Output is correct |
27 |
Correct |
179 ms |
72936 KB |
Output is correct |
28 |
Correct |
187 ms |
72932 KB |
Output is correct |
29 |
Incorrect |
849 ms |
102204 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |