Cod sursa(job #3338720)

Utilizator InformaticianInDevenire1Munteanu Mihnea Gabriel InformaticianInDevenire1 Data 4 februarie 2026 18:29:02
Problema Arbori indexati binar Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

ifstream fin ("aib.in");
ofstream fout ("aib.out");

int v[200005];
int aib[200005];
int n,m;

int LSB(int x){
    return (x&(-x));
}

int Query(int pos){
    int ans = 0;
    while (pos>=1){
        int p = LSB(pos);
        ans += aib[pos];
        pos -= p;
    }
    return ans;
}

void Update(int pos,int val){
    while (pos<=n){
        int p = LSB(pos);
        aib[pos] += val;
        pos += p;
    }
}

int BinarySearch(int a){
    int p = 1<<20;
    int ans = 0;
    int sum = 0;
    while (p){
        if (ans+p<=n and sum+aib[ans+p]<a){
            sum += aib[ans+p];
            ans += p;
        }
        p /= 2;
    }
    if (sum+v[ans+1]==a) return ans+1;
    return -1;
}

signed main()
{
    fin >> n >> m;
    for (int i=1;i<=n;++i){
        fin >> v[i];
        Update(i,v[i]);
    }
    while (m--){
        int type;
        fin >> type;
        if (type==0){
            int a,b;
            fin >> a >> b;
            Update(a,b);
        }else if (type==1){
            int a,b;
            fin >> a >> b;
            fout << Query(b)-Query(a-1) << '\n';
        }else{
            int a;
            fin >> a;
            fout << BinarySearch(a) << '\n';
        }
    }
    return 0;
}