Cod sursa(job #3230436)

Utilizator alexxiacrisanCrisan Maria - Alexia alexxiacrisan Data 21 mai 2024 17:21:55
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.45 kb
#include <iostream>
#include <fstream>

using namespace std;

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

struct camere
{
    int maxim;
    int start;  //nr de camere libere consecutive de la inceputul segmentului
    int finish; //nr de camere libere consecutive pana in finalul segmentului
    bool ocupat;
}hotel[400005];

void build(int nod, int left, int right)
{
    if(left == right)
        hotel[nod] = {1, 1, 1, 0};
    else
    {
        int mij = (left + right) / 2;
        build(2 * nod, left, mij);
        build(2 * nod + 1, mij + 1, right);
        hotel[nod] = {right - left + 1, right - left + 1, right - left + 1, 0};
    }
}

void update(int nod, int left, int right, int a, int b, int op)
{
    if(a <= left && right <= b)
    {
        if(op == 1) hotel[nod] = {0, 0, 0, 1};
        else hotel[nod] = {right - left + 1, right - left + 1, right - left + 1, 1};
    }
    else
    {
        int mij = (left + right) / 2;

        if(hotel[nod].ocupat)
        {
            if(hotel[nod].maxim)
            {
                hotel[2 * nod] = {mij - left + 1, mij - left + 1, mij - left + 1, 1};
                hotel[2 * nod + 1] = {right - mij, right - mij, right - mij, 1};
            }
            else
            {
                hotel[2 * nod] = {0, 0, 0, 1};
                hotel[2 * nod + 1] = {0, 0, 0, 1};
            }
            hotel[nod].ocupat = 0;
        }

        if(a <= mij)
            update(2 * nod, left, mij, a, b, op);
        if(b > mij)
            update(2 * nod + 1, mij + 1, right, a , b, op);
        hotel[nod].maxim = max(max(hotel[2 * nod].maxim, hotel[2 * nod + 1].maxim), hotel[2 * nod].finish + hotel[2 * nod + 1].start);

        if(hotel[2 * nod].start == mij - left + 1)
            hotel[nod].start = hotel[2 * nod].start + hotel[2 * nod + 1].start;
        else
            hotel[nod].start = hotel[2 * nod].start;

        if(hotel[2 * nod + 1].finish == right - mij)
            hotel[nod].finish = hotel[2 * nod].finish + hotel[2 * nod + 1].finish;
        else
            hotel[nod].finish = hotel[2 * nod + 1].finish;

    }
}

int main()
{
    int n, p, i, op, a, b;
    fin>>n>>p;
    build(1, 1, n);
    for(i=0; i<p; i++)
    {
        fin>>op;
        if(op == 3)
            fout<<hotel[1].maxim<<endl;
        else
        {
            fin>>a>>b;
            update(1, 1, n, a, a + b - 1, op);
        }
    }
    return 0;
}