Cod sursa(job #2901049)

Utilizator RobertuRobert Udrea Robertu Data 12 mai 2022 19:50:43
Problema Datorii Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <fstream>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
 
int arb[60008];
 
int getSum(int nod, int left, int right, int pozS, int pozD) {
    if(pozS > right || pozD < left) return 0;
 
    if( pozS <= left && right <= pozD ) return arb[nod];
 
    int mij = (left + right) / 2;
    return getSum(nod * 2, left, mij, pozS, pozD) + getSum(nod * 2 + 1, mij + 1, right,  pozS, pozD);
}
 
inline void updateElement(int nod, int left, int right, int poz, int val) {
    if(left == right) {
        arb[nod] -= val;
        return;
    }
 
    int mij = (left + right) / 2;
 
    if( mij >= poz )
        updateElement(nod * 2, left, mij, poz, val);
    else 
        updateElement(nod * 2 + 1, mij + 1, right, poz, val);
 
    arb[nod] = arb[nod * 2] + arb[nod * 2 + 1];
}
 
inline void setElement(int nod, int left, int right, int poz, int val) {
    if(left == right) {
        arb[nod] = val;
        return;
    }
 
    int mij = (left + right) / 2;
 
    if( mij >= poz )
        setElement(nod * 2, left, mij, poz, val);
    else 
        setElement(nod * 2 + 1, mij + 1, right, poz, val);
 
    arb[nod] = arb[nod * 2] + arb[nod * 2 + 1];
}
 
int main() {
 
    int n, m, a, b, op;
    fin >> n >> m;
 
    for(int i = 1; i <= n; i++) {
        fin >> a;
        setElement(1, 1, n, i, a);
    }
 
    for(int i = 0; i < m; i++) {
        fin >> op >> a >> b;
 
        if(op) fout << getSum(1, 1, n, a, b) << '\n';
        else
            updateElement(1, 1, n, a, b);
    }
 
    return 0;
}