Cod sursa(job #962257)

Utilizator addy01adrian dumitrache addy01 Data 14 iunie 2013 10:51:27
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int AINT[1 << 16];

void update (int nod, int st, int dr, int poz, int val)
{
    if (st == dr){
        AINT[nod] += val;
        return;
    }

    int mid = (st + dr) / 2;

    if (poz < mid)
        update (2 * nod, st, mid, poz, val);
    else
        update (2 * nod + 1, mid + 1, dr, poz, val);

    AINT[nod] = AINT[nod * 2] + AINT[nod * 2 + 1];
}

int query (int nod, int st, int dr, int a, int b)
{
    if (a <= st && dr <= b)
        return AINT[nod];

    int mid = (st + dr) / 2;
    int ans = 0;

    if (a <= mid)
        ans += query (nod * 2, st, mid, a, b);
    if (mid < b)
        ans += query (nod * 2 + 1, mid + 1, dr, a, b);

    return ans;
}

int main()
{
    int N, M, i, a, b, c;

    in >> N >> M;
    for (i = 1; i <= N; i ++){
        in >> a;
        update (1, 1, N, i, a);
    }

    while (M --){
        in >> a >> b >> c;

        if (!a)
            update (1, 1, N, b + 1, -c);
        else
            out << query (1, 1, N, b + 1, c) << "\n";
    }

    return 0;
}