Cod sursa(job #1301653)

Utilizator bence21Bako Bence bence21 Data 26 decembrie 2014 11:54:35
Problema Arbori indexati binar Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.32 kb
#include<fstream>
using namespace std;
int main()
{
    ifstream f("aib.in");
    ofstream g("aib.out");
    unsigned long n,m,i,b,j,h=0,c,d,k;
    unsigned long long a,x,e[100001];
    e[0]=0;
    int t[100001],p;
    f>>n>>m;
    for(i=1;i<=n;i++)
        f>>t[i];
    for(i=1;i<=m;i++)
    {
        f>>p;
        if(p==0)
        {
            f>>a>>b;
            t[a]+=b;
            for(j=a;j<=n;j++)
                e[j]+=b;
        }
        else if(p==1)
        {
            f>>a>>b;
            if(b>h)
            {
                x=e[h];
                while(h<b)
                {
                    x+=t[++h];
                    e[h]=x;
                }
            }
            g<<e[b]-e[a-1]<<"\n";
        }
        else {
            f>>a;
            if(h==0)
            {
                x=0;
                while(x<a&&h<n)
                {
                    x+=t[++h];
                    e[h]=x;
                }
                if(x==a)
                    g<<h<<"\n";
                else g<<"-1\n";
            }
            else
            {
                if(e[h]>=a)
                {
                    c=1;
                    d=h;
                    k=(c+d)/2;
                    bool jo=0;
                    while(c<=d)
                    {
                        if(e[k]==a)
                        {
                            c=d+1;
                            g<<k<<"\n";
                            jo=1;
                        }
                        else if(e[k]<a)
                        {
                            c=k+1;
                            k=(c+d)/2;
                        }
                        else {
                            d=k-1;
                            k=(c+d)/2;
                        }
                    }
                    if(jo==0)
                        g<<"-1\n";
                }
                else {
                    x=e[h];
                    while(x<a&&h<n)
                    {
                        x+=t[++h];
                        e[h]=x;
                    }
                    if(x==a)
                        g<<h<<"\n";
                    else g<<"-1\n";
                }
            }
        }
    }
    f.close();
    g.close();
}