Cod sursa(job #980969)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 6 august 2013 01:19:04
Problema Cuburi2 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
using namespace std;

const int MAX_N = 250002;

int N, M;
int v[MAX_N];
long long int st[MAX_N], dr[MAX_N], a[MAX_N], b[MAX_N];

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

    f >> N >> M;
    for(int i = 1; i <= N; ++i) {
        f >> v[i];
        a[i] = v[i] + a[i-1], st[i] = st[i-1] + a[i-1];
    }

    for(int i = N; i; --i)
        b[i] = v[i] + b[i+1], dr[i] = dr[i+1] + b[i+1];
    while(M--) {
        int x, y, ind, l, r, m;
        f >> x >> y;
        l = x, r = y;
        long long int BestCost, Cost1, Cost2;
        while(l <= r) {
            m = (l+r)/2;
            if(l == y && r == y) {
                ind = y;
                break;
            }
            Cost1 = st[m] + dr[m] - b[y+1];
            Cost2 = st[m+1] - a[x-1] + dr[m+1];
            if(Cost1 <= Cost2)
                ind = m, r = m-1;
            else l = m+1;
        }
        BestCost = st[ind] - st[x] - a[x-1]*(ind-x) + dr[ind] - dr[y] - b[y+1]*(y-ind);

        g << ind << " " << BestCost << "\n";
    }

    f.close();
    g.close();

    return 0;
}