Cod sursa(job #1366605)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 1 martie 2015 12:03:12
Problema Cuburi2 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 250000 + 1;

long long sum[Nmax];
long long sum2[Nmax];
long long v[Nmax];
int N, M;

long long calc(int x, int y, int m)
{
    return (sum2[y] + sum2[x - 1] - 2LL * sum2[m]) + 1LL * m * (2LL * sum[m] - sum[y] - sum[x - 1]);
}

int main()
{
    ifstream in("cuburi2.in");
    ofstream out("cuburi2.out");

    in >> N >> M;

    for ( int i = 1; i <= N; ++i )
    {
        in >> v[i];
        sum[i] = sum[i - 1] + v[i];
        sum2[i] = sum2[i - 1] + 1LL * i * v[i];
    }

    while ( M-- )
    {
        int st, dr, gasit, x, y;
        in >> x >> y;
        st = x; dr = y;
        gasit = N;

        while ( st <= dr )
        {
            int m = (st + dr) / 2;

            if ( calc(x, y, m) <= calc(x, y, m + 1) )
            {
                gasit = m;
                dr = m - 1;
            }
            else
                st = m + 1;
        }

        out << gasit << " " << calc(x, y, gasit) << "\n";
    }

    return 0;
}