Cod sursa(job #3154977)

Utilizator PitigoiOlteanEmanuelPitigoi Oltean Emanuel PitigoiOlteanEmanuel Data 6 octombrie 2023 23:25:21
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream cin("aib.in");
ofstream cout("aib.out");
int n;
int v[100001], aib[100001];
int sum;

void update (int x,int val)
{
        while(x<=n)
        {
            aib[x]+=val;
            x+=x&(-x);

        }

}

int prefix(int x)
{
    sum=0;
     while(x>0)
        {
            sum+=aib[x];

            x-=x&(-x);

        }
        return sum;
}

int main()
{
    int k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i];


    }

    for(int i=1;i<=n;i++)
    {
        update(i,v[i]);

    }


    for(int i=1;i<=k;i++)
    {
        int x,a,b;
        cin>>x;
        if(x==0)
        {
            cin>>a>>b;
            update(a,b);

        }
        else if(x==1)
        {

            cin>>a>>b;

            cout<<prefix(b)-prefix(a-1)<<'\n';
        }
        else
        {
            cin>>a;
            int st=1,dr=n+1,mij;
             int y;
            while(st<dr)
            {
                mij=(st+dr)/2;

        y=prefix(mij);

                if(y==a)
                {
                    break;

                }
                if(y>a)
                {
                    dr=mij;
                }
                else
                {
                    st=mij+1;
                }

            }
            if(y==a)
            cout<<mij<<'\n';
            else
            {
                cout<<-1<<'\n';
            }

        }

    }








    return 0;

}