Cod sursa(job #1565511)

Utilizator Athena99Anghel Anca Athena99 Data 10 ianuarie 2016 21:57:53
Problema Cuburi2 Scor 32
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>

using namespace std;

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

const int nmax= 250000;

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

int main(  ) {
    int n, m;
    fin>>n>>m;
    for ( int i= 1; i<=n; ++i ) {
        fin>>v[i];
        a[i]= a[i-1]+v[i];
        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;
        fin>>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;
            }
        }

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

    return 0;
}