Cod sursa(job #1487337)

Utilizator dec0o0dinu pinu dec0o0 Data 16 septembrie 2015 18:04:22
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;

vi v(16000000);

inline void add(const int nod, const int left, const int right, const int poz, const int val){
    if (left == right){
        v[nod] += val;
        return;
    }
    
    int mij = (left + right) / 2;
    if (poz <= mij)
        add(2 * nod, left, mij, poz, val);
    else
        add(2 * nod + 1, mij + 1, right, poz, val);
    
    v[nod] = v[2 * nod] + v[2 * nod + 1];
}

inline int sum(const int nod, const int left, const int right, const int a, const int b){
    if (left >= a and right <= b)
        return v[nod];
    
    int mij = (left + right) / 2;
    int s = 0;
    if (a <= mij)
        s = sum(2 * nod, left, mij, a, b);
    if (mij < b)
        s += sum(2 * nod + 1, mij + 1, right, a, b);
    
    return s;
}

int main(int argc, const char * argv[]) {
    freopen("datorii.in", "r", stdin);
    freopen("datorii.out", "w", stdout);
   
    int n, m;
    cin >> n >> m;
    
    int x;
    for (int i = 1; i <= m; i++) {
        cin >> x;
        add(1, 1, n, i, x);
    }
    
    int a, b;
    while (m--){
        cin >> x >> a >> b;
        if (x)
            cout << sum(1, 1, n, a, b) << '\n';
        else
            add(1, 1, n, a, -b);
        
    }
    
    return 0;
}