Cod sursa(job #1761074)

Utilizator din99danyMatei Daniel din99dany Data 21 septembrie 2016 19:16:01
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <cstdio>
using namespace std;

#define NMAX 15005

int arbore[ NMAX ];
int n;

void add( int poz, int k );
int query( int poz );

int main()
{

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

    int m, i, j, x, y, z, t;

    scanf("%d%d",&n,&m);
    for( i = 1; i <= n; ++i ){
        scanf("%d",&x);
        add( i, x );
    }

    while( m-- ){
        scanf("%d%d%d",&t,&x,&y);
        if( !t ) add( x, -y );
        else printf("%d\n",query( y ) - query( x - 1 ));
    }

    return 0;

}

void add( int poz, int k ){
    while( poz <= n ){
        arbore[ poz ] += k;
        poz += ( poz & -poz );
    }
}

int query( int poz ){

    int s = 0;

    while( poz > 0 ){
        s += arbore[ poz ];
        poz -= ( poz & -poz );
    }

    return s;

}