This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include<bits/stdc++.h>
#define int long long
//#define ll long long
#define down cout<<'\n';
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void ngha();
const int mod2=1e9+7;
const int mod1=998244353;
struct ib
{
int a;
int b;
};
struct icd
{
int a,b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c, d,e;
};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int i,s10,s12;
int el=29;
main()
{
//#ifndef ONLINE_JUDGE
// fin(task),fou(task);
//#endif
NHP
//modwwe
// cin>>res;
ngha(),down
checktime
}
vector<ib> v[500001];
vector<int>v2[500001];
ic t[2000001];
int t2[2000001];
int a[500001];
int b[500001];
void ff(int x)
{
for(int i=x*2;i<=x*2+1;i++)
{
t[i].a=max(t[x].a,t[i].a);
if(t[i].a!=0)
t[i].c=max(t[i].c,t[i].a+t[i].b);
}
}
void build(int node,int l,int r)
{
if(l==r){
t[node].b=a[l]; return;}
int mid=l+r>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
t[node].b=max(t[node<<1].b,t[node<<1|1].b);
}
void upd(int node,int l,int r,int l1,int r1,int k)
{//cout<<l<<" "<<r<<" "<<l1<<" "<<r1<<" "<<t[node].c,down
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1) {
t[node].a=max(t[node].a,k);
t[node].c=max(t[node].c,t[node].a+t[node].b);
return;
}
int mid=l+r>>1;
ff(node);
upd(node<<1,l,mid,l1,r1,k);
upd(node<<1|1,mid+1,r,l1,r1,k);
t[node].c=max(t[node<<1].c,t[node<<1|1].c);
}
int get(int node,int l,int r,int r1)
{
if(l>r1) return 0;
if(r<=r1){ return t[node].c;}
ff(node);
int mid=l+r>>1;
return max(get(node<<1,l,mid,r1),get(node<<1|1,mid+1,r,r1));
}
void ngha()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>l>>r;
v[l].pb({r,i});
} /// chi xet den cai lon hon dau tien :3
/// va cai lon nhat
/// neu a<=b<=c thi chon b va c hoac a va b
stack<int> s;
for(int i=1;i<=n;i++)
{
while(!s.empty()&&a[s.top()]<=a[i])
{
v2[s.top()].pb(i);
s.pop();
}
if(!s.empty()) v2[s.top()].pb(i);
s.push(i);
}
for(int i=n-2;i>=1;--i)
{
for(auto x:v2[i])
{ ///b-a<=c-b 2b-a<=b
s2=x*2-i;
if(s2<=n)
upd(1,1,n,s2,n,a[i]+a[x]);
// if(i>=11) cout<<s2<<" "<<a[i]+a[x]<<" "<<t[2].c<<" cucu",down
}
for(auto x:v[i])
{
b[x.b]=get(1,1,n,x.a);
}
}
for(int i=1;i<=m;i++)
cout<<b[i],down
}
Compilation message (stderr)
jumps.cpp:45:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
45 | main()
| ^~~~
jumps.cpp: In function 'void build(long long int, long long int, long long int)':
jumps.cpp:75:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | int mid=l+r>>1;
| ~^~
jumps.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
jumps.cpp:88:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
88 | int mid=l+r>>1;
| ~^~
jumps.cpp: In function 'long long int get(long long int, long long int, long long int, long long int)':
jumps.cpp:99:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
99 | int mid=l+r>>1;
| ~^~
jumps.cpp: In function 'void ngha()':
jumps.cpp:105:7: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
105 | for(int i=1;i<=n;i++)
| ^~~
jumps.cpp:107:10: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
107 | build(1,1,n);
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |