Cod sursa(job #2912772)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 10 iulie 2022 16:49:53
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.94 kb
#include <bits/stdc++.h>
#define NMAX 100005

using namespace std;

/*******************************/
// INPUT / OUTPUT

ifstream f("aib.in");
ofstream g("aib.out");
/*******************************/
/// GLOBAL DECLARATIONS

int N, M;
int aib[NMAX];
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
void update(int val, int pos)
{
    while (pos <= N)
    {
        aib[pos] += val;
        pos += pos & (-pos);
    }
}
///-------------------------------------
inline void ReadInput()
{
    f >> N >> M;

    int num;
    for (int i = 1 ; i <= N ; ++ i)
    {
        f >> num;
        update(num, i);
    }
}
///-------------------------------------
int query1(int pos)
{
    int sum = 0;
    while (pos)
    {
        sum += aib[pos];
        pos -= pos & (-pos);
    }

    return sum;
}
///-------------------------------------
int query2(int target)
{
    int step = (1 << int(log2(N)));

    int sum = 0, pos = 0;

    while (step)
    {
        if (pos + step <= N && sum + aib[sum + step] < target)
        {
            pos += step;
            sum += aib[step];
        }
        step >>= 1;
    }

    return query1(pos + 1) == target ? pos + 1 : -1;
}
///-------------------------------------
inline void Solution()
{
    int tip, a, b;
    while (M --)
    {
        f >> tip;

        if (tip == 0)
        {
            f >> a >> b;
            update(b, a);
        }
        else
        {
            if (tip == 1)
            {
                f >> a >> b;
                g << query1(b) - query1(a - 1) << "\n";
            }
            else
            {
                f >> a;
                // g << query2(a) << "\n";
            }
        }
    }
}
///-------------------------------------
inline void Output()
{

}
///-------------------------------------
int main()
{
    ReadInput();
    Solution();
    Output();
    return 0;
}