Cod sursa(job #1771445)

Utilizator priboiraduPriboi Radu Bogdan priboiradu Data 5 octombrie 2016 17:22:07
Problema Datorii Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

FILE *fin, *fout;

int v[15000], aint[1<<15];
int suma;

int read() {
    int nr;
    char ch;
    ch = fgetc( fin );
    while ( isdigit( ch ) == 0 )
        ch = fgetc( fin );
    nr = 0;
    while ( isdigit( ch ) ) {
        nr = nr * 10 + ( ch - '0' );
        ch = fgetc( fin );
    }
    return nr;
}

void arboreInterval( int st, int dr, int p ) {
    if ( st == dr )
        aint[p] = v[st];
    else {
        arboreInterval( st, ( st + dr ) / 2, 2 * p );
        arboreInterval( ( st + dr ) / 2 + 1, dr, 2 * p + 1 );
        aint[p] = aint[2*p] + aint[2*p+1];
    }
}

void update( int st, int dr, int p, int poz ) {
    if ( st == dr )
        aint[p] = v[st];
    else {
        if ( poz <= ( st + dr ) / 2 )
            update( st, ( st + dr ) / 2, 2 * p, poz );
        if ( ( st + dr ) / 2 + 1 <= poz )
            update( ( st + dr ) / 2 + 1, dr, 2 * p + 1, poz );
        aint[p] = aint[2*p] + aint[2*p+1];
    }
}

void query( int st, int dr, int p, int x, int y ) {
    if ( x <= st && dr <= y )
        suma += aint[p];
    else {
        if ( x <= ( st + dr ) / 2 )
            query( st, ( st + dr ) / 2, 2 * p, x, y );
        if ( ( st + dr ) / 2 + 1 <= y )
            query( ( st + dr ) / 2 + 1, dr, 2 * p + 1, x, y );
    }
}

int main() {
    int n, m, i, q, x, y;
    fin = fopen( "datorii.in", "r" );
    fout = fopen( "datorii.out", "w" );
    n = read();
    m = read();
    for ( i = 0; i < n; i++ )
        v[i] = read();
    arboreInterval( 0, n - 1, 1 );
    for ( i = 0; i < m; i++ ) {
        q = read();
        x = read();
        y = read();
        if ( q == 0 ) {
            x--;
            v[x] -= y;
            update( 0, n - 1, 1, x );
        }
        else {
            x--;
            y--;
            suma = 0;
            query( 0, n - 1, 1, x, y );
            fprintf( fout, "%d\n", suma );
        }
    }
    fclose( fin );
    fclose( fout );
    return 0;
}