Cod sursa(job #3372638)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 4 noiembrie 2026 16:00:43
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
#define lsb(i) (i & (-i))
const int NMAX = 15001;
int n, q, arr[NMAX];

class FenwickTree
{
private:
    int tree[NMAX];

public:
    void update(int x, int val)
    {
        for(int i = x; i <= n; i += lsb(i))
            tree[i] += val;
    }

    int query(int x)
    {
        int sum = 0;
        for(int i = x; i > 0; i -= lsb(i))
            sum += tree[i];
        return sum;
    }
};
FenwickTree aib;

int main()
{
    fin >> n >> q;
    for(int i = 1; i <= n; i++)
    {
        fin >> arr[i];
        aib.update(i, arr[i]);
    }
    while(q--)
    {
        int tip;
        fin >> tip;
        if(tip == 0)
        {
            int x, val;
            fin >> x >> val;
            aib.update(x, -val);
        }
        else
        {
            int st, dr;
            fin >> st >> dr;
            fout << aib.query(dr) - aib.query(st - 1) << "\n";
        }
    }

    fin.close();
    fout.close();
    return 0;
}