Pagini recente » Cod sursa (job #1512074) | Cod sursa (job #834352) | Cod sursa (job #2345958) | Cod sursa (job #909250) | Cod sursa (job #407964)
Cod sursa(job #407964)
#include<fstream>
using namespace std;
const char iname[]="cuburi2.in";
const char oname[]="cuburi2.out";
const int maxn=256000;
ifstream f(iname);
ofstream g(oname);
long long a[maxn],s[maxn],sd[maxn],ss[maxn],i,j,n,m,step,x,y;
int main()
{
f>>n>>m;
for(i=1;i<=n;++i)
f>>a[i],s[i]=s[i-1]+a[i],ss[i]=ss[i-1]+s[i-1];
for(i=n;i;--i)
sd[i]=sd[i+1]+s[n]-s[i];
for(i=1;i<=m;++i)
{
f>>x>>y;
for(step=1;step<y-x;step<<=1);
for(j=x;step;step>>=1)
if(j+step<=y&&s[j+step]-s[x-1]<s[y]-s[j+step])
j+=step;
++j;
g<<j<<" "<<ss[j]-ss[x]-s[x-1]*(j-x)+sd[j]-sd[y]-(s[n]-s[y])*(y-j)<<"\n";
}
f.close();
g.close();
return 0;
}