Cod sursa(job #2758575)

Utilizator mihnea.cazan15mihnea cazan mihnea.cazan15 Data 11 iunie 2021 11:56:03
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <fstream>

using namespace std;
ifstream cin("hotel.in");
ofstream cout("hotel.out");
const int N = 300001;
int l_max[N], l_st[N], l_dr[N], a, b;
void liber(int p, int st, int dr)
{
    l_max[p] = l_st[p] = l_dr[p] = dr - st + 1;
}
void ocupat(int p, int st, int dr)
{
    l_max[p] = l_st[p] = l_dr[p] = 0;
}
void constructie(int p, int st, int dr)
{
    liber(p, st, dr);
    if (st == dr)
    {
        return;
    }
    int m = (st + dr) / 2;
    constructie(2*p, st, m);
    constructie(2*p + 1, m + 1, dr);
}
void actualizare(int p, int st, int dr, bool ocupa)
{
    if (st == dr)
    {
        if (ocupa)
            ocupat(p, st, dr);
        else
            liber(p, st, dr);
        return;
    }
    int m = (st + dr) / 2, fs = 2*p, fd = 2*p + 1;
    if (l_max[p] == dr - st + 1)
    {
        liber(fs, st, m);
        liber(fd, m + 1, dr);
    }
    if (l_max[p] == 0)
    {
        ocupat(fs, st, m);
        ocupat(fd, m + 1, dr);
    }
    if (a <= st && dr <= b)
    {
        if (ocupa)
            ocupat(p, st, dr);
        else
            liber(p,st,dr);
        return;
    }
    if(a <= m)
        actualizare(fs, st, m, ocupa);
    if(b>m)
        actualizare(fd,m+1,dr,ocupa);
    if(l_max[fs]==m-st+1)
        l_st[p]=m-st+1+l_st[fd];
    else
        l_st[p]=l_st[fs];
    if(l_max[fd]==dr - m)
        l_dr[p]=dr-m+l_dr[fs];
    else
        l_dr[p]=l_dr[fd];
    l_max[p]=max(max(l_max[fs],l_max[fd]),l_dr[fs]+l_st[fd]);
}
int main()
{
    int n,p;
    cin>>n>> p;
    constructie(1, 1, n);
    for(int i=1; i<=p; i++)
    {
        int tip;
        cin>>tip;
        if (tip == 1 ||tip == 2)
        {
            cin >> a >> b;
            b=a+b-1;
            actualizare(1, 1, n, (tip == 1));
        }
        else
            cout<<l_max[1]<<'\n';
    }
    return 0;
}