Cod sursa(job #931259)

Utilizator rudarelLup Ionut rudarel Data 28 martie 2013 09:20:04
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <cstdio>
#include <cstring>
#include <cstdlib>
 
#define FIN "hotel.in"
#define FOUT "hotel.out"
 
#define NMAX 1 << 18
 
typedef struct tree
{
    int tip, st, dr, max;
} TREE;
 
TREE T[NMAX];
int N, P, st_v, dr_v, middle, t ;
 
inline int MAX( int a, int b )
{
    return a > b ? a : b;
}
inline int MAX4( int a, int b, int c, int d, int e )
{
    int max = a;
    if( b > max ) max = b;
    if( c > max ) max = c;
    if( d > max ) max = d;
    if( e > max ) max = e;
    return max;
}
void modify( TREE & T, int t, int value )
{
    T.tip = t;
    if( t )
        value = 0;
    T.st = T.dr = T.max = value;
}
 
void update( int nod, int st, int dr, int t )
{
    int half;
    if( st >= st_v && dr <= dr_v )
        modify( T[nod], t, dr - st + 1 );
    else
    {
        half = ( st + dr ) >> 1;
        if( T[nod].tip != 2 )
        {
            modify( T[2*nod], T[nod].tip, half - st + 1 );
            modify( T[2*nod+1], T[nod].tip, dr - half );
        }
        if( st_v <= half )
            update( 2 * nod, st, half, t );
        if( dr_v > half )
            update( 2 * nod + 1, half + 1, dr, t );
        middle = T[2*nod].dr + T[2*nod+1].st;
        T[nod].st = !T[2*nod].tip ? T[2*nod].st + T[2*nod+1].st : T[2*nod].st;
        T[nod].dr = !T[2*nod+1].tip ? T[2*nod].dr + T[2*nod+1].dr : T[2*nod+1].dr;
        T[nod].max = MAX4( T[nod].st, T[nod].dr, middle, T[2*nod].max, T[2*nod+1].max );
        if( T[2*nod].tip == T[2*nod+1].tip )
            T[nod].tip = T[2*nod].tip;
        else
            T[nod].tip = 2;
    }
}
 
void build( int nod, int st, int dr )
{
    int half = ( st + dr ) >> 1;
    if( st == dr )
        modify( T[nod], 0, 1 );
    else
    {
        modify( T[nod], 0, dr - st + 1 );
        build( 2 * nod, st, half );
        build( 2 * nod + 1, half + 1, dr );
    }
         
}
 
 
int main()
{
    FILE * fin = fopen( FIN, "r" );
    FILE * fout = fopen( FOUT, "w" );
    fscanf( fin, "%d%d", &N, &P );
    build( 1, 1, N );
    while( P-- )
    {
     
        fscanf( fin, "%d", &t );
        if( t == 3 )
        fprintf( fout, "%d\n", T[1].max );
        else
        {
            fscanf( fin, "%d%d", &st_v, &dr_v );
            dr_v = st_v + dr_v - 1;
            update( 1, 1, N, 2 - t );
        }
    }
     
    fclose( fin );
    fclose(fout );
     
    return 0;
}