Cod sursa(job #2541992)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 9 februarie 2020 12:08:27
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, a, b;
int aib[150050];

bool cod;

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

int query (int poz){
    int rez = 0;
    for (; poz>0; poz -= (poz&(-poz))){
        rez += aib[poz];
    }
    return rez;
}

int main(){
    fin >> n >> m;
    for (int i=1; i<=n; i++){
        fin >> a;
        update (i, a);
    }
    for (int i=1; i<=m; i++){
        fin >> cod >> a >> b;
        if (cod == 1){
            fout << query (b) - query (a-1) << "\n";
        }
        else if (cod == 0){
            update (a, -b);
        }
    }
    return 0;
}