Cod sursa(job #1039123)

Utilizator Alexghita96Ghita Alexandru Alexghita96 Data 22 noiembrie 2013 16:19:34
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <cstdio>

#define Nmax 40010

int N, M, A[Nmax], poz, nr, a, b, Suma;

void Update(int li, int lf, int ind)
{
    if (li == lf)
    {
        A[ind] += nr;
        return;
    }
    int mij = (li + lf) / 2;
    if (poz <= mij)
        Update(li, mij, 2 * ind);
    else
        Update(mij + 1, lf, 2 * ind + 1);
    A[ind] = A[2 * ind] + A[2 * ind + 1];
}

void Citire()
{
    scanf("%d %d", &N, &M);
    for (poz = 1; poz <= N; ++poz)
    {
        scanf("%d", &nr);
        Update(1, N, 1);
    }
}

void Query(int li, int lf, int ind)
{
    if (a <= li && lf <= b)
    {
        Suma += A[ind];
        return;
    }
    int mij = (li + lf) / 2;
    if (a <= mij)
        Query(li, mij, 2 * ind);
    if (b > mij)
        Query(mij + 1, lf, 2 * ind + 1);
}

void Operatii()
{
    int op;
    for (int i = 1; i <= M; ++i)
    {
        scanf("%d %d %d", &op, &a, &b);
        if (!op)
        {
            poz = a;
            nr = -b;
            Update(1, N, 1);
        }
        else
        {
            Suma = 0;
            Query(1, N, 1);
            printf("%d\n", Suma);
        }
    }
}

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

    Citire();
    Operatii();

    return 0;
}