Mai intai trebuie sa te autentifici.

Cod sursa(job #3364862)

Utilizator RobertIon013Ion Robert Andrei RobertIon013 Data 12 septembrie 2026 16:22:00
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.73 kb
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream fin ("aib.in");
ofstream fout ("aib.out");
struct AIB
{
    int sz;
    vector<long long> a;
    AIB(int n)
    {
        sz=n;
        a.assign(n+2,0);
    }
    void add(long long x,long long v)
    {
        for(;x<=sz;x+=x&-x)a[x]+=v;
    }
    long long query(long long x)
    {
        long long s=0;
        for(;x>0;x-=x&-x)s+=a[x];
        return s;
    }
    long long queryX(int l,int r)
    {
        if(l>r)return 0;
        return query(r)-query(l-1);
    }
};
int main()
{
    int N,M;
    fin>>N>>M;
    vector<int> v(N+1);
    AIB bit(N);
    for(int i=1;i<=N;i++)
    {
        fin>>v[i];
        bit.add(i,v[i]);
    }
    while(M--)
    {
        int tip;
        fin>>tip;
        if(tip==0)
        {
            int a,b;
            fin>>a>>b;
            bit.add(a,b);
        }
        else
        if(tip==1)
        {
            int a,b;
            fin>>a>>b;
            fout<<bit.queryX(a,b)<<'\n';
        }
        else
        if(tip==2)
        {
            int a,k=0;
            fin>>a;
            int st=1;
            int dr=N;
            while(st<=dr)
            {
                int mij=(st+dr)/2;
                if(bit.query(mij)==a)
                {
                    k=mij;
                    break;
                }
                else
                if(bit.query(mij)<a)
                {
                    st=mij+1;
                }
                else
                {
                    dr=mij-1;
                }
            }
            if(k>0)fout<<k<<'\n';
            else
            fout<<-1<<'\n';
        }
    }

    return 0;
}