Cod sursa(job #2615846)

Utilizator stanbianca611Stan Bianca stanbianca611 Data 15 mai 2020 18:05:34
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <bits/stdc++.h>
#define bit(x) ( x & (-x) )
using namespace std;

const int NMAX = 15005;
int aib[NMAX], n, q;

ifstream f("datorii.in");
ofstream g("datorii.out");

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

int query(int pos) {
    int sum = 0;
    for(int i = pos; i > 0; i -= bit(i)) {
        sum += aib[i];
    }
    return sum;
}

int main()
{
    f >> n >> q;
    for(int i = 1; i <= n; i++) {
        int x;
        f >> x;
        update(i, x);
    }
    while(q--) {
        int op;
        f >> op;
        if(op == 0) {
            int pos, val;
            f >> pos >> val;
            update(pos, -val);
        } else {
            int st, dr;
            f >> st >> dr;
            g << query(dr) - query(st - 1) << '\n';
        }
    }
    return 0;
}