Cod sursa(job #2386079)

Utilizator andrei32576Andrei Florea andrei32576 Data 22 martie 2019 09:47:55
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <fstream>
using namespace std;

int n,m,i,op,x,y;
int lmax[270000],ls[270000],ld[270000];

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

void ocupa(int nod, int st, int dr, int a, int b)
{
    if (a<=st && dr<=b)
    {
        lmax[nod] = ls[nod] = ld[nod] = 0;
        return;
    }
    int fs = nod<<1, fd = (nod<<1) + 1;
    int mij = (st+dr)>>1;
    if (lmax[nod] == dr-st+1)
    {
        lmax[fs] = ls[fs] = ld[fs] = mij-st+1;
        lmax[fd] = ls[fd] = ld[fd] = dr-mij;
    }
    if (a <= mij)
        ocupa(fs, st, mij, a, b);
    if (b > mij)
        ocupa(fd, mij + 1, dr, a, b);
    lmax[nod] = max(max(lmax[fs], lmax[fd]), ld[fs] + ls[fd]);
    if (ls[fs] == mij-st+1)
        ls[nod] = ls[fs] + ls[fd];
    else
        ls[nod] = ls[fs];
    if(ld[fd] == dr-mij)
        ld[nod] = ld[fs] + ld[fd];
    else
        ld[nod] = ld[fd];
}

void elibereaza(int nod, int st, int dr, int a, int b)
{
    if (a<=st && dr<=b)
    {
        lmax[nod] = ls[nod] = ld[nod] = dr-st+1;
        return;
    }
    int fs = nod<<1, fd = (nod<<1) + 1;
    int mij = (st+dr)>>1;
    if (lmax[nod] == 0)
    {
        lmax[fs] = ls[fs] = ld[fs] = 0;
        lmax[fd] = ls[fd] = ld[fd] = 0;
    }

    if (a <= mij)
        elibereaza(fs, st, mij, a, b);
    if (mij < b)
        elibereaza(fd, mij + 1, dr, a, b);
    lmax[nod] = max(max(lmax[fs], lmax[fd]), ld[fs] + ls[fd]);
    if (ls[fs] == mij-st+1)
        ls[nod] = ls[fs] + ls[fd];
    else
        ls[nod] = ls[fs];
    if(ld[fd] == dr-mij)
        ld[nod] = ld[fs] + ld[fd];
    else
        ld[nod] = ld[fd];
}

int main()
{
    f>>n>>m;

    lmax[1] = ls[1] = ld[1] = n;

    for (i=1;i<=m;i++)
    {
        f>>op;
        if (op == 3)
        {
            g<<lmax[1]<<"\n";
        }
        else
        {
            f>>x>>y;
            if (op == 1)
                ocupa(1, 1, n, x, x+y-1);
            else
                elibereaza(1, 1, n, x, x+y-1);
        }
    }

    f.close();
    g.close();
    return 0;
}