Cod sursa(job #1425495)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 27 aprilie 2015 16:11:20
Problema Datorii Scor 100
Compilator cpp Status done
Runda pregatire-lot-aib Marime 1.24 kb
#include<fstream>

using namespace std;

ifstream fin( "datorii.in" );
ofstream fout( "datorii.out" );

const int nmax = 15000;
int ans, ai[ 4 * nmax + 1 ];

void update( int st, int dr, int poz, int x, int val ) {
    if ( st == dr ) {
        ai[ poz ] += val;
        return ;
    }
    int mid = ( st + dr ) / 2;
    if ( x <= mid ) {
        update( st, mid, 2 * poz, x, val );
    } else {
        update( mid + 1, dr, 2 * poz + 1, x, val );
    }
    ai[ poz ] = ai[ 2 * poz ] + ai[ 2 * poz + 1 ];
}
void query( int st, int dr, int poz, int x, int y ) {
    if ( x <= st && dr <= y ) {
        ans += ai[ poz ];
        return ;
    }
    int mid = ( st + dr ) / 2;
    if ( x <= mid ) {
        query( st, mid, 2 * poz, x, y );
    }
    if ( y >= mid + 1 ) {
        query( mid + 1, dr, 2 * poz + 1, x, y );
    }
}
int main() {
    int n, m, x, y, t;
    fin >> n >> m;
    for( int i = 1; i <= n; ++ i ) {
        fin >> x;
        update( 1, n, 1, i, x );
    }
    for( int i = 0; i < m; ++ i ) {
        fin >> t >> x >> y;
        if ( t == 0 ) {
            update( 1, n, 1, x, -y );
        } else {
            ans = 0;
            query( 1, n, 1, x, y );
            fout << ans << "\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}