Cod sursa(job #1376301)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 5 martie 2015 16:59:02
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

using VI = vector<int>;

int n, m;
int tip, a, b, 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);
    while ( m-- )
    {
        is >> tip;
        if ( tip == 3 )
            os << t[1] << "\n";
        else
        {
            is >> a >> val;
            b = a + val - 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 ( a <= st && dr <= b )
    {
        s[nod] = d[nod] = t[nod] = ( dr - st + 1 ) * val;
        return;
    }
    Push(nod, st, dr);
    int md = ( st + dr ) / 2;
    if ( a <= md )
        Update(2 * nod, st, md);
    if ( md < b )
        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(max(t[2 * nod], t[2 * nod + 1]), d[2 * nod] + s[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)
{
    s[nod] = d[nod] = t[nod] = dr - st + 1;
    if ( st != dr )
    {
        int md = ( st + dr ) / 2;
        Build(2 * nod, st, md);
        Build(2 * nod + 1, md + 1, dr);
    }
}