Pagini recente » Clasament concurs_elf1 | Monitorul de evaluare | Cod sursa (job #1583414) | Cod sursa (job #1297536) | Cod sursa (job #2471344)
#include <fstream>
using namespace std;
ifstream fin ("cuburi2.in");
ofstream fout ("cuburi2.out");
int v[250001];
long long costst[250001],costdr[250001],spst[250001],spdr[250001];
int main()
{
int n,m,i,l1,l2,poz,mij,x,y;
long long s1,s2;
fin>>n>>m;
for (i=1;i<=n;i++)
{
fin>>v[i];
spst[i]=spst[i-1]+v[i];
costst[i]=costst[i-1]+spst[i-1];
}
for (i=n;i>=1;i--)
{
spdr[i]=spdr[i+1]+v[i];
costdr[i]=costdr[i+1]+spdr[i+1];
}
for (i=1;i<=m;i++)
{
fin>>x>>y;
l1=x;
l2=y;
while (l1<=l2)
{
mij=(l1+l2)/2;
/*s1=costst[mij]-costst[x]-(mij-x)*spst[x-1];
s2=costdr[mij]-costdr[y]-(y-mij)*spdr[y+1];
if (s1>s2)
l2=mij-1;
l1=mij+1;*/
if (spst[mij-1]-spst[x-1]<spst[y]-spst[mij-1])
{
poz=mij;
l1=mij+1;
}
else
l2=mij-1;
}
s1=costst[poz]-costst[x]-(poz-x)*spst[x-1];
s2=costdr[poz]-costdr[y]-(y-poz)*spdr[y+1];
fout<<poz<<" "<<s1+s2<<'\n';
}
return 0;
}