Cod sursa(job #2536263)

Utilizator victorzarzuZarzu Victor victorzarzu Data 1 februarie 2020 18:10:59
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;
#define zeros(x) ((x ^ (x - 1)) & x)
ifstream f("datorii.in");
ofstream g("datorii.out");
typedef unsigned long long unll;
unll n,m;
unll AIB[15001];

void Add(unll pos,unll quantity)
{
    for(int i = pos;i <= n;i += zeros(i))
        AIB[i] += quantity;
}

unll Query(unll pos)
{
    unll sum = 0;
    for(int i = pos;i > 0;i -= zeros(i))
        sum += AIB[i];
    return sum;
}

void Read()
{
    unll x;
    unll type, a, b;
    f>>n>>m;
    for(int i = 1;i <= n;++i)
    {
        f>>x;
        Add(i, x);
    }
    for(int i = 1;i <= m;++i)
    {
        f>>type>>a>>b;
        switch(type)
        {
            case 0:
                Add(a, -b);
                break;
            case 1:
                g<<Query(b) - Query(a - 1)<<'\n';
                break;
        }
    }
    f.close();
    g.close();
}
int main()
{
    Read();
    return 0;
}