Pagini recente » Cod sursa (job #3192232) | Cod sursa (job #2793252) | Cod sursa (job #2938140) | Cod sursa (job #2318878) | Cod sursa (job #643846)
Cod sursa(job #643846)
#include <cstdio>
#define MAXN 250010
int main(){
freopen("cuburi2.in", "r", stdin);
freopen("cuburi2.out", "w", stdout);
int N, M, i, x, y, aux, r, l, mid;
long long cost;
static int A[MAXN];
static long long L[MAXN], R[MAXN], costL[MAXN], costR[MAXN];
scanf("%d%d", &N, &M);
for(i=1; i<=N; i++){
scanf("%d", A+i);
L[i]=L[i-1]+A[i];
costL[i]=costL[i-1]+L[i-1];
}
for(i=N; i; i--){
R[i]=R[i+1]+A[i];
costR[i]=costR[i+1]+R[i+1];
}
while(M--){
scanf("%d%d", &x, &y);
if(x>y){
aux=x; x=y; y=aux;
}
l=x; r=y;
while(l<=r){
mid=(r+l)>>1;
if(L[mid-1]-L[x-1] < L[y]-L[mid-1])
l=mid+1;
else
r=mid-1;
}
cost=costL[r]-L[x-1]*(r-x)-costL[x]+costR[r]-R[y+1]*(y-r)-costR[y];
printf("%d %lld\n", r, cost);
}
return 0;
}