Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Istoria paginii utilizator/tebaatusasula | Cod sursa (job #1565514)
#include <fstream>
using namespace std;
ifstream fin("cuburi2.in");
ofstream fout("cuburi2.out");
typedef long long i64;
const int nmax= 250000;
i64 a[nmax+1], b[nmax+1], c[nmax+1];
int main( ) {
int n, m;
fin>>n>>m;
for ( int i= 1, x; i<=n; ++i ) {
fin>>x;
a[i]= (i64)a[i-1]+x;
b[i]= (i64)b[i-1]+a[i-1];
}
for ( int i= n-1; i>=1; --i ) {
c[i]= (i64)c[i+1]+a[n]-a[i];
}
for ( int cnt= 1; cnt<=m; ++cnt ) {
int x, y;
fin>>x>>y;
i64 sol= x, step;
for ( step= 1; step<=y-x; step*= 2 ) ;
for ( ; step>0; step/= 2 ) {
if ( (i64)sol+step<=y && (i64)a[sol+step-1]-a[x-1]<(i64)a[y]-a[sol+step-1] ) {
sol+= step;
}
}
i64 a1= (i64)b[sol]-b[x]-(sol-x)*a[x-1], a2= (i64)c[sol]-c[y]-(y-sol)*(a[n]-a[y]);
fout<<sol<<" "<<(i64)a1+a2<<"\n";
}
return 0;
}