Pagini recente » Cod sursa (job #144674) | Cod sursa (job #507323) | Cod sursa (job #3179359) | Cod sursa (job #1915436) | Cod sursa (job #2954234)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuburi2.in");
ofstream fout("cuburi2.out");
int n,m;
int a[250005];
long long pref[250005],sc[250005],suff[250005];
long long ppref[250005],ssuff[250005];
/// +pref[p]-pref[x-1] - suff[p+1]+suff[y+1]
/// suff[y+1]-pref[x-1] + pref[p] - suff[p+1]
/// (suff[y+1]-pref[x-1])*(b-x) + (pref[x]+...+pref[b-1]) - (suff[x+1]+...+suff[b])
/*
1 3 7 2 5
4 14
11 7
13 5
*/
int main()
{
fin>>n>>m;
for(int i=1; i<=n; i++)
fin>>a[i];
for(int i=1; i<=n; i++)
pref[i]=pref[i-1]+a[i];
for(int i=n; i>=1; i--)
suff[i]=suff[i+1]+a[i];
for(int i=1; i<=n; i++)
ppref[i]=ppref[i-1]+pref[i];
for(int i=n; i>=1; i--)
ssuff[i]=ssuff[i+1]+suff[i];
for(int i=1; i<=n; i++)
sc[i]=sc[i-1]+1LL*i*a[i];
while(m--)
{
int x,y;
fin>>x>>y;
if(x==y)
fout<<x<<" 0\n";
else
{
if(suff[y+1]-pref[x-1]+pref[x]-suff[x+1]>=0)
{
fout<<x<<" ";
fout<<(sc[y]-sc[x])-1LL*x*(pref[y]-pref[x])<<"\n";
}
else if(suff[y+1]-pref[x-1]+pref[y-1]-suff[y]<=0)
{
fout<<y<<" ";
fout<<1LL*(pref[y-1]-pref[x-1])*y-(sc[y-1]-sc[x-1])<<"\n";
}
else
{
int st=x+1,dr=y-1,mij,b;
while(st<=dr)
{
mij=(st+dr)/2;
if(suff[y+1]-pref[x-1]+pref[mij]-suff[mij+1]>0)
{
b=mij;
dr=mij-1;
}
else
st=mij+1;
}
fout<<b<<" ";
fout<<(sc[y]-sc[x])-1LL*x*(pref[y]-pref[x])+1LL*(suff[y+1]-pref[x-1])*(b-x)+(ppref[b-1]-ppref[x-1])-(ssuff[x+1]-ssuff[b+1])<<"\n";
}
}
}
return 0;
}