Cod sursa(job #2488455)

Utilizator Tudor06MusatTudor Tudor06 Data 6 noiembrie 2019 22:19:03
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.89 kb
#include <iostream>
#include <fstream>

using namespace std;

const int NMAX = 1e5;

struct nod {
    int suf;
    int pref;
    int l;
    int lazy;
};

nod aint[4 * NMAX];

int val;

void propag( int st, int dr, int i ) {
    if ( aint[i].lazy != -1 ) {
        int mij = ( st + dr ) / 2;
        if ( i * 2 + 1 < 4 * NMAX ) {
            aint[i * 2 + 1].suf = aint[i * 2 + 1].pref = aint[i * 2 + 1].l = aint[i].lazy * ( mij - st + 1 );
            aint[i * 2 + 1].lazy = aint[i].lazy;
        }
        if ( i * 2 + 2 < 4 * NMAX ) {
            aint[i * 2 + 2].suf = aint[i * 2 + 2].pref = aint[i * 2 + 2].l = aint[i].lazy * ( dr - mij );
            aint[i * 2 + 2].lazy = aint[i].lazy;
        }
        aint[i].lazy = -1;
    }
}

void update( int a, int b, int st, int dr, int i ) {
    propag( st, dr, i );
    if ( a == st && b == dr ) { /// Intervalul acesta se stie sigur ca se va goli/umple
        aint[i].l = aint[i].suf = aint[i].pref = val * ( dr - st + 1 );
        aint[i].lazy = val;
    } else {
        int mij = ( st + dr ) / 2;
        if ( b <= mij )
            update( a, b, st, mij, i * 2 + 1 );
        else if ( a > mij )
            update( a, b, mij + 1, dr, i * 2 + 2 );
        else {
            update( a, mij, st, mij, i * 2 + 1 );
            update( mij + 1, b, mij + 1, dr, i * 2 + 2 );
        }
        aint[i].suf = aint[i * 2 + 2].suf; /// Stim garantat ca sufixul este minim sufixul intervalului mai mic
        if ( aint[i * 2 + 2].suf == dr - mij ) /// Insa daca intervalul mai mic este plin de unu atunci la el se mai adauga si sufixul celuilalt interval mai mic
            aint[i].suf += aint[i * 2 + 1].suf;
        aint[i].pref = aint[i * 2 + 1].pref; /// Analog si pentru prefixe;
        if ( aint[i * 2 + 1].pref == mij - st + 1 )
            aint[i].pref += aint[i * 2 + 2].pref;
        aint[i].l = max( max( aint[i * 2 + 1].l, aint[i * 2 + 2].l ),
                        aint[i * 2 + 1].suf + aint[i * 2 + 2].pref );
        /// lugimea maxim de 0 este maximul dintre
        /// Lungimea intervalului mai mic din stanga  ( left_son  )
        /// Lungimea intervalului mai mic din dreapta ( right_son )
        /// Lungimea celui mai lung sufix din  ( left_son  ) +
        /// Lungimea celui mai lung prefix din ( right_son )
    }
}

int main() {
    ifstream fin( "hotel.in" );
    ofstream fout( "hotel.out" );
    int n, p, i, a, b;
    fin >> n >> p;
    val = 1;
    for ( i = 0; i < 4 * NMAX; i ++ ) {
        aint[i].lazy = -1;
    }
    update( 0, n - 1, 0, n - 1, 0 );
    for ( i = 0; i < p; i ++ ) {
        fin >> val;
        if ( val == 3 ) {
            fout << aint[0].l << endl;
        } else {
            val --;
            fin >> a >> b;
            a --;
            b --;
            update( a, a + b, 0, n - 1, 0 );
        }
    }
    fin.close();
    fout.close();
    return 0;
}