Cod sursa(job #2002433)

Utilizator MihaelaCismaruMihaela Cismaru MihaelaCismaru Data 19 iulie 2017 21:27:49
Problema Hotel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.11 kb
#include<fstream>
using namespace std;
ifstream in("hotel.in");
ofstream out("hotel.out");
const int INF = 200001;
struct str{
    long long sum, ssm,dsm,sm,lazy;
}aint[400001];
str update_nod( str s1, str s2 ){
    str srez;
    srez.sum = s1.sum + s2.sum;
    srez.ssm = max( s1.ssm, s1.sum+s2.ssm );
    srez.dsm = max( s2.dsm, s2.sum+s1.dsm );
    srez.sm = max( s1.dsm + s2.ssm,max( s1.sm, s2.sm) );
    srez.lazy = 0;
    return srez;
}
void update_lazy( int nod, int st, int dr ){
    if( aint[nod].lazy == 1 ){
        if( st != dr ){
            aint[nod<<1].lazy = 1;
            aint[nod<<1|1].lazy = 1;
        }
        aint[nod] = { -INF,-INF,-INF,-INF,0 };
    }
    if( aint[nod].lazy == 2 ){
        if( st != dr ){
            aint[nod<<1].lazy = 2;
            aint[nod<<1|1].lazy = 2;
        }
        aint[nod] = { dr-st+1, dr-st+1,dr-st+1,dr-st+1,0 };
    }
    return;
}
void build( int nod, int st, int dr ){
    if( st == dr ){
        aint[nod] = { 1,1,1,1,0};
        return;
    }
    int mid = ( st + dr )>> 1;
    build( nod << 1, st , mid );
    build( nod << 1 | 1, mid + 1, dr );
    aint[nod] = update_nod( aint[nod<<1], aint[nod<<1|1] );
    return;
}
void update( int nod, int st, int dr, int x, int y, int laz ){
    update_lazy( nod, st, dr );
    if( dr < x || st > y ){
        return;
    }
    if( st >= x && dr <= y ){
        aint[nod].lazy = laz;
        update_lazy( nod, st, dr );
        return;
    }
    int mid = ( st + dr )>> 1;
    update( nod << 1,st,mid,x,y,laz );
    update(nod << 1 | 1, mid + 1,dr,x,y,laz );
    aint[nod] = update_nod( aint[nod << 1], aint[nod <<1|1] );
    return;
}
int n,p,i,t,x,y;
int main( void ){
    in >> n >> p;
    build( 1,1,n );
    for( i = 1; i <= p; i ++ ){
        in >> t;
        if( t == 1 ){
            in >> x >> y;
            y+=x-1;
            update( 1,1,n,x,y,1);
        }
        if( t == 2 ){
            in >> x >> y;
            y+=x-1;
            update( 1,1,n,x,y,2);
        }
        if( t == 3 ){
            out << aint[1].sm<<"\n";
        }
    }
    return 0;
}