Cod sursa(job #2758572)

Utilizator seburebu111Mustata Dumtru Sebastian seburebu111 Data 11 iunie 2021 11:46:51
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N = 1<<18;

int l_max[N], l_st[N], l_dr[N], a, b;

void constructie(int p, int st, int dr)
{
    l_max[p] = l_st[p] = l_dr[p] = dr-st+1;
    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)
            l_max[p] = l_st[p] = l_dr[p] = 0;
        else
            l_max[p] = l_st[p] = l_dr[p] = dr-st+1;
        return;
    }

    int m=(st+dr)/2;
    int fs=2*p;
    int fd=2*p+1;

    if(l_max[p] == dr-st+1)//intervalul curent este complet liber
    {
        l_max[fs] = l_st[fs] = l_dr[fs] = m-st+1;
        l_max[fd] = l_st[fd] = l_dr[fd] = dr-m;//dr-(m+1)+1
    }
    if(l_max[p] == 0)//intervalul curent este complet ocupa
    {
        l_max[fs] = l_st[fs] = l_dr[fs] = 0;
        l_max[fd] = l_st[fd] = l_dr[fd] = 0;
    }

    if(a<=st && b>=dr)
    {
        if(ocupa)
            l_max[p] = l_st[p] = l_dr[p] = 0;
        else
            l_max[p] = l_st[p] = l_dr[p] = dr-st+1;
        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)//caz particular: fiul stang este complet liber
        l_st[p] = m-st+1+l_st[fd];
    else
        l_st[p] = l_st[fs];

    if(l_max[fd]==dr-m)//caz particular: fiul drept este complet liber
        l_dr[p]=dr-m+l_dr[fs];
    else
        l_dr[p]=l_dr[fd];

    l_max[p]=max(l_max[fs], max(l_max[fd], l_dr[fs]+l_st[fd]));

}

int main()
{
    int n, p;
    in>>n>>p;
    constructie(1, 1, n);
    for(int i=0; i<p; i++)
    {
        int tip;
        in>>tip;
        if(tip==1 || tip==2)
        {
            in>>a>>b;
            b=a+b-1;
            actualizare(1, 1, n, (tip==1));
        }
        else
        {
            out<<l_max[1]<<'\n';
        }
    }
}