Pagini recente » Cod sursa (job #259174) | Cod sursa (job #3213109) | Cod sursa (job #2624185) | Cod sursa (job #978722) | Cod sursa (job #2731957)
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,m,a[100005];
struct smh{
ll sum;
ll pref;
ll suf;
ll ans;
};
smh idk;
smh tree[1000005];
smh con(smh x,smh y){
smh res;
res.sum=x.sum+y.sum;
res.ans=max(x.ans,max(y.ans,x.suf+y.pref));
res.pref=max(x.pref,x.sum+y.pref);
res.suf=max(y.suf,y.sum+x.suf);
return res;
}
void build(ll node,ll l,ll r){
if(l==r){
tree[node].sum=a[l];
tree[node].ans=a[l];
tree[node].suf=a[l];
tree[node].pref=a[l];
return;
}
ll mid=(l+r)/2;
build(2*node,l,mid);
build(2*node+1,mid+1,r);
tree[node]=con(tree[2*node],tree[2*node+1]);
}
smh query(ll node,ll l,ll r,ll st,ll dr){
if(r<st){
return idk;
}
if(l>dr){
return idk;
}
if(st<=l&&r<=dr){
return tree[node];
}
ll mid=(l+r)/2;
return con(query(2*node,l,mid,st,dr),query(2*node+1,mid+1,r,st,dr));
}
int32_t main(){
CODE_START;
ifstream cin("sequencequery.in");
ofstream cout("sequencequery.out");
cin>>n>>m;
idk.sum=-INF;
idk.pref=-INF;
idk.suf=-INF;
idk.ans=-INF;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
build(1,1,n);
while(m--){
ll l,r;
cin>>l>>r;
cout<<query(1,1,n,l,r).ans<<endl;
}
}