Cod sursa(job #1292132)

Utilizator angelaAngela Visuian angela Data 13 decembrie 2014 17:52:53
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.37 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
 
class ST
{
public:
    ST( int _N )
    {
        N = _N;
        best = pre = su = vector<int>( 4 * N );
        build( 1, 1, N );
    }
 
    void update( int x, int y, int v ) 
    {
        update( 1, 1, N, x, y, v );
    }
 
    int query() const
    {
        return best[1];
    }
 
 protected:
    void build( int nod, int st, int dr )
    {
        best[nod] = su[nod] = pre[nod] = dr - st + 1;
 
        if ( st != dr )
        {
            int m = ( st + dr ) / 2;
            build( 2 * nod, st, m );
            build( 2 * nod + 1, m + 1, dr );
        }
    }
 
    void set( int nod, int val )
    {
        best[nod] = pre[nod] = su[nod] = val;
    }
 
    void push_down( int nod, int st, int dr )
    {
        if ( best[nod] == 0 )
        {
            set( 2 * nod, 0 );
            set( 2 * nod + 1, 0 );
        }
 
        if ( best[nod] == dr - st + 1 )
        {
            int m = ( st + dr ) / 2;
            set( 2 * nod, m - st + 1 );
            set( 2 * nod + 1, dr - m );
        }
    }
 
    void update( int nod, int st, int dr, int L, int R, int val )
    {
        if ( L <= st && dr <= R )
        {                                           
            set( nod, ( dr - st + 1 ) * val ); 
            return;                         
        }
        
    
		push_down( nod, st, dr );

		int m = ( st + dr ) / 2;

		if ( L <= m ) update( 2 * nod, st, m, L, R, val );
		if ( m < R )  update( 2 * nod + 1, m + 1, dr, L, R, val );

		pre[nod] = pre[2 * nod];

		if ( pre[2 * nod] == m - st + 1 )
			pre[nod] += pre[2 * nod + 1];

		su[nod] = su[2 * nod + 1];

		if ( su[2 * nod + 1] == dr - m )
			su[nod] += su[2 * nod];

		best[nod] = max( best[2 * nod], best[2 * nod + 1] );
		best[nod] = max( best[nod], su[2 * nod] + pre[2 * nod + 1] );
    }
private:
    vector <int> best, pre, su;
    int N;

};
 
int N, P;
 
int main()
{
    ifstream in("hotel.in");
    ofstream out("hotel.out");
 
    in >> N >> P;
 
    ST S( N );
	int c, i, M;
    while ( P-- )
    {
        in >> c;
        if ( c == 1 )
        {
            in >> i >> M;
            S.update( i, i + M - 1, 0 );
        }
 
        if ( c == 2 )
        {
            in >> i >> M;
 
            S.update( i, i + M - 1, 1 );
        }
 
        if ( c == 3 )
        {
            out << S.query() << "\n";
        }
    }
    return 0;
}