Cod sursa(job #2189813)

Utilizator bigmixerVictor Purice bigmixer Data 29 martie 2018 09:32:26
Problema Arbori indexati binar Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 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
const int mod=1e9+7;
const int inf=1e5;
using namespace std;
std::set<int>::iterator it;
std::set<int>::iterator it1;

long long aib[100005];
long long lol,m,n;
int a[100005];
int t,x,y;

long long par(int p);

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>= aib[i+step]){
                i+= step, val-=aib[i];
                if (!val) return i;
            }
         }
    }

    return -1;
}


void upd(long long v,long long g){
     while(v<=n){
        aib[v]+=g;
        v+=v&-v;
     }
}

long long par(int p){
     lol=0;
     while(p){
        lol+=aib[p];
        p-=p&-p;
     }
     return lol;
}


int main(){ _
    ifstream fin("aib.in");
    ofstream fout("aib.out");
    fin >> n >> m;
    for(int i=1;i<=n;i++){
        fin >> a[i];
        upd(i,a[i]);
    }
    while(m--){
        fin >> t;
        if(t==0){
            fin >> x >> y;
            upd(x,y);
            continue;
        }
        if(t==1){
            fin >> x >> y;
            fout<<par(y)-par(x-1)<<"\n";
            continue;
        }
        if(t==2){
            fin >> x;
            fout<<searcha(x)<<endl;
            continue;
        }
    }

}