Cod sursa(job #2189855)

Utilizator bigmixerVictor Purice bigmixer Data 29 martie 2018 10:59:04
Problema Arbori indexati binar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>
#define sz size
#define pb push_back
#define er erase
#define in insert
#define fr first
#define sc second
#define mp make_pair
#define pi pair
#define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
#define rc(s) return cout<<s,0
#define int long long
const int mod=1e9+7;
const int inf=1e5;
using namespace std;
std::set<int>::iterator it;
std::set<int>::iterator it1;

int arb[100005],m,n,lel,t,x,y;

int searcha(int val){
    int i,step;
    for(step=1;step<n;step<<=1);
    for(i=0;step>0;step>>=1){
         if(i+step<=n){
            if(val>=arb[i+step]){
                i+=step, val-=arb[i];
                if(!val) return i;
            }
         }
    }
    return -1;
}

void upd(int q,int k){
    while(q<=n){
        arb[q]+=k;
        q+=(q&-q);
    }
}

int sum(int q){
    lel=0;
    while(q>0){
        lel+=arb[q];
        q-=(q&-q);
    }
    return lel;
}

signed main(){ _
    ifstream fin("aib.in");
    ofstream fout("aib.out");
    fin >> n >> m;
    for(int i=1;i<=n;i++){
        fin >> x;
        upd(i,x);
    }
    for(int j=1;j<=m;j++){
        fin >> t;
        if(t==0){ fin >> x >> y; upd(x,y); continue;}
        if(t==1){ fin >> x >> y; fout<<sum(y)-sum(x-1)<<'\n'; continue; }
        if(t==2){ fin >> x; fout<<searcha(x)<<'\n'; continue;}
    }
}