Cod sursa(job #919379)

Utilizator apopeid13Apopeid Alejandro apopeid13 Data 19 martie 2013 17:00:40
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <algorithm>
#include <fstream>
#include <cstdlib>
 
typedef long long ll;
  
using namespace std;
 
ifstream fin("cuburi2.in");
 
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);
}
  
char *buffer;
 
int read_int(){
    while (*buffer<'0'|| *buffer>'9'){
        ++buffer;
    }
    int  x= 0;
    while (*buffer>='0'&& *buffer<='9'){
        x= 10*x+*buffer-'0';
        ++buffer;
    }
    return x;
}
 
int main()
{
    fin.seekg(0, ios::end);
    int fs= fin.tellg();
    fin.seekg(0, ios::beg);
    buffer= (char*)malloc(fs);
    fin.getline(buffer, fs, 0);
 
    ll i,ml,mr;
 
    freopen("cuburi2.out", "w", stdout);
    n= read_int(); m= read_int();
     
    for (i=1;i<=n;++i)
    {
        v[i]= read_int();
  
        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)
    {
        x= read_int(); y= read_int();
        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;
}