Cod sursa(job #403893)

Utilizator perticas_catalinperticas catalin perticas_catalin Data 25 februarie 2010 15:41:46
Problema Cuburi2 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include<iostream>
#include<string>

using namespace std;

#define NM 250005
#define LL long long

int N,M;

int A[NM];

LL UL[NM],UR[NM],TL[NM],TR[NM];

inline LL retsum(int x,int y,int t)
{
     LL ans=0;  
     ans+=(TL[t]-TL[x-1]-UL[x-1]*(t-x+1));
     ans+=(TR[t]-TR[y+1]-UR[y+1]*(y-t+1));
     
     return ans;  
}

int main()
{
    int x,y;
    
    freopen("cuburi2.in","r",stdin);
    freopen("cuburi2.out","w",stdout);
    
    scanf("%d %d",&N,&M);
    
    for(int i=1;i<=N;++i)
       scanf("%d",&A[i]);
    
    for(int i=1;i<=N;++i)
       UL[i]=UL[i-1]+A[i];
    
    for(int i=N;i>=1;--i)
       UR[i]=UR[i+1]+A[i];
       
    for(int i=1;i<=N;++i)
       TL[i]=TL[i-1]+UL[i-1];
       
    for(int i=N;i>=1;--i)
       TR[i]=TR[i+1]+UR[i+1];         
    
    for(int i=1;i<=M;++i)
    {
       scanf("%d %d",&x,&y);
            
       LL best=retsum(x,y,x);
       int pos=x;
       
       /*
       for(int j=x;j<=y;++j)
          printf("%I64d ",retsum(x,y,j));
       */
       
       for(int j=x;j<=y;++j)
       {
           LL cur=retsum(x,y,j);
           if(cur<best)
           {
              best=cur;
              pos=j;
           }
       }
       
       printf("%d %lld",pos,best);
       
       printf("\n");
    }
    
    return 0;
}