Cod sursa(job #1565570)

Utilizator Athena99Anghel Anca Athena99 Data 10 ianuarie 2016 23:32:37
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <cassert>
#include <cstdio>

using namespace std;

typedef long long i64;

const int nmax= 250000;

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

int main(  ) {
    assert( freopen( "cuburi2.in", "r", stdin ) );
    assert( freopen( "cuburi2.out", "w", stdout ) );

    int n, m;
    assert( scanf( "%d%d", &n, &m ) );

    for ( int i= 1, x; i<=n; ++i ) {
        assert( scanf( "%d", &x ) );
        a[i]= a[i-1]+x;
        b[i]= b[i-1]+a[i-1];
    }
    for ( int i= n-1; i>=1; --i ) {
        c[i]= c[i+1]+a[n]-a[i];
    }

    for ( int cnt= 1; cnt<=m; ++cnt ) {
        int x, y;
        assert( scanf( "%d%d", &x, &y ) );

        int 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= (i64)b[sol]-b[x]-a[x-1]*(sol-x), a2= (i64)c[sol]-c[y]-(a[n]-a[y])*(y-sol);
        a1+= a2;
        printf( "%d %lld\n", sol, a1 );
    }

    return 0;
}