Cod sursa(job #981289)

Utilizator Kira96Denis Mita Kira96 Data 6 august 2013 17:31:48
Problema Cuburi2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include<fstream>
#include<cstring>
#define N 250100
#define DIM 5000000
using namespace std;
ifstream f("cuburi2.in");
ofstream g("cuburi2.out");
long long S1[N],S2[N],S[N],rez;
int i,st,dr,m,n,mij,po,x,y,t;
char s[DIM];
int main()
{
    f.get(s,DIM,EOF);
    while(s[t]>='0')
        n=n*10+s[t++]-'0';
    while(s[t]<'0')
        t++;
    while(s[t]>='0')
        m=m*10+s[t++]-'0';
    for(i=1;i<=n;++i)
    {
        x=0;
        while(s[t]<'0')
            t++;
        while(s[t]>='0')
            x=x*10+s[t++]-'0';
        S[i]=x+S[i-1];
        S1[i]=S[i]+S1[i-1];
    }
    for(i=n;i>=1;--i)
        S2[i]=S2[i+1]+S[n]-S[i-1];
    while(m--)
    {
        x=y=0;
        while(s[t]<'0')
            t++;
        while(s[t]>='0')
            x=x*10+s[t++]-'0';
        while(s[t]<'0')
            t++;
        while(s[t]>='0')
            y=y*10+s[t++]-'0';
        st=x;dr=y;
        po=x;
        while(st<=dr)
        {
            mij=(st+dr)>>1;
            if(S[mij-1]-S[x-1]<S[y]-S[mij-1])
            {
                st=mij+1;
                po=mij;
            }
            else
                dr=mij-1;
        }
		rez=S1[po-1]-S1[x-1]-1LL*(po-x)*S[x-1];
		rez+=S2[po+1]-S2[y+1]-1LL*(y-po)*(S[n]-S[y]);
        g<<po<<" "<<rez<<"\n";
    }
    return 0;
}