Cod sursa(job #3303455)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 15 iulie 2025 17:17:25
Problema Cuburi2 Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>

using namespace std;

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

const int nmax = 250000;
int n, a[nmax + 2], nrq, lf, rg;

int64_t sp[nmax + 2];
int64_t costtlf[nmax + 2];
int64_t costtrg[nmax + 2];

int64_t target, bestt;

int64_t costt(int st, int dr, int pos){
    int64_t leftt = costtlf[pos - 1] - costtlf[st - 1];
    int64_t rightt = costtrg[dr] - costtrg[pos];

    ///(n - pos) - 1
    leftt -= 1ll * (n - pos + 1) * (sp[pos - 1] - sp[st - 1]);
    rightt -= 1ll * pos * (sp[dr] - sp[pos]);

    ///out<<pos - 1<<" "<<n - pos - 1<<" | "<<pos + 1<<" "<<pos<<" ||| ";

    return leftt + rightt;
}

int main(){

    in>>n>>nrq;
    for(int i = 1; i <= n; i++)
        in>>a[i], sp[i] = sp[i - 1] + a[i];

    for(int i = 1; i <= n; i++){
        costtlf[i] = costtlf[i - 1] + 1ll * (n - i + 1) * a[i];
        costtrg[i] = costtrg[i - 1] + 1ll * i * a[i];
    }

    for(int it = 1; it <= nrq; it++){
        in>>lf>>rg; bestt = (1ll << 60);

        ///out<<"For: "<<lf<<" "<<rg<<" -> \n";
        for(int i = lf; i <= rg; i++){
            ///out<<"---> "<<i<<" "<<costt(lf, rg, i)<<"\n";
            if(bestt > costt(lf, rg, i)){
                bestt = costt(lf, rg, i);
                target = i;
            }
        }

        out<<target<<" "<<bestt<<"\n";
    }

    return 0;
}