Cod sursa(job #2750802)

Utilizator As932Stanciu Andreea As932 Data 13 mai 2021 12:04:06
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>

using namespace std;

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

const int nmax = 30005;

int n, m, pos, val, a, b, arb[nmax];

void update(int nod, int st, int dr, int cer){
    if(st == dr){
        if(cer == 0)
            arb[nod] = val;
        else
            arb[nod] -= val;

        return;
    }

    int mij = (st + dr) / 2;

    if(pos <= mij)
        update(2 * nod, st, mij, cer);
    else
        update(2 * nod + 1, mij + 1, dr, cer);

    arb[nod] = arb[2 * nod] + arb[2 * nod + 1];
}

int query(int nod, int st, int dr){
    if(a <= st && dr <= b)
        return arb[nod];

    int ans = 0;

    int mij = (st + dr) / 2;

    if(a <= mij)
        ans = ans + query(2 * nod, st, mij);
    else
        ans = ans + query(2 * nod + 1, mij + 1, dr);

    return ans;
}

void read(){
    fin >> n >> m;

    for(int i = 1; i <= n; i++){
        fin >> val;
        pos = i;
        update(1, 1, n, 0);
    }
}

void queries(){
    for(int i = 1; i <= m; i++){
        int t;
        fin >> t;

        if(t == 0){
            fin >> pos >> val;
            update(1, 1, n, 1);
        } else {
            fin >> a >> b;
            fout << query(1, 1, n) << "\n";
        }
    }
}

int main()
{
    read();
    queries();

    return 0;
}