Cod sursa(job #981281)

Utilizator Kira96Denis Mita Kira96 Data 6 august 2013 17:17:15
Problema Cuburi2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<fstream>
#include<cstring>
#define N 250100
#define DIM 5000000
using namespace std;
ifstream f("cuburi2.in");
ofstream g("cuburi2.out");
long long S1[N],S2[N],Ss[N],Sd[N],A[N],i,st,dr,m,n,mij,sol,x,y,t;
char s[DIM];
long long solve(long long po)
{
	return S1[po-1]-S1[x-1]-(po-x)*Ss[x-1]+S2[po+1]-S2[y+1]-(y-po)*Sd[y+1];
}
int main()
{
	f.get(s,DIM,EOF);
	while(s[t]>='0')
		n=n*10+s[t++]-'0';
	while(s[t]<'0')
		t++;
	while(s[t]>='0')
		m=m*10+s[t++]-'0';
	for(i=1;i<=n;++i)
	{
		x=0;
		while(s[t]<'0')
			t++;
		while(s[t]>='0')
			x=x*10+s[t++]-'0';
		A[i]=x;
		Ss[i]=A[i]+Ss[i-1];
		S1[i]=Ss[i]+S1[i-1];
	}
	for(i=n;i>=1;--i)
	{
		Sd[i]=A[i]+Sd[i+1];
		S2[i]=S2[i+1]+Sd[i];
	}
	while(m--)
	{
		x=y=0;
		while(s[t]<'0')
			t++;
		while(s[t]>='0')
			x=x*10+s[t++]-'0';
		while(s[t]<'0')
			t++;
		while(s[t]>='0')
			y=y*10+s[t++]-'0';
		if(x==y)
		{
			g<<x<<" "<<0<<"\n";
			continue;
		}
		if(x==y+1||y==x+1)
		{
			if(A[x]<A[y])
				g<<y<<" "<<A[x]<<"\n";
			else
				g<<x<<" "<<A[y]<<"\n";
			continue;
		}
		st=x;dr=y;
		sol=x;
		while(st<=dr)
		{
			mij=(st+dr)>>1;
			if(Ss[mij-1]-Ss[x-1]<Ss[y]-Ss[mij-1])
			{
				st=mij+1;
				sol=mij;
			}
			else
				dr=mij-1;
		}
		g<<sol<<" "<<solve(sol)<<"\n";
	}
	return 0;
}