Cod sursa(job #2128612)

Utilizator Alexandru_StoianStoian Sorin Alexandru Alexandru_Stoian Data 11 februarie 2018 21:05:40
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <climits>

#define dim 100001

using namespace std;

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

inline void Update( int nod, int st, int dr );

inline void Querry( int nod, int st, int dr );

int n, m, Arbmax[ 4 * dim + 10 ], nod, st, dr, val, poz, ok, a, b, sf, in, maxim, j, k;

int main(){
    f >> n >> m;
    for( int i = 1; i <= n; ++i ){
        f >> val;
        poz = i;
        Update( 1, 1, n );
    }
    for( int i = 1; i <= m; ++i ){
        f >> ok >> a >> b;
        if( ok == 1 ){
            maxim = 0;
            in = a, sf = b;
            Querry( 1, 1, n );
            g << maxim << '\n';
        }
        else{
            val = -b;
            poz = a;
            Update( 1, 1, n );
        }
        /**
        j = 1;
        k = 1;
        while( j < 9 ){
            for( ; j < k; ++j )
                g << Arbmax[ j ] << ' ';
            g << '\n';
            k = k * 2;
        }
        */
    }
    return 0;
}

inline void Update( int nod, int st, int dr ){
    if( st == dr ){
        Arbmax[ nod ] = Arbmax[ nod ] + val;
        return;
    }
    int div = ( st + dr ) / 2;
    if( poz <= div )Update( nod * 2, st, div );
    else Update( nod * 2 + 1, div + 1, dr );
    Arbmax[ nod ] =  Arbmax[ 2 * nod ] + Arbmax[ 2 * nod + 1 ];
}

inline void Querry( int nod, int st, int dr ){
    if( in <= st && dr <= sf ){
        maxim = Arbmax[ nod ] + maxim;
        return;
    }
    int div = ( st + dr ) / 2;
    if( in <= div )Querry( nod * 2, st, div );
    if( div < sf )Querry( nod * 2 + 1, div + 1, dr );
}