Pagini recente » Cod sursa (job #272671) | Cod sursa (job #2018735) | Cod sursa (job #1610238) | Cod sursa (job #2042) | Cod sursa (job #530531)
Cod sursa(job #530531)
#include <stdio.h>
#define ma 250001
using namespace std;
int n,m,i,x,y,st,dr,sol,mij,A[ma];
long long PST[ma],PDR[ma],costST[ma],costDR[ma];
long long rez1,rez2;
FILE *f,*g;
void schimba()
{
int aux;
aux=x;
x=y;
y=aux;
}
int main()
{
f=fopen("cuburi2.in","r");
g=fopen("cuburi2.out","w");
fscanf(f,"%d %d",&n,&m);
for (i=1;i<=n;++i)
{
fscanf(f,"%d",&A[i]);
PST[i]=PST[i-1]+A[i];
costST[i]=costST[i-1]+PST[i-1];
}
for (i=n;i>=1;--i)
{
PDR[i]=PDR[i+1]+A[i];
costDR[i]=costDR[i+1]+PDR[i+1];
}
for (i=1;i<=m;i++)
{
fscanf(f,"%d %d",&x,&y);
if (x>y)
schimba();
st=1;
dr=n;
while (st<=dr)
{
mij=(st+dr)/2;
if (PST[mij-1]-PST[x-1]<PST[y]-PST[mij-1])
{
sol=mij;
st=mij+1;
}
else
dr=mij-1;
}
rez1=costST[sol]-(sol-x)*PST[x-1]-costST[x];
rez2=costDR[sol]-(y-sol)*PDR[y+1]-costDR[y];
fprintf(g,"%d %lld\n",sol,rez1+rez2);
}
fclose(f);
fclose(g);
return 0;
}