#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=2e5+5,mod=1e9+7;
int t,a[N],L[N],R[N],lazy[4*N],ans[N],n,F;
pair<int,pair<int,int> > tree[4*N];
vector<int>V[N];
vector<pair<int,int> > q[N];
void update(int u,int start,int end,int l,int r,int val,int f) {
if(lazy[u]){
tree[u].s.f =max(tree[u].s.f,lazy[u]);
tree[u].s.s = max(tree[u].s.s,tree[u].f + tree[u].s.f);
if(l!=r){
lazy[2*u]=max(lazy[2*u],lazy[u]);
lazy[2*u+1]=max(lazy[2*u+1],lazy[u]);
}
lazy[u] = 0;
}
if(l>end || r<start) return;
if(start<=l && r<=end) { // if(F) cout<<"++++++++++++++"<<endl;cout<<l<<" _______ "<<r<<" "<<tree[u].f<<" "<<tree[u].s.f<<" "<<tree[u].s.s<<endl;
if(f == 0) tree[u].f = val,tree[u].s.s = val;
else {
lazy[u] = val;
tree[u].s.f =max(tree[u].s.f,lazy[u]);
tree[u].s.s = max(tree[u].s.s,tree[u].f + tree[u].s.f);
if(l!=r){
lazy[2*u]=max(lazy[2*u],lazy[u]);
lazy[2*u+1]=max(lazy[2*u+1],lazy[u]);
}
lazy[u] = 0;
}// cout<<l<<" _______ "<<r<<" "<<tree[u].f<<" "<<tree[u].s.f<<" "<<tree[u].s.s<<endl;
return;
}
int mid=(l+r)/2;
update(2*u,start,end,l,mid,val,f);
update(2*u+1,start,end,mid+1,r,val,f);
if(tree[2*u].f > tree[2*u+1].f) tree[u]=tree[2*u]; else tree[u]=tree[2*u+1];
tree[u].s.s = max(tree[2*u].s.s,tree[2*u+1].s.s);
// cout<<l<<" "<<r<<" da "<<tree[u].s.s<<endl;
}
int getans(int u,int start,int end,int l,int r){
if(lazy[u]){
tree[u].s.f =max(tree[u].s.f,lazy[u]);
tree[u].s.s = max(tree[u].s.s,tree[u].f + tree[u].s.f);
if(l!=r){
lazy[2*u]=max(lazy[2*u],lazy[u]);
lazy[2*u+1]=max(lazy[2*u+1],lazy[u]);
}
lazy[u] = 0;
}
if(l>end || r<start) return 0;
if(start<=l && r<=end) {
return tree[u].s.s;
}
int mid=(l+r)/2;
return max(getans(2*u,start,end,l,mid),getans(2*u+1,start,end,mid+1,r));
}
string s;
main(){
// t=1;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
int x=i-1;
while(x && a[x]<a[i]){
x=L[x];
}
L[i]=x;
V[x].push_back(i);
//cout<<x<<"__"<<i<<endl;
}
for(int i=n;i>=1;i--){
int x = i+1;
if(x<=n && a[x]<a[i]){
x=R[x];
}
R[i]=x;
V[i].push_back(x);
//cout<<i<<"__"<<x<<endl;
}
int Q;
cin>>Q;
for(int i=1;i<=Q;i++){
int l,r;
cin>>l>>r;
q[l].push_back({r,i});
}
for(int i=n;i>=1;i--){
update(1,i,i,1,n,a[i],0);
for(int j=0;j<V[i].size();j++){
int ind = 2*V[i][j]-i; //cout<<i<<"____"<<V[i][j]<<" "<<ind<<endl;
if(i==3 && V[i][j]==4) F=1;
update(1,ind,n,1,n,a[i]+a[V[i][j]],1); F=0;
}
for(int j=0;j<q[i].size();j++){
if(i==2) F=1;
ans[q[i][j].s]=getans(1,1,q[i][j].f,1,n); F=0;
}
}
for(int i=1;i<=Q;i++){
cout<<ans[i]<<" ";
}
}
Compilation message
jumps.cpp:61:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
61 | main(){
| ^
jumps.cpp: In function 'int main()':
jumps.cpp:93:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int j=0;j<V[i].size();j++){
| ~^~~~~~~~~~~~
jumps.cpp:98:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int j=0;j<q[i].size();j++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
7 ms |
9748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
7 ms |
9748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
349 ms |
40032 KB |
Output is correct |
2 |
Correct |
276 ms |
40360 KB |
Output is correct |
3 |
Correct |
341 ms |
38744 KB |
Output is correct |
4 |
Correct |
354 ms |
39968 KB |
Output is correct |
5 |
Correct |
378 ms |
39980 KB |
Output is correct |
6 |
Correct |
330 ms |
39380 KB |
Output is correct |
7 |
Incorrect |
333 ms |
39192 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Incorrect |
7 ms |
9748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |