Cod sursa(job #1968342)

Utilizator jurjstyleJurj Andrei jurjstyle Data 17 aprilie 2017 17:12:36
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.29 kb
/*
Vom considera V[i] = 1 : camera libera si V[i] = 0 : camera ocupata
Vom lucra cu arbori de intervale asemanator problemei : subsecventa de suma maxima intervalului [l, r], dar aici ne intereseaza doar daca putem unii secventele de 1
*/

#include <fstream>

using namespace std;

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

const int NMAX = 100002;

int N, P;
int Left[4 * NMAX];
int Right[4 * NMAX];
int Best[4 * NMAX];
int A, B, val;

void Build(int nod, int st, int dr)
{
    if (st == dr)
    {
        Left[nod] = Right[nod] = Best[nod] = 1;
        return;
    }
    int mid = (st + dr) >> 1;
    Build (nod << 1, st, mid);
    Build ((nod << 1) + 1, mid + 1, dr);
    Left[nod] = Right[nod] = Best[nod] = Best[nod << 1] + Best[(nod << 1) + 1];
}
void Update(int nod, int st, int dr)
{
    if (A <= st && dr <= B)
    {
        Left[nod] = Right[nod] = Best[nod] = (dr - st + 1) * val;
        return;
    }

    int mid = (st + dr) >> 1;

    if (Best[nod] == 0)
    {
        Left[nod << 1] = Right[nod << 1] = Best[nod << 1] = 0;
        Left[(nod << 1) + 1] = Right[(nod << 1) + 1] = Best[(nod << 1) + 1] = 0;
    }
    else if (Best[nod] == dr - st + 1)
    {
        Left[nod << 1] = Right[nod << 1] = Best[nod << 1] = mid - st + 1;
        Left[(nod << 1) + 1] = Right[(nod << 1) + 1] = Best[(nod << 1) + 1] = dr - mid;
    }

    if (A <= mid)
        Update(nod << 1, st, mid);
    if (B > mid)
        Update((nod << 1) + 1, mid + 1, dr);

    Left[nod] = Left[nod << 1];
    if (Left[nod << 1] == (mid - st + 1))
        Left[nod] += Left[(nod << 1) + 1];

    Right[nod] = Right[(nod << 1) + 1];
    if (Right[(nod << 1) + 1] == (dr - mid))
        Right[nod] += Right[nod << 1];

    Best[nod] = max(Best[nod << 1], Best[(nod << 1) + 1]);
    Best[nod] = max(Best[nod], Right[nod << 1] + Left[(nod << 1) + 1]);
}


int Query()
{
    return Best[1];
}

int main()
{
    f >> N >> P;
    Build (1, 1, N);
    for (int i = 1; i <= P; ++i)
    {
        int tip;
        f >> tip;
        if (tip == 3)
            g << Query() << "\n";
        else
        {
            f >> A >> B;
            B += A - 1;
            val = tip - 1;
            Update (1, 1, N);
        }
    }
    f.close();
    g.close();
    return 0;
}