Pagini recente » Cod sursa (job #1267679) | Cod sursa (job #337654) | Cod sursa (job #2868683) | Cod sursa (job #1264855) | Cod sursa (job #288920)
Cod sursa(job #288920)
#include<stdio.h>
#define N 250010
int n,m;
int v[N];
long long s[N],ajt[N];
long long sr[N],ajtr[N];
char c[9*N];
inline void citescv()
{
fgets(c,9*N,stdin);
int cate=0,aux=0;
for(int i=0; c[i]!='\0'; ++i)
{
if(c[i]>='0' && c[i]<='9')
aux=aux*10+c[i]-'0';
else
{
v[++cate]=aux;
aux=0;
}
}
if(cate<n)
v[++cate]=aux;
}
inline void citire()
{
scanf("%d%d\n",&n,&m);
citescv();
}
inline void precalc()
{
for(int i=1; i<=n; ++i)
{
long long aux=(long long)v[i];
s[i]=s[i-1]+aux;
ajt[i]=ajt[i-1]+s[i];
}
for(int i=n; i; --i)
{
sr[i]=sr[i+1]+v[i];
ajtr[i]=ajtr[i+1]+sr[i];
}
}
inline long long coststg(int x,int y)
{
if(x>y)
return 0;
return ajt[y]-ajt[x-1]-s[x-1]*(y-x+1);
}
inline long long costdr(int x,int y)
{
if(x>y)
return 0;
return ajtr[x]-ajtr[y+1]-sr[y+1]*(y-x+1);
}
inline void rezolva()
{
char c[30];
fgets(c,30,stdin);
int x=0,y=0,i=0;
for(i=0; c[i]>='0' && c[i]<='9'; ++i)
x=x*10+c[i]-'0';
for(++i; c[i]>='0' && c[i]<='9'; ++i)
y=y*10+c[i]-'0';
if(x==y)
{
printf("%d 0\n",x);
return;
}
int p=y;
long long cost;
long long aux=(long long)(y-x);
cost=ajt[y]-ajt[x]-aux*s[x-1];
long long cost1=cost;
int u=y;
p=x;
int pok=x;
while(p<=u)
{
int m=(p+u)>>1;
long long aux1=coststg(x,m-1);
long long aux2=costdr(m+1,y);
if(aux2-aux1>0)
{
pok=m;
p=m+1;
}
else
u=m-1;
}
p=pok;
cost=coststg(x,pok-1)+costdr(pok+1,y);
/*u=p;
if(p!=x)
{
cost1=coststg(x,p-2)+costdr(p,y);
if(cost1<cost)
{
cost=cost1;
u=p-1;
}
}*/
if(pok<y)
{
p=pok+1;
cost1=coststg(x,p-1)+costdr(p+1,y);
if(cost1<cost)
{
cost=cost1;
pok=p;
}
}
printf("%d %lld\n",pok,cost);
}
int main()
{
freopen("cuburi2.in","r",stdin);
freopen("cuburi2.out","w",stdout);
citire();
precalc();
for(; m; --m)
rezolva();
return 0;
}