Cod sursa(job #3344681)

Utilizator MMEnisEnis Mutlu MMEnis Data 4 martie 2026 17:34:56
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.45 kb
#include <bits/stdc++.h>

using namespace std;

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

char lazy[400001]; /// 0 - nimic, 1 - umple, 2 - elibereaza
int pref[400001];
int suf[400001];
int best[400001];

void combine ( int nod, int st, int dr )
{
    int mij = ( st + dr ) / 2;
    int len_st = mij - st + 1;
    int len_dr = dr - mij;
    if ( pref[nod * 2] == len_st )
        pref[nod] = len_st + pref[nod * 2 + 1];
    else pref[nod] = pref[nod * 2];
    if ( suf[nod * 2 + 1] == len_dr )
        suf[nod] = suf[nod * 2] + len_dr;
    else suf[nod] = suf[nod * 2 + 1];
    best[nod] = max ( best[nod * 2], best[nod * 2 + 1] );
    best[nod] = max ( best[nod], suf[nod * 2] + pref[nod * 2 + 1] );
}
void build ( int nod, int st, int dr )
{
    if ( st == dr )
    {
        pref[nod] = 1;
        suf[nod] = 1;
        best[nod] = 1;
        return;
    }
    int mij = ( st + dr ) / 2;
    build ( nod * 2, st, mij );
    build ( nod * 2 + 1, mij + 1, dr );
    combine ( nod, st, dr );
}
void pushdown ( int nod, int st, int dr )
{
    if ( lazy[nod] == 0 )
        return;
    int mij = ( st + dr ) / 2;
    lazy[nod * 2] = lazy[nod];
    if ( lazy[nod * 2] == 1 )
        pref[nod * 2] = suf[nod * 2] = best[nod * 2] = 0;
    else pref[nod * 2] = suf[nod * 2] = best[nod * 2] = mij + 1 - st;
    lazy[nod * 2 + 1] = lazy[nod];
    if ( lazy[nod * 2 + 1] == 1 )
        pref[nod * 2 + 1] = suf[nod * 2 + 1] = best[nod * 2 + 1] = 0;
    else pref[nod * 2 + 1] = suf[nod * 2 + 1] = best[nod * 2 + 1] = dr - mij;
    lazy[nod] = 0;
}
void update ( int nod, int st, int dr, int a, int b, int type )
{
    if ( b < st || dr < a )
        return;
    if ( a <= st && dr <= b )
    {
        lazy[nod] = type;
        if ( type == 1 )
            pref[nod] = suf[nod] = best[nod] = 0;
        else pref[nod] = suf[nod] = best[nod] = dr - st + 1;
        return;
    }
    pushdown( nod, st, dr );
    int mij = ( st + dr ) / 2;
    update ( nod * 2, st, mij, a, b, type );
    update ( nod * 2 + 1, mij + 1, dr, a, b, type );
    combine ( nod, st, dr );
}

int main()
{
    int n, p;
    f >> n >> p;
    build ( 1, 1, n );
    while ( p -- )
    {
        int tip;
        f >> tip;
        if ( tip == 3 )
            g << best[1] << '\n';
        else
        {
            int poz, m;
            f >> poz >> m;
            update ( 1, 1, n, poz, poz + m - 1, tip );
        }
    }
    return 0;
}