Cod sursa(job #3131821)

Utilizator DariusM17Murgoci Darius DariusM17 Data 21 mai 2023 18:07:14
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map> 
#include <map>
#include <set>
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
const int NMAX = 1e5 + 5;
vector <int> v(NMAX), aint(NMAX * 4);
int n, m;
void build(int nod, int st, int dr) {
    if (st == dr) {
        aint[nod] = v[st];
        return;
    }
    int mij = (st + dr) >> 1;
    build(2 * nod, st, mij);
    build(2 * nod + 1, mij + 1, dr);
    aint[nod] = aint[2 * nod + 1] + aint[2 * nod];
}
void update(int nod, int  st, int dr, int pos, int val) {
    if (st == dr) {
        aint[nod] -= val;
        return;
    }
    int mij = (st + dr) >> 1;
    if (pos <= mij) update(2 * nod, st, mij, pos, val);
    else update(2 * nod, mij + 1, dr, pos, val);
    aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
}
int query(int nod, int st, int dr, int x, int y) {
    if (st > y || dr < x) return 0;
    if (x <= st && dr <= y) return aint[nod];
    int mij = (st + dr) >> 1;
    return query(2 * nod, st, mij, x, y) + query(2 * nod + 1, mij + 1, dr, x, y);
}
int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; ++i) fin >> v[i];
    build(1, 1, n);
    for (int i = 1, op, x, y; i <= m; ++i) {
        fin >> op >> x >> y;
        if (op == 1) fout << query(1, 1, n, x, y)<<'\n';
        else update(1, 1, n, x, y);
    }
    return 0;
}