Cod sursa(job #2758586)

Utilizator pielevladutPiele Vladut Stefan pielevladut Data 11 iunie 2021 12:35:09
Problema Hotel Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.79 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, p;
struct elem
{
    int ans;
    int pref;
    int suf;
};
elem aint[400400];

void liber(int nod, int st, int dr)
{
    aint[nod].ans = aint[nod].pref = aint[nod].suf = dr - st + 1;
}

void ocupat(int nod, int st, int dr)
{
    aint[nod].ans = aint[nod].pref = aint[nod].suf = 0;
}

void build(int nod, int st, int dr)
{
    liber(nod, st, dr);
    if(st == dr)
    {
        return ;
    }
    int mijloc = (st + dr) / 2;
    build(2 * nod, st, mijloc);
    build(2 * nod + 1, mijloc + 1, dr);
}

void update(int a, int b, int nod, int st, int dr, bool verifica)
{
    if(st == dr)
    {
        if(verifica == 0)
        {
            ocupat(nod, st, dr);
        }
        else
        {
            liber(nod, st, dr);
        }
        return ;
    }
    int mijloc = (st + dr) / 2;
    /*
    if(aint[nod].ans == dr - st + 1)
    {
        liber(2 * nod, st, mijloc);
        liber(2 * nod + 1, mijloc + 1, dr);
    }
    */
    if(aint[nod].ans == 0)
    {
        ocupat(2 * nod, st, mijloc);
        ocupat(2 * nod + 1, mijloc + 1, dr);

    }
    if(a <= st && dr <= b)
    {
        if(verifica == 0)
        {
            ocupat(nod, st, dr);
        }
        else
        {
            liber(nod, st, dr);
        }
        return ;
    }
    if(a <= mijloc)
    {
        update(a, b, 2 * nod, st, mijloc, verifica);
    }
    if(b > mijloc)
    {
        update(a, b, 2 * nod + 1, mijloc + 1, dr, verifica);
    }
    if(aint[2 * nod].ans == mijloc - st + 1)
    {
        aint[nod].pref = mijloc - st + 1 + aint[2 * nod + 1].pref;
    }
    else
    {
        aint[nod].pref = aint[2 * nod].pref;
    }
    if(aint[2 * nod + 1].ans == dr - mijloc)
    {
        aint[nod].suf = dr - mijloc + aint[2 * nod].suf;
    }
    else
    {
        aint[nod].suf = aint[2 * nod + 1].suf;
    }
    aint[nod].ans = max(aint[2 * nod].ans, max(aint[2 * nod + 1].ans, aint[2 * nod].suf + aint[2 * nod + 1].pref));
}

int main()
{
    fin >> n >> p;
    build(1, 1, n);
    while(p--)
    {
        int tip;
        fin >> tip;
        if(tip == 3)
        {
            fout << aint[1].ans << '\n';
        }
        else
        {
            int poz, val;
            fin >> poz >> val;
            bool verifica;
            if(tip == 1)
            {
                update(poz, poz + val - 1, 1, 1, n, 0);
                /// verifica == 0 atunci cand trebuie sa vina un grup de turisti
            }
            else
            {
                update(poz, poz + val - 1, 1, 1, n, 1);
                /// verifica == 1 atunci cand trebuie sa plece un grup de turisti
            }
        }
    }
}