#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=5e5+5,mod=1e9+7;
int t,a[N],L[N],R[N],lazy[4*N],ans[N],n,F,mx[N],b[N];
pair<int,int> tree[4*N];
map<int,int> f;
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 = max(tree[u].s,tree[u].f + lazy[u]);
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 == 0) tree[u].f = val,tree[u].s = val;
else {
lazy[u] = val;
tree[u].s = max(tree[u].s,tree[u].f + lazy[u]);
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;
}
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);
tree[u].s = max(tree[2*u].s,tree[2*u+1].s);
tree[u].f = max(tree[2*u].f,tree[2*u+1].f);
}
int getans(int u,int start,int end,int l,int r){
if(lazy[u]){
tree[u].s = max(tree[u].s,tree[u].f + lazy[u]);
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;
}
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;
freopen("connect.in.txt","r",stdin);
freopen("connect.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
f[a[i]] = 1;
int x=i-1;
while(x && a[x]<a[i]){
x=L[x];
}
L[i]=x;
V[x].push_back(i);
}
for(int i=n;i>=1;i--){
int x = i+1;
while(x<=n && a[x]<a[i]){
x=R[x];
}
R[i]=x;
V[i].push_back(x);
}
int Q;
cin>>Q;
for(int i=1;i<=Q;i++){
int l,r;
cin>>l>>r;
q[l].push_back({r,i});
}
// subtask #3
for(int i=n;i>=1;i--){
mx[i] = max(mx[i+1],a[i]);
}
int Ans = 0;
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;
Ans = max(Ans,mx[ind]+a[i]+a[V[i][j]]);
// update(1,ind,n,1,n,a[i]+a[V[i][j]],1);
}
for(int j=0;j<q[i].size();j++){
// ans[q[i][j].s]=getans(1,i+2,q[i][j].f,1,n);
}
}
cout<<Ans<<" ";
}
Compilation message
jumps.cpp:58:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main(){
| ^
jumps.cpp: In function 'int main()':
jumps.cpp:97: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]
97 | for(int j=0;j<V[i].size();j++){
| ~^~~~~~~~~~~~
jumps.cpp:103: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]
103 | for(int j=0;j<q[i].size();j++){
| ~^~~~~~~~~~~~
jumps.cpp:60:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
60 | freopen("connect.in.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
61 | freopen("connect.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |