Cod sursa(job #2802798)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 18 noiembrie 2021 20:19:42
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int maxN = 300005;
int n, p, vst[maxN], vdr[maxN], maxi[maxN], a, b;

int max(int x1, int x2, int x3)
{
    return max(x1, max(x2, x3));
}

void precalc(int nod, int st, int dr)
{
    vst[nod] = dr - st + 1;
    vdr[nod] = dr - st + 1;
    maxi[nod] = dr - st + 1;
    if (st == dr)
        return;
    int med = (st + dr) / 2;
    precalc(2 * nod, st, med);
    precalc(2 * nod + 1, med + 1, dr);
}

void update(int nod, int st, int dr, bool c)
{
    if(st == dr)
    {
        if(c)
        {
            vst[nod] = 0;
            vdr[nod] = 0;
            maxi[nod] = 0;
        }
        else
        {
            vst[nod] = 1;
            vdr[nod] = 1;
            maxi[nod] = 1;
        }
        return;
    }
    int med = (st + dr) / 2;
    if(maxi[nod] == dr - st + 1)
    {
        vst[2 * nod] = med - st + 1;
        vdr[2 * nod] = med - st + 1;
        maxi[2 * nod] = med - st + 1;
        vst[2 * nod + 1] = dr - med;
        vdr[2 * nod + 1] = dr - med;
        maxi[2 * nod + 1] = dr - med;
    }
    if(maxi[nod] == 0)
    {
        vst[2 * nod] = 0;
        vdr[2 * nod] = 0;
        maxi[2 * nod] = 0;
        vst[2 * nod + 1] = 0;
        vdr[2 * nod + 1] =  0;
        maxi[2 * nod + 1] = 0;
    }
    if(a <= st && dr <= b)
    {
        if(c)
        {
            vst[nod] = 0;
            vdr[nod] = 0;
            maxi[nod] = 0;
        }
        else
        {
            vst[nod] = dr - st + 1;
            vdr[nod] = dr - st + 1;
            maxi[nod] = dr - st + 1;
        }
        return;
    }
    if(a <= med)
        update(2 * nod, st, med, c);
    if(med < b)
        update(2 * nod + 1, med + 1, dr, c);
    if(maxi[2 * nod] == med - st + 1)
        vst[nod] = maxi[2 * nod] + vst[2 * nod + 1];
    else
        vst[nod] = vst[2 * nod];
    if(maxi[2 * nod + 1] == dr - med)
        vdr[nod] = maxi[2 * nod + 1] + vdr[2 * nod];
    else
        vdr[nod] = vdr[2 * nod + 1];
    maxi[nod] = max(maxi[2 * nod], maxi[2 * nod  + 1], vst[2 * nod + 1] + vdr[2 * nod]);
}


int main()
{
    fin >> n >> p;
    precalc(1, 1, n);
    for(int i = 1; i <= p; i++)
    {
        int cerinta;
        fin >> cerinta;
        if(cerinta < 3)
        {
            fin >> a >> b;
            b = a + b - 1;
            update(1, 1, n, 2 - cerinta);
        }
        else
            fout << maxi[1] << '\n';
    }
    return 0;
}