Cod sursa(job #1889238)

Utilizator cosmin_candreaCosmin Candrea cosmin_candrea Data 22 februarie 2017 17:17:38
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>

using namespace std;

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

int n, m, x, y, z, v[100003], arb[4 * 100010], ans;

void update(int nod, int left, int right, int poz, int val)
{
    int mij = (left + right) >> 1;

    if(left == right)
    {
        arb[nod] += val;
        return;
    }

    if(poz <= mij)
        update(2 * nod, left, mij, poz, val);
    else
        update(2 * nod + 1, mij + 1, right, poz, val);

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

void query(int nod, int left, int right, int L, int R)
{
    int mij = (left + right) / 2;

    if(left >= L && right <= R)
    {
        ans += arb[nod];
        return;
    }

    if(left > R || right < L)
        return;

    query(2 * nod, left, mij, L, R);
    query(2 * nod + 1, mij + 1, right, L, R);
}

int main()
{
    F >> n >> m;
    for(int i = 1; i <= n; ++ i)
    {
        F >> x;
        update(1, 1, n, i ,x);
        v[i] = x;
    }

    for(int i = 0; i < m; ++ i)
    {
        F >> z >> x >> y;
        if(!z)
            update(1, 1, n, x, -y);
        else
        {
            ans = 0;
            query(1, 1, n, x, y);
            G << ans << '\n';
        }
    }
    return 0;
}