Cod sursa(job #3292702)

Utilizator Luca_georgescuLucageorgescu Luca_georgescu Data 9 aprilie 2025 00:24:40
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("aib.in");
ofstream g("aib.out");

int q;
int n,m,a[100005],aib[100005],x,y;
int sp[100005],k=-1;

int query(int i)
{
    int sum=0;
    while ( i>0 )
    {
        sum+=aib[i];
        i-=(i&-i);
    }
    return sum;
}

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

void cb()
{
    int st=1,dr=n;
    k=-1;

    while ( st<=dr )
    {
        int m=(st+dr)/2;
        if ( query(m)<x )
            st=m+1;
        else if ( query(m)>x )
            dr=m-1;
        else if ( query(m)==x )
        {
            k=m;
            return;
        }
    }
}

int main()
{
    f >> n >> m;
    for (int i=1; i<=n; i++ )
    {
        f >> x;
        sp[i]=sp[i-1]+x;
        update(i,x);
    }

    for (int i=1; i<=m; i++ )
    {
        f >> q;
        if ( q==2 )
            f >> x;
        else f >> x >> y;

        if ( q==0 )
            update(x,y);

        else if ( q==1 )
            g << query(y)-query(x-1) << '\n';

        else if ( q==2 )
        {
            cb();
            g << k << '\n';
        }
    }
    return 0;
}