Cod sursa(job #1336933)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 8 februarie 2015 14:24:07
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 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;

void Build(int nod, int st, int dr);
void Update(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);
    Build(1, 1, n);
    int tip;
    while ( m-- )
    {
        is >> tip;
        if ( tip == 3 )
        {
            os << t[1] << "\n";
            continue;
        }
        is >> lt >> val;
        rt = lt + val - 1;
        if ( tip == 1 )
            val = 0;
        else
            val = 1;
        Update(1, 1, n);
    }
    is.close();
    os.close();
    return 0;
}

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 Update(int nod, int st, int dr)
{
    if ( lt <= st && dr <= rt )
    {
        s[nod] = d[nod] = t[nod] = ( dr - st + 1 ) * val;
        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];
    if ( s[2 * nod] == md - st + 1 )
        s[nod] += s[2 * nod + 1];
    d[nod] = d[2 * nod + 1];
    if ( d[2 * nod + 1] == dr - md )
        d[nod] += d[2 * nod];
    t[nod] = max(d[2 * nod] + s[2 * nod + 1], max(t[2 * nod], t[2 * nod + 1]));
}

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