Cod sursa(job #2382051)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 17 martie 2019 17:28:01
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("aib.in");
ofstream out("aib.out");
int v[100005];
int aib[100005];
int n, m;

void update(int poz, int val)
{
    while(poz <= n)
    {
        aib[poz] += val;
        poz += (poz & (-poz));
    }
}

int query(int poz)
{
    int s = 0;
    while(poz)
    {
        s += aib[poz];
        poz -= (poz & (-poz));
    }
    return s;
}

int cautbin(int val)
{
    int st = 1, dr = n;
    while(st <= dr)
    {
        int mid = (st + dr) / 2;
        int x = query(mid);
        if(val == x)
            return mid;
        if(x < val)
            st = mid + 1;
        else
            dr = mid - 1;
    }
    return -1;
}

void citire()
{
    in >> n >> m;
    for(int i = 1; i <= n; i ++)
    {
        int x;
        in >> x;
        update(i, x);
    }
    for(int i = 1; i <= m; i ++)
    {
        int tip, a, b = 0;
        in >> tip >> a;
        if(tip == 2)
            out << cautbin(a) << '\n';
        else
        {
            in >> b;
            if(tip == 0)
                update(a, b);
            else
                out << query(b) - query(a - 1) << '\n';
        }
    }
}

int main()
{
    citire();

    return 0;
}