Cod sursa(job #2540258)

Utilizator nubnubMeh Neh nubnub Data 6 februarie 2020 21:46:17
Problema Cuburi2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <iostream>
#include <fstream>
#define dim 250005
using namespace std;
ifstream f("cuburi2.in");
ofstream g("cuburi2.out");
int n,m; long long st[dim],dr[dim],cost_st[dim],cost_dr[dim],a[dim];
void read()
{
    f>>n>>m;
    for(int i=1;i<=n;i++)
    {
        f>>a[i];
        st[i]=st[i-1]+a[i];
        cost_st[i]=cost_st[i-1]+st[i-1];
    }
}
int caut_binar(int x,int y)
{
    int i,j,mij,rez=x;
    i=x; j=y;
    while(i<=j)
    {
        mij=(i+j)/2;
        if(st[mij-1]-st[x-1]<st[y]-st[mij-1])
        {
            i=mij+1;
            rez=mij;
        }
        else
            j=mij-1;
    }
    return rez;
}
void solve()
{
    int x,y,pos;
    for(int i=n;i>=1;--i)
    {
        dr[i]=dr[i+1]+a[i];
        cost_dr[i]=cost_dr[i+1]+dr[i+1];
    }
    for(int i=1;i<=m;i++)
    {
        f>>x>>y;
        pos=caut_binar(x,y);
        long long rez_st,rez_dr;
        rez_st=cost_st[pos]-cost_st[x-1]-st[x-1]*(pos-x+1);
        rez_dr=cost_dr[pos]-cost_dr[y+1]-dr[y+1]*(y-pos+1);
        g<<pos<<" "<<1LL*(rez_st+rez_dr)<<'\n';
    }
}
int main()
{
    read();
    solve();
    return 0;
}