Cod sursa(job #657665)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 7 ianuarie 2012 00:03:37
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <cstdio>
#include <algorithm>

typedef long long ll;

using namespace std;

ll n,m,poz,x,y;
ll v[250001],nl[250001],nr[250001],cl[250001],cr[250001];

inline void bs(int left,int right)
{
    int mid;

	if (left>right)
		return;

	mid=(left+right)/2;

	if (nl[mid-1]-nl[x-1]<nl[y]-nl[mid-1])
	{
		poz=mid;
		bs(mid+1,right);
	}
	else
        bs(left,mid-1);
}

int main()
{
    ll i,ml,mr;

	freopen("cuburi2.in","r",stdin);
	freopen("cuburi2.out","w",stdout);

	scanf("%lld %lld",&n,&m);

	for (i=1;i<=n;++i)
	{
		scanf("%lld",&v[i]);

		nl[i]=nl[i-1]+v[i];
		cl[i]=cl[i-1]+nl[i-1];
	}

	for (i=n;i;--i)
	{
		nr[i]=nr[i+1]+v[i];
		cr[i]=cr[i+1]+nr[i+1];
	}

	for (;m;--m)
	{
		scanf("%lld %lld",&x,&y);
		if (x>y)
			swap(x,y);

		bs(x,y);

		ml=cl[poz]-(poz-x)*nl[x-1]-cl[x];
		mr=cr[poz]-(y-poz)*nr[y+1]-cr[y];

		printf("%lld %lld\n",poz,ml+mr);
	}

	return 0;
}