Cod sursa(job #2911585)

Utilizator StefannnnnStefan Stefannnnn Data 30 iunie 2022 16:48:10
Problema Arbori indexati binar Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.87 kb
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <fstream>
using namespace std;
ifstream cin("aib.in");
ofstream cout("aib.out");
const int NMAX = 100001;
int v[NMAX], n;
long long AIB[NMAX];
void set(int i, int val){
    while(i<=n){
        AIB[i] += val;
        i += i & (-i);
    }
}

long long sum1(int i, int j){
    long long sum=0;
    i--;
    while(j>i){
        sum+=AIB[j];
        j-=j&(-j);
    }
    while(i>j){
        sum-=AIB[i];
        i-=i&(-i);
    }
    return sum;
}


long long suma(int i){
    long long sum=0;
    while(i>0){
        sum += AIB[i];
        i -= i & (-i);
    }
    return sum;
}
int main()
{
    cin>>n;
    int q;
    cin>>q;
    for(int i=1; i<=n; i++){
        cin>>v[i];
    }
    for(int i=1; i<=n; i++){
        set(i, v[i]);
    }
    for(int t=0; t<q; t++){
        int a;
        cin>>a;
        if(a==0){
            int x, y;
            cin>>x>>y;
            set(x, y);
            //cout<<0;
        }
        else if(a==1){
            int x, y;
            cin>>x>>y;
            cout<<sum1(x, y)<<endl;
            //cout<<1;
        }
        else{
            int x;
            cin>>x;
            int r=n+1, l=1;
            while(l<r){
                int mid = (r+l)/2;
                if(suma(mid) < x)
                    l = mid+1;
                else r = mid;
                //cout<<l<<" "<<r<<endl;
            }
            if(suma(r) == x)
            cout<<r<<endl;
            else cout<<-1<<endl;
        }
    }
    return 0;
}