Cod sursa(job #3204939)

Utilizator prod_cristiAnghel Cristi prod_cristi Data 18 februarie 2024 13:00:00
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("aib.in");
ofstream g("aib.out");

#define nmax 100000

int v[nmax], aib[nmax], n, m;
void update(int ai, int val)
{
    for(int i = ai;i <= n;i += i &(-i)) 
        aib[i] += val;
}

long long int suma(int a)
{
    long long sum = 0;
    for(int i = a; i > 0; i -= i &(-i))
        sum += aib[i];
    
    return sum;
}

void cautare(int st, int dr, int val)
{
    while(st <= dr)
    {
        int mid = (st + dr) /2;
        if(suma(mid) >= val)
            dr = mid - 1;
        else st = mid + 1;
    }
    if(suma(st) == val)
        g << st <<"\n";
    else
        g <<-1<<'\n';
}
void citire()
{
    f >> n >> m;
    for(int i = 1;i <= n;i ++)
    {
        f >> v[i];
        update(i, v[i]);
    }
}
void rez()
{
    for(int i = 1; i <= m;i ++)
    {
        int tip;
        f >>tip;
        if(tip == 0)
        {
            int a, b;
            f >>  a >> b;
            update(a, b);
        }
        else if(tip == 1)
        {
            int a, b;
            f >> a >> b;
            g << suma(b) - suma(a - 1) <<'\n';
        }
        else
        {
            int a;
            f >> a;
            cautare(1, n, a);
        }
    }
}
int main(){
    
    citire();
    rez();
    return 0;
}