Cod sursa(job #1278814)

Utilizator felixiPuscasu Felix felixi Data 29 noiembrie 2014 14:34:53
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <fstream>

using namespace std;

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

const int NMAX = 100000;

int N,M;
int arb[4*NMAX+1], Ans_Q;

void Build( int nod, int st, int dr, int poz, int val ) {
    if( st == dr ) {   ///  Am ajuns intr-o frunza
        arb[nod]= val;
    }
    else {  ///  Vad unde ma duc
        int mid= (st+dr)/2;
        if( poz <= mid ) {
            Build( 2*nod, st, mid, poz, val );
        }
        else {
            Build( 2*nod+1, mid+1, dr, poz, val );
        }
        arb[ nod ] = arb[2*nod] + arb[2*nod+1];
    }
}

void Update( int nod, int st, int dr, int poz, int val ) {
    if( st == dr ) {   ///  Am ajuns intr-o frunza
        arb[nod]-= val;
    }
    else {  ///  Vad unde ma duc
        int mid= (st+dr)/2;
        if( poz <= mid ) {
            Update( 2*nod, st, mid, poz, val );
        }
        else {
            Update( 2*nod+1, mid+1, dr, poz, val );
        }
        arb[ nod ] = arb[2*nod] + arb[2*nod+1];
    }
}

void Querry( int nod, int st, int dr, int x, int y ) {
    int mid= (st+dr)/2;
    if( st >= x && y >= dr ) {
        Ans_Q+= arb[nod];
        return;
    }

    mid= (st+dr)/2;
    if( x<=mid ) {
        Querry( 2*nod, st, mid, x, y );
    }
    if( y>mid ) {
        Querry( 2*nod+1, mid+1, dr, x, y );
    }
}

int main() {
    in >> N >> M;
    for( int i= 1;  i<=N;  ++i ) {
        int a;
        in >> a;
        Build( 1, 1,N, i, a );
    }
    for( int i= 1;  i<=M;  ++i ) {
        int t,x,y;
        in >> t >> x >> y;
        if( t == 1 ) {
            Ans_Q= 0;
            Querry( 1, 1,N, x,y );
            out << Ans_Q << '\n';
        }
        else {
            Update( 1, 1,N, x, y );
        }
    }
    return 0;
}