Cod sursa(job #2998936)

Utilizator suzanicaSuzanica Mihu suzanica Data 10 martie 2023 11:40:11
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>
#define next(x) ((x^(x-1))&x)
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int aib[100011],n;
void update(int x,int poz)
{
    while(poz<=n)
    {
        aib[poz]=aib[poz]+x;
        poz=poz+next(poz);
    }
}
int query(int poz)
{
    int s=0;
    while(poz)
    {
        s=s+aib[poz];
        poz=poz-next(poz);
    }
    return s;
}
int m,x,i,C,y,st,dr,found,mij,s;
int main()
{
    f>>n>>m;
    for(i=1;i<=n;i++)
    {
        f>>x;
        update(x,i);
    }
    for(i=1;i<=m;i++)
    {
        f>>C;
        if(C==0)
        {
            f>>x>>y;
            update(y,x);
        }
        else
        if(C==1)
        {
            f>>x>>y;
            g<<query(y)-query(x-1)<<'\n';
        }
        else
        {
            f>>x;
            st=1;
            dr=n;
            found=-1;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                s=query(mij);
                if(s==x)
                {
                    found=mij;
                    dr=mij-1;
                }
                else
                if(s>x)
                    dr=mij-1;
                else
                    st=mij+1;
            }
            g<<found<<'\n';
        }
    }
    return 0;
}