Cod sursa(job #3284946)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 12 martie 2025 13:12:51
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#define nmax 100001
using namespace std;
ifstream cin("aib.in");
ofstream cout("aib.out");
int n,q,aib[nmax],a,b,task;
void update(int p,int x){
    for(;p<=n;p+=(-p)&p)
        aib[p]+=x;
}
int query(int p){
    int r=0;
    for(;p>0;p-=(-p)&p)
        r+=aib[p];
    return r;
}
int cb(int x){
    int st=1,dr=n,p=-1;
    while(st<=dr){
        int mid=(st+dr)/2,val=query(mid);
        if(val==x)
            p=mid,dr=mid-1;
        else if(val<x)
            st=mid+1;
        else
            dr=mid-1;
    }
    return p;
}
int main()
{
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>a;
        update(i,a);
    }
    while(q--){
        cin>>task>>a;
        if(task==2){
            cout<<cb(a)<<'\n';
            continue;
        }
        cin>>b;
        if(task==0)
            update(a,b);
        else
            cout<<query(b)-query(a-1)<<'\n';
    }
    return 0;
}