Cod sursa(job #2752239)

Utilizator As932Stanciu Andreea As932 Data 17 mai 2021 09:49:47
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, arb[60100];

void update(int nod, int st, int dr, int pos, int val){
    if(st == dr){
        arb[nod] = val;
        return;
    }

    int mij = (st + dr) / 2;

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

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

void update1(int nod, int st, int dr, int pos, int val){
    if(st == dr){
        arb[nod] -= val;
        return;
    }

    int mij = (st + dr) / 2;

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

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

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

    int ans = 0;

    int mij = (st + dr) / 2;

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

    return ans;
}

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

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

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

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

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

    return 0;
}