Cod sursa(job #2497705)

Utilizator cristina-criCristina cristina-cri Data 23 noiembrie 2019 10:23:13
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <cstdio>

using namespace std;

int n, m, x, aib[15005], cer, poz, pozi, pozj;

int zeroes(int x)
{
    return ((x^(x-1))&x);
}

void add(int poz, int x)
{
    while(poz <= n)
    {
        aib[poz]+=x;
        poz=poz+zeroes(poz);
    }

}

void achit(int poz, int x)
{
    while(poz <= n)
    {
        aib[poz]-=x;
        poz=poz+zeroes(poz);
    }

}

long long sum(int i)
{
    long long s=0;
    while(i>0)
    {
        s=s+aib[i];
        i=i-zeroes(i);
    }
    return s;
}

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

    scanf("%d %d", &n, &m);
    for(int i=1; i <= n; i++)
    {
        scanf("%d", &x);
        add(i, x);
    }
    for(int i=1; i <= m; i++)
    {
        scanf("%d", &cer);
        if(cer == 0)
        {
            scanf("%d %d", &poz, &x);
            achit(poz, x);
        }
        else
        {
            scanf("%d %d", &pozi, &pozj);
            printf("%lld\n", sum(pozj)-sum(pozi-1));
        }
    }
    return 0;
}