Cod sursa(job #2851113)

Utilizator alexmorosanuMorosanu Alexandru alexmorosanu Data 18 februarie 2022 09:35:42
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#define zeros(x) ((x^(x-1))&x)
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int n,a[100011];
int query(int x)
{
    int s=0;
    while(x>0)
    {
        s=s+a[x];
        x=x-zeros(x);
    }
    return s;
}
void update(int ind,int val)
{
    while(ind<=n)
    {
    a[ind]=a[ind]+val;
    ind=ind+zeros(ind);
    }
}
int i,j,x,y,st,dr,mij,m,C,gasit;
int main()
{
    f>>n>>m;
    for(i=1;i<=n;i++)
    {
        f>>x;
        update(i,x);
    }
    for(i=1;i<=m;i++)
    {
        f>>C;
        if(C==0)
        {
            f>>x>>y;
            update(x,y);
        }
        else
        if(C==1)
        {
            f>>x>>y;
            g<<query(y)-query(x-1)<<'\n';
        }
        else
        {
            f>>x;
            st=1;
            dr=n;
            gasit=-1;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                y=query(mij);
                if(y==x)
                {
                    gasit=mij;
                    dr=mij-1;
                }
                else
                if(y>x)
                    dr=mij-1;
                else
                    st=mij+1;
            }
            g<<gasit<<'\n';
        }
    }
    return 0;
}