Cod sursa(job #336636)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 31 iulie 2009 22:33:12
Problema Cuburi2 Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define file_in "cuburi2.in"
#define file_out "cuburi2.out"

#define Nmax 251000
#define LL long long

LL n,m,x,y;
LL v[Nmax];
LL v1[Nmax],v2[Nmax];
LL v3[Nmax],v4[Nmax];
LL s,st,dr;

LL cbin(LL ls, LL ld)
{
	LL sol=ls;
	LL mij;
	
	while(ls<=ld)
	{
		mij=(ls+ld)>>1;
		if (v1[y]-v1[mij-1]>v1[mij-1]-v1[x-1]) 
		{
			sol=mij;
			ls=mij+1;
		}
		else
		{
			ld=mij-1;
		}
	}
	
	return sol;
}

int main()
{
	int i,j;
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%lld %lld", &n,&m);
	v1[0]=v2[0]=0;
	for (i=1;i<=n;++i)
	{
		scanf("%lld", &v[i]);
		v1[i]=v1[i-1]+v[i];
		v2[i]=v2[i-1]+v1[i];
	}
	v3[n+1]=v4[n+1]=0;
	for (i=n;i>=1;--i)
	{
		v3[i]=v3[i+1]+v[i];
		v4[i]=v4[i+1]+v3[i+1];
	}
	
	for (i=1;i<=m;++i)
	{
		scanf("%lld %lld", &x,&y);
		s=cbin(x,y);
		st=v2[s]-v2[x-1]-v1[x-1]*(s-x+1);   
        dr=v4[s]-v4[y+1]-v3[y+1]*(y-s+1);
		printf("%lld %lld\n",s,st+dr);
	}
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}