Cod sursa(job #2748658)

Utilizator As932Stanciu Andreea As932 Data 2 mai 2021 10:03:01
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 15e3 + 5;

int n, m, aib[nmax];

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

void read(){
    fin >> n >> m;

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

int query(int pos){
    int sum = 0;

    for(; pos > 0; pos -= pos & -pos)
        sum += aib[pos];

    return sum;
}

void solve(){
    while(m--){
        int tip, a, b;
        fin >> tip >> a >> b;

        if(tip == 0)
            update(a, (-1) * b);
        else {
            int ans = query(b) - query(a - 1);
            fout << ans << "\n";
        }
    }
}

int main()
{
    read();
    solve();

    return 0;
}