Cod sursa(job #1783780)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 19 octombrie 2016 14:22:56
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int arb[30010], n;

inline void build ()
{
    for (int i = n - 1; i; --i)
        arb[i] = arb[i << 1] + arb[(i << 1) + 1];
}

inline void update (int poz, int val)
{
    arb[poz += n - 1] -= val;
    for (poz >>= 1; poz; poz >>= 1)
        arb[poz] = arb[poz << 1] + arb[(poz << 1) + 1];
}

inline int querry (int st, int dr)
{
    int sum = 0;
    for (st += n - 1, dr += n; st < dr; st >>= 1, dr >>= 1)
    {
        if (st & 1) sum += arb[st++];
        if (dr & 1) sum += arb[--dr];
    }

    return sum;
}

int main ()
{
    freopen ("datorii.in", "r", stdin);
    freopen ("datorii.out", "w", stdout);

    int m;
    scanf ("%d %d", &n, &m);

    for (int i = 0; i < n; ++i)
        scanf ("%d", &arb[i + n]);

    build ();

    for (int i = 1; i <= m; ++i)
    {
        int op, x, y;
        scanf ("%d %d %d", &op, &x, &y);

        if (op) printf ("%d\n", querry (x, y));
        else update (x, y);
    }

    return 0;
}