Cod sursa(job #1565514)

Utilizator Athena99Anghel Anca Athena99 Data 10 ianuarie 2016 22:03:19
Problema Cuburi2 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>

using namespace std;

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

typedef long long i64;

const int nmax= 250000;

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

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

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

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

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

    return 0;
}