Cod sursa(job #1565512)

Utilizator Athena99Anghel Anca Athena99 Data 10 ianuarie 2016 21:59:09
Problema Cuburi2 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>

using namespace std;

ifstream fin("cuburi2.in");
ofstream fout("cuburi2.out");

typedef long long i64;

const i64 nmax= 250000;

i64 v[nmax+1], a[nmax+1], b[nmax+1], c[nmax+1];

int main(  ) {
    i64 n, m;
    fin>>n>>m;
    for ( i64 i= 1; i<=n; ++i ) {
        fin>>v[i];
        a[i]= a[i-1]+v[i];
        b[i]= b[i-1]+a[i-1];
    }
    for ( i64 i= n-1; i>=1; --i ) {
        c[i]= c[i+1]+a[n]-a[i];
    }

    for ( i64 cnt= 1; cnt<=m; ++cnt ) {
        i64 x, y;
        fin>>x>>y;

        i64 sol= x, step;
        for ( step= 1; step<=y-x; step*= 2 ) ;
        for ( ; step>0; step/= 2 ) {
            if ( sol+step<=y && a[sol+step-1]-a[x-1]<a[y]-a[sol+step-1] ) {
                sol+= step;
            }
        }

        i64 a1= b[sol]-b[x]-(sol-x)*a[x-1], a2= c[sol]-c[y]-(y-sol)*(a[n]-a[y]);
        fout<<sol<<" "<<(i64)a1+a2<<"\n";
    }

    return 0;
}