Cod sursa(job #3309575)

Utilizator MrPetcuPetcu Robert MrPetcu Data 6 septembrie 2025 15:05:47
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>

using namespace std;

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

int n, m;
int x, y;
int tip;
int aib[100005];

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

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

int suma(int x, int y) {
    if(x != 1){
        return suma(1, y) - suma(1, x-1);
    }
    int suma_tot = 0;
    while(y > 0) {
        suma_tot += aib[y];
        y -= (y & (-y));
    }
    return suma_tot;
}

int main(){
    fin >> n >> m;
    for(int i = 1; i <= n; i++) {
        fin >> x;
        adaugare(i, x);
    }
    for(int i = 1; i <= m; i++) {
        fin >> tip;
        if(tip == 0) {
            fin >> x >> y;
            scadere(x, y);
        }
        else if(tip == 1) {
            fin >> x >> y;
            fout << suma(x, y) << '\n';
        }
    }
    return 0;
}