Cod sursa(job #2377816)

Utilizator smoc_georgemarianSmoc George-Marian smoc_georgemarian Data 11 martie 2019 10:19:20
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#define NMAX 100010

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

int n, q;
int aib[NMAX];

inline int lsb(int x) {
    return x & (-x);
}

void update(int pos, int val) {
    for (int i = pos; i <= n; i += lsb(i))
        aib[i] += val;
}

void update1(int pos, int val) {
    for (int i = pos; i <= n; i += lsb(i))
        aib[i] -= val;
}
int query(int pos) {
    int sum = 0;
    for (int i = pos; i >= 1; i -= lsb(i))
        sum += aib[i];
    return sum;
}



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

    for (int it = 0; it < q; it++) {
        int t, x, y;
        fin >> t ;
        if (t ==1)
            {fin >>x>> y;fout<<query(y)-query(x-1)<<'\n';}
      else
        if (!t)
        {fin>>x>>y;
            update1(x, y);
        }
    }

    fout.close();
    return 0;
}