Pagini recente » Cod sursa (job #1484464) | Clasament lazy_guys | Cod sursa (job #2817512) | Cod sursa (job #1801140) | Cod sursa (job #2253676)
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
long long sp[250005];
int v[250005];
long long calc[250005];
int main()
{ freopen("cuburi2.in", "r",stdin);
freopen("cuburi2.out", "w",stdout);
int n,m,i,j,x,y,ii;
long long s=0;
scanf("%d%d", &n, &m);
for(i=1; i<=n; i++){
scanf("%d", &v[i]);
sp[i]=sp[i-1]+v[i];
}
for(i=1; i<=m; i++){
scanf("%d%d", &x, &y);
for(j=x; j<=y; j++){
calc[j-x+1]=sp[y]-sp[x-1]-2*1LL*(sp[j]-sp[x-1]);
}
if(calc[1]<0){
ii=x;
s=calc[j-x+1];
}
else{
ii=x+1;
s=0;
}
for(j=x+1; j<y; j++){
if(calc[j-x+1]+s<0)
s=calc[j-x+1];
else{
ii=j+1;
s=0;
}
}
s=0;
for(j=x; j<=y; j++)
s+=v[j]*1LL*abs(ii-j);
printf("%d %lld\n", ii, s);
}
return 0;
}