Cod sursa(job #1599120)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 13 februarie 2016 17:05:03
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream is("hotel.in");
ofstream os("hotel.out");

using VI = vector<int>;

int n, m;
int lt, rt, val;
VI s, d, t; // 1 - liber

void Update(int nod, int st, int dr);
void Build(int nod, int st, int dr);
void Push(int nod, int st, int dr);

int main()
{
    is >> n >> m;
    s = d = t = VI(4 * n + 1);
    Build(1, 1, n);
    int tip;
    while ( m-- )
    {
        is >> tip;
        if ( tip == 3 )
            os << t[1] << "\n";
        else
        {
            is >> lt >> rt;
            rt += lt - 1;
            if ( tip == 1 )
                val = 0;
            else
                val = 1;
            Update(1, 1, n);
        }
    }
    is.close();
    os.close();
    return 0;
}

void Update(int nod, int st, int dr)
{
    if ( lt <= st && dr <= rt )
    {
        s[nod] = d[nod] = t[nod] = val * ( dr - st + 1 );
        return;
    }
    if ( st == dr )
        return;
    Push(nod, st, dr);
    int md = ( st + dr ) / 2;
    if ( lt <= md )
        Update(2 * nod, st, md);
    if ( md < rt )
        Update(2 * nod + 1, md + 1, dr);
    s[nod] = s[2 * nod] + ( s[2 * nod] == md + 1 - st ? s[2 * nod + 1] : 0 );
    d[nod] = d[2 * nod + 1] + ( d[2 * nod + 1] == dr - md ? d[2 * nod] : 0 );
    t[nod] = max(d[2 * nod] + s[2 * nod + 1], max(t[2 * nod], t[2 * nod + 1]));
}

void Push(int nod, int st, int dr)
{
    if ( !t[nod] )
    {
        s[2 * nod] = d[2 * nod] = t[2 * nod] = 0;
        s[2 * nod + 1] = d[2 * nod + 1] = t[2 * nod + 1] = 0;
    }
    else
        if ( t[nod] == dr - st + 1 )
        {
            int md = ( st + dr ) / 2;
            s[2 * nod] = d[2 * nod] = t[2 * nod] = md - st + 1;
            s[2 * nod + 1] = d[2 * nod + 1] = t[2 * nod + 1] = dr - md;
        }
}

void Build(int nod, int st, int dr)
{
    if ( st == dr )
    {
        s[nod] = d[nod] = t[nod] = 1;
        return;
    }
    int md = ( st + dr ) / 2;
    Build(2 * nod, st, md);
    Build(2 * nod + 1, md + 1, dr);
    s[nod] = d[nod] = t[nod] = dr - st + 1;
}