Pagini recente » Cod sursa (job #3320878) | Cod sursa (job #1764602) | Cod sursa (job #1360995) | Cod sursa (job #775646) | Cod sursa (job #3303455)
#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;
}