Cod sursa(job #2681805)

Utilizator WilIiamperWilliam Damian Balint WilIiamper Data 6 decembrie 2020 19:26:31
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>

using namespace std;

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

int n, arbint[4 * 15001], datorii[15001];

int query ( int lo, int hi, int node, int lowerBound, int upperBound ) {

    if ( lo >= lowerBound && hi <= upperBound )
        return arbint[node];

    if ( lo > upperBound || hi < lowerBound )
        return 0;

    int mid = lo + (hi-lo)/2;

    return query( lo, mid, node*2, lowerBound, upperBound )
            + query( mid+1, hi, node*2+1, lowerBound, upperBound );
}

void update ( int lo, int hi, int node, int pos) {

    if ( lo == hi ) {
        arbint[node] = datorii[pos];
        return;
    }

    int mid = lo + (hi-lo)/2;
    if ( pos > mid )
        update ( mid+1, hi, node*2+1, pos);
    else
        update ( lo, mid, node*2, pos);

    arbint[node] = arbint[ node*2 ] + arbint[ node*2+1 ];
}

void solve () {

    int m, op, x, y;
    fin >> n >> m;

    for ( int i = 1; i <= n; i++ ) {
        fin >> datorii[i];
        update (1, n, 1, i);
    }

    while ( m-- ) {
        fin >> op >> x >> y;

        if ( op == 0 ) {
            datorii[x] -= y;
            update( 1, n, 1, x);
        }
        else
            fout << query(1, n, 1, x, y) << "\n";
    }
}

int main()
{
    solve ();
    return 0;
}