Cod sursa(job #3311057)

Utilizator jumaracosminJumara Cosmin-Mihai jumaracosmin Data 19 septembrie 2025 08:22:42
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>

std::ifstream fin("datorii.in");
std::ofstream fout("datorii.out");

const int NMAX = 15e3 + 5;

int n, q;
int fenwick[NMAX];

int LSB(int x) { return (x & (-x)); }

void update(int poz, int val)
{
    for(int i = poz; i <= n; i += LSB(i))
        fenwick[i] += val;
}

int query(int poz)
{
    int ans = 0;
    for(int i = poz; i >= 1; i -= LSB(i))
        ans += fenwick[i];
    return ans;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    fin >> n >> q;

    for(int i = 1; i <= n; ++i)
    {
        int x;
        fin >> x;
        update(i, x);
    }

    while(q--)
    {
        int task;
        fin >> task;

        if(task == 0)
        {
            int x, poz;
            fin >> poz >> x;
            update(poz, -x);
        }
        else if(task == 1)
        {
            int l, r;
            fin >> l >> r;
            fout << query(r) - query(l - 1) << "\n";
        }
    }
    
    return 0;
}