Cod sursa(job #270477)

Utilizator eugen.nodeaEugen Nodea eugen.nodea Data 4 martie 2009 01:00:02
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
# include <stdio.h>
# define N 250002
long long a[N],A[N],b[N],B[N],st,dr;
int v[N],n,m,x,y,poz;
void BS(int i, int j)
{ int m;
  while (i<=j) {
    m=(i+j)>>1;
    if (a[m-1]-a[x-1]<a[y]-a[m-1]){
	  i=m+1; poz=m;
	  }
    else j=m-1;
  }
}
int main(){
  int i;
  freopen("cuburi2.in", "r", stdin);
  freopen("cuburi2.out", "w", stdout);
  scanf("%d %d",&n,&m);
  for (i=1;i<=n;++i){
	scanf("%d ",&v[i]);
	a[i]=a[i-1]+v[i];
	A[i]=A[i-1]+a[i-1];
    }
  for (i=n;i>0;--i){
	b[i]=b[i+1]+v[i];
	B[i]=B[i+1]+b[i+1];
    }
  for (i=1;i<=m;++i){
	scanf("%d %d",&x,&y);
	poz=x; BS(x,y);
	st=A[poz]-A[x-1]-a[x-1]*(poz-x+1);
	dr=B[poz]-B[y+1]-b[y+1]*(y-poz+1);
	printf("%d %lld\n",poz,st+dr);
    }
  return 0;
}