Cod sursa(job #2410258)

Utilizator Vlad_NituNitu Vlad-Petru Vlad_Nitu Data 19 aprilie 2019 20:48:49
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <bits/stdc++.h>
#define DIM 100005
using namespace std;
ifstream f ("aib.in") ;
ofstream g ("aib.out") ;
int N , M , x;
int cer , a , b;
int AIB[DIM] ;
void Update (int poz , int val)
{
    for (int i = poz ; i <= N ; i += i & (-i))
        AIB[i] += val ;
    return ;
}
int Query(int poz)
{
    int sum = 0 ;
    for (int i = poz ; i >= 1 ; i -= i & (-i))
        sum += AIB[i] ;
    return sum ;
}
int main()
{
    f >> N >> M ;
    for (int i = 1 ; i <= N ; ++i)
        f >> x , Update(i,x) ;
    for (int i = 1 ; i <= M ; ++i)
    {
        f >> cer >> a ;
        if (cer == 0) f >> b ,Update(a,b);
        else if (cer == 1) f >> b , g << Query(b) - Query(a-1) << '\n';
        else
        {
                int st = 1 , dr = N , mij , val ; // cautare binara
                bool OK = true ;
                while (OK && st <= dr)
                {
                    mij = (st + dr) / 2;
                    if (Query(mij) == a)OK = false , val = mij;
                    else if (a < Query(mij)) dr = mij - 1;
                    else st = mij + 1 ;
                }
                if (OK) // nu am gasit val
                    val = -1;
                g << val << '\n' ;
        }

    }
    f.close();
    g.close() ;
    return 0 ;
}