Cod sursa(job #2761811)

Utilizator bestman4111Tiberiu Niculae bestman4111 Data 4 iulie 2021 02:57:33
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include<iostream>
#include<fstream>
using namespace std;

ifstream cit("datorii.in");
ofstream afis("datorii.out");

int N, M, arbore[100001], vec[15001], op, x, y, num;

void constrArbore(int st, int dr, int index){
    int mij;
    if(st == dr){
        arbore[index] = vec[st];
    }
    else{
        mij = (st + dr)/2;
        constrArbore(st, mij, 2 * index);
        constrArbore(mij + 1, dr, 2 * index + 1);
        arbore[index] = arbore[2 * index] + arbore[2 * index + 1];
    }
}

int interogare(int st, int dr, int index, int x, int y){
    int suma1, suma2, mij;
    if((x <= st) && (y >= dr)){
        return arbore[index];
    }
    else{
        suma1 = 0;
        suma2 = 0;
        mij = (st + dr)/2;
        if(x <= mij){
            suma1 = interogare(st, mij, 2 * index, x, y);
        }
        if(y > mij){
            suma2 = interogare(mij + 1, dr, 2 * index + 1, x, y);
        }
        suma1 += suma2;
        return suma1;
    }
}

void plateste(int st, int dr, int index, int x, int y){
    int mij;
    if(st == dr){
        arbore[index] -= y;
    }
    else{
        mij = (st + dr)/2;
        if(x <= mij){
            plateste(st, mij, 2 * index, x, y);
        }
        else{
            plateste(mij + 1, dr, 2 * index + 1, x, y);
        }
        arbore[index] = arbore[2 * index] + arbore[2 * index + 1];
    }
}

int main()
{
    cit>>N>>M;
    for(int i = 1; i <= N; i++){
        cit>>vec[i];
    }
    constrArbore(1, N, 1);
    for(int i = 1; i <= M; i++){
        cit>>op>>x>>y;
        if(op == 0){
            plateste(1, N, 1, x, y);
        }
        else{
            num = interogare(1, N, 1, x, y);
            afis<<num<<"\n";
        }
    }
    return 0;
}