Cod sursa(job #999056)

Utilizator crucerucalinCalin-Cristian Cruceru crucerucalin Data 18 septembrie 2013 23:41:18
Problema Datorii Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int BIT[15005];
int N, M, P, Q;


void update( int indice, int value )
{
    int pozbit = 0;
    while ( indice <= N ) {
        BIT[indice] += value;
        while ( ! ( indice & ( 1<<pozbit ) ) ) {
            ++pozbit;
        }
        indice += ( 1<<pozbit );
        ++pozbit;
    }
}

int sum( int indice )
{
    int sum = 0, pozbit = 0;

    while ( indice > 0 ) {
        sum += BIT[indice];
        while ( !( indice & ( 1<<pozbit ) ) ) {
            ++pozbit;
        }
        indice -= ( 1<<pozbit );
        ++pozbit;
    }

    return sum;
}

int main()
{
    int curent;
    int i, bool;

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

    scanf("%d",&N);
    scanf("%d",&M);

    for ( i=1; i<=N; i++ ) {
        scanf("%d",&curent);
        update(i, curent);
    }


    for ( ; M; M-- ) {

        scanf("%d",&bool);
        scanf("%d",&P);
        scanf("%d",&Q);

        if ( !bool ) {
            update(P,-Q);
        }
        else {
            printf("%d\n",sum(Q)-sum(P-1));
        }
    }

    return 0;
}