Cod sursa(job #3237602)

Utilizator TomMMMMatei Toma TomMMM Data 10 iulie 2024 16:09:41
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N_max = 15005;

int n, m;
int v[N_max];
long long aint[2*N_max + 1];
void Build(int nod, int s, int d){
    if(s == d){
        aint[nod] = v[s];
        return;
    }
    int mij = (s + d)/ 2;
    Build(2 * nod, s, mij);
    Build(2 * nod + 1, mij + 1, d);
    aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
}
void replace(int nod, int s, int d, int poz, int val){
    if(s == d) {
        v[poz] -= val;
        aint[nod] = v[poz];
        return;
    }
    int mij = (s + d) / 2;
    if(poz <= mij)
        replace(2 * nod, s, mij, poz, val);
    else
        replace(2 * nod + 1, mij + 1, d, poz, val);

    aint[nod] = 1LL *(aint[2 * nod] + aint[2 * nod + 1]);
}
long long  answer(int nod, int s, int d, int x, int y){
    if(s == x && d == y)
        return aint[nod];
    int mij = (s + d) / 2;
    if(y <= mij){
        return answer(2 * nod, s, mij, x, y);
    }else{
        if(x > mij){
            return answer(2 * nod + 1, mij + 1, d, x, y);
        }else{
            return answer(2 * nod, s, mij, x, mij) + answer(2 * nod + 1, mij + 1, d, mij + 1, y);
        }
    }
}

int main() {
    fin >> n >> m;
    for(int i = 1; i <= n; i++) fin >> v[i];
    Build(1, 1, n);
    while(m--){
        bool tip; fin >> tip;
        if(tip == 0){
            int i, val; fin >> i >> val;
            replace(1, 1, n, i, val);
        }else{
            int x, y;fin >> x >> y;
            fout << answer(1, 1, n, x, y) << '\n';
        }
    }
}