Cod sursa(job #2901098)

Utilizator crastanRavariu Eugen crastan Data 12 mai 2022 22:08:52
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
void update(int *arbIB, int n, int poz, int val){
    int ind = poz+1;
    while(ind <= n){
        arbIB[ind] += val;
        ind = ind + (ind&(-ind));
    }

}

 int query(int *arbIB, int poz){
     int s = 0;
    int ind = poz;
    while(ind > 0){
        s += arbIB[ind];
        ind = ind - (ind&(-ind));

    }
    return s;
}


int main()
{
    int n, m;
    fin >> n >> m;
     int arbIB[n+5]={0};

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

    for(int i = 0; i < m; i++){
         int t,x,y;
        fin >> t ;
        if(t == 1){
            fin >> x >> y;
            fout << query(arbIB, y) - query(arbIB, x - 1) << '\n';
        } else if(t == 0){
            fin >> x >> y;
            update(arbIB, n, x-1, -y);
        }
    }
    return 0;
}