Cod sursa(job #1413738)

Utilizator bogdanpaunFMI Paun Bogdan Gabriel bogdanpaun Data 2 aprilie 2015 02:32:09
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <fstream>

using namespace std;

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

int n , q, tip  , _1 , _2;
#define amax 400005
int a[amax],l[amax],r[amax];

void build(int x,int s,int d){
    if( s == d ){
        a[x] = l[x] = r[x] = 1;
        return ;
    }

    int m = (s+d)>>1;
    build( x*2  , s  , m  );
    build( x*2+1, m+1, d  );

    a[x]=l[x]=r[x] = a[x*2] + a[x*2+1];
}
void update(int x,int s,int d){
    if( _1 <= s && d <= _2){
        if( tip == 2 )
            a[x] = l[x] = r[x] = d-s+1;
        else
            a[x] = l[x] = r[x] = 0;
        return ;
    }

    int m = (s+d)>>1;

    if( a[x] == 0 ){
        a[x*2]   = l[x*2]   = r[x*2]   = 0;
        a[x*2+1] = l[x*2+1] = r[x*2+1] = 0;
    }
    if( a[x] == d-s+1 ){
        a[x*2]   = l[x*2]   = r[x*2]   = m-s+1;
        a[x*2+1] = l[x*2+1] = r[x*2+1] = d-m;
    }

    if( _1 <= m  )      update( x*2  , s   ,  m );
    if(  m < _2 )       update( x*2+1, m+1 ,  d );

    if( l[x*2] == m-s+1 )
        l[x] = l[x*2] + l[x*2+1];
    else
        l[x] = l[x*2];

    if( r[x*2+1] == d-m )
        r[x] = r[x*2+1] + r[x*2];
    else
        r[x] = r[x*2+1];


    a[x] = max( max(a[x*2] , a[x*2+1]) , r[x*2] + l[x*2+1] );

}

int main()
{
    f >> n >> q;

    build(1,1,n);

    for(    int i=1;    i<=q;   ++i){
        f >> tip;
        if( tip == 3 ){
            g << a[1] << '\n';
        }else{
            f >> _1 >> _2;
            _2 = _1 + _2 - 1;
            update(1,1,n);
        }
    }


    return 0;
}