Cod sursa(job #1100857)

Utilizator 2dorTudor Ciurca 2dor Data 7 februarie 2014 16:13:52
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int MAXN = 15005;
int N, M, tree[MAXN];

void Update(int index, int val) {
    while (index <= N) {
        tree[index] -= val;
        index += index & -index;
    }
}

int Query(int index) {
    int sum = 0;
    while (index) {
        sum += tree[index];
        index -= index & -index;
    }
    return sum;
}

void Read() {
    fin >> N >> M;
    int nr;
    for (int i = 1; i <= N; ++i) {
        fin >> nr;
        Update(i, nr);
    }
}

int main() {
    Read();
    int op, a, b;
    while (M--) {
        fin >> op >> a >> b;
        if (op)
            fout << Query(b) - Query(a - 1) << '\n';
        else
            Update(a, b);
    }
    fin.close();
    fout.close();
    return 0;
}