Cod sursa(job #643743)

Utilizator Smaug-Andrei C. Smaug- Data 4 decembrie 2011 13:07:55
Problema Cuburi2 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <cstdio>

#define MAXN 250010

int main(){

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

  int N, M, i, x, y, aux, r, l, mid, sol;
  long long cost;
  static int A[MAXN];
  static long long L[MAXN], R[MAXN], costL[MAXN], costR[MAXN];

  scanf("%d%d", &N, &M);
  for(i=1; i<=N; i++){
    scanf("%d", A+i);
    L[i]=L[i-1]+A[i];
    costL[i]=costL[i-1]+L[i-1];
  }
  for(i=N; i; i--){
    R[i]=R[i+1]+A[i];
    costR[i]=costR[i+1]+R[i+1];
  }

  while(M--){
    scanf("%d%d", &x, &y);
    
    if(x>y){
      aux=x; x=y; y=aux;
    }

    l=x; r=y;
    while(l<=r){
      mid=(r+l)>>1;
      if(L[mid-1]-L[x-1] <= L[y]-L[mid-1])
	l=mid+1;
      else
	r=mid-1;
    }
    
    sol=r;
    cost=costL[sol]-L[x-1]*(sol-x)-costL[x]+costR[sol]-R[y+1]*(y-sol)-costR[y];
    printf("%d %lld\n", sol, cost);

  }

  return 0;

}