Pagini recente » Monitorul de evaluare | Cod sursa (job #2551714) | Cod sursa (job #2634981) | Cod sursa (job #2011211) | Cod sursa (job #2476618)
#include <fstream>
using namespace std;
ifstream cin ("cuburi2.in");
ofstream cout ("cuburi2.out");
int v[250005];
int st[250005];
int dr[250005];
long long costst[250005];
long long costdr[250005];
int main()
{
long long n,m,i,poz,x,y,sumst,sumdr,sta,dre,mi,mini=100000000000000000;
cin>>n>>m;
for (i=1;i<=n;i++)
{
cin>>v[i];
st[i]=st[i-1]+v[i];
costst[i]=costst[i-1]+st[i-1];
}
for (i=n;i>=1;i--)
{
dr[i]=dr[i+1]+v[i];
costdr[i]=costdr[i+1]+dr[i+1];
}
for (int h=1;h<=m;h++)
{
cin>>x>>y;
if (y==x)
{
poz=x;
}
else
if (x==y-1)
{
int t=x;
long long a=costst[t]-costst[x]-st[x-1]*(t-x)+costdr[t]-costdr[y]-dr[y+1]*(y-t);
t++;
long long b=costst[t]-costst[x]-st[x-1]*(t-x)+costdr[t]-costdr[y]-dr[y+1]*(y-t);
if (a<b)
poz=x;
else
poz=y;
}
else
{sta=x;
dre=y;
poz=-1;
while (sta<=dre&&poz==-1)
{
mi=(sta+dre)/2;
int t=mi;
if (t==x||t==y)
poz=t;
else
{
long long a,b,c;
b=costst[t]-costst[x]-st[x-1]*(t-x)+costdr[t]-costdr[y]-dr[y+1]*(y-t);
t--;
a=costst[t]-costst[x]-st[x-1]*(t-x)+costdr[t]-costdr[y]-dr[y+1]*(y-t);
t+=2;
c=costst[t]-costst[x]-st[x-1]*(t-x)+costdr[t]-costdr[y]-dr[y+1]*(y-t);
t--;
if (a>=b&&b<=c)
{
poz=t;
}
else
if (a<b&&b<c)
dre=mi-1;
else
{
sta=mi+1;
//if (a==b||b==c)
}
}
}
}
sumst=costst[poz]-costst[x]-st[x-1]*(poz-x);
sumdr=costdr[poz]-costdr[y]-dr[y+1]*(y-poz);
cout<<poz<<' '<<sumst+sumdr<<'\n';
}
return 0;
}