Cod sursa(job #1500353)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 11 octombrie 2015 19:56:43
Problema Cuburi2 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int MAXN = 250010;

int V[MAXN];
long long A[MAXN], Left[MAXN];
long long B[MAXN], Right[MAXN];

long long f (int X, int mid, int Y)
{
    //calculeaza costul pentru a muta toate cuburile din [X, Y] pe turnul de pe pozitia mid

    long long ret = 0;

    ret = Right[mid] - Right[Y];
    ret -= 1LL * B[Y + 1] * (Y - mid);
    ret += Left[mid] - Left[X];
    ret -= 1LL * A[X - 1] * (mid - X);

    return ret;
}

int main ()
{
    int N, M;
    int i;

    in >> N >> M;

    for (i = 1; i <= N; i ++){
        in >> V[i];
        A[i] = A[i - 1] + V[i];
        Left[i] = Left[i - 1] + A[i - 1];
    }

    for (i = N; i; i --){
        B[i] = B[i + 1] + V[i];
        Right[i] = Right[i + 1] + B[i + 1];
    }

    int x, y;
    int step, idx;

    while (M --){
        in >> x >> y;

        for (step = 1; step < y - x + 1; step <<= 1);

        for (i = x - 1; step; step >>= 1)
            if (i + step <= y)
                if (A[i + step] - A[x - 1] >= A[y] - A[i + step])   
                    idx = i + step;
                else
                    i += step;

        out << idx << " " << f (x, idx, y) << "\n";
    }

    return 0;
}