Cod sursa(job #1060662)

Utilizator mihail.jianuJianu Mihail mihail.jianu Data 18 decembrie 2013 12:36:15
Problema Hotel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.44 kb
#include <cstdio>

const int N = 100000;

struct arbore
{
    int sufix, prefix, secvMax;
};

arbore t [1 << 18];
int n, p, c1, c2;

void fisiere ()
{
    freopen ("hotel.in", "r", stdin);
    freopen ("hotel.out", "w", stdout);
}

void initTree (int poz, int st, int dr)
{
    t [poz]. sufix = t [poz]. prefix = t [poz]. secvMax = dr - st + 1;

    int m = (st + dr) / 2;

    if (st != dr)
    {
        initTree (poz * 2, st ,m);
        initTree (poz * 2 + 1, m + 1, dr);
    }
}

int max3 (int a, int b, int c)
{
    int max = a;

    if (b > max)
        max = b;
    if (c > max)
        max = c;

    return max;
}

void actual (bool f, int poz, int st, int dr)
{
    int m = (st + dr) / 2, cc;

    if (st == dr)
    {
        if (! f)
            t [poz]. sufix = t [poz]. prefix = t [poz]. secvMax = 1;
        else
            t [poz]. sufix = t [poz]. prefix = t [poz]. secvMax = 0;

        return;
    }

    if (m >= c2)
    {
        actual (f, poz * 2, st, m);

        t [poz]. secvMax = max3 (t [poz * 2]. secvMax, t [poz * 2 + 1]. secvMax, t [poz * 2]. sufix + t [poz * 2 + 1]. prefix);
        if (t [poz * 2 + 1]. sufix == dr - st + 1)
            t [poz]. sufix = t [poz * 2]. sufix + t [poz * 2 + 1]. sufix;
        else
            t [poz]. sufix = t [poz * 2 + 1]. sufix;

        if (t [poz * 2]. sufix == dr - st + 1)
            t [poz]. prefix = t [poz * 2 + 1]. sufix + t [poz * 2]. prefix;
        else
            t [poz]. prefix = t [poz * 2]. prefix;
    }
    else if (m  < c1)
    {
        actual (f, poz * 2 + 1, m + 1, dr);

        t [poz]. secvMax = max3 (t [poz * 2]. secvMax, t [poz * 2 + 1]. secvMax, t [poz * 2]. sufix + t [poz * 2 + 1]. prefix);
        if (t [poz * 2 + 1]. sufix == dr - st + 1)
            t [poz]. sufix = t [poz * 2]. sufix + t [poz * 2 + 1]. sufix;
        else
            t [poz]. sufix = t [poz * 2 + 1]. sufix;

        if (t [poz * 2]. sufix == dr - st + 1)
            t [poz]. prefix = t [poz * 2 + 1]. sufix + t [poz * 2]. prefix;
        else
            t [poz]. prefix = t [poz * 2]. prefix;
    }
    else
    {
        cc = c2;
        c2 = m;

        actual (f, poz * 2, st, m);

        c2 = cc;
        cc = c1;
        c1 = m + 1;

        actual (f, poz * 2 + 1, m + 1, dr);

        c1 = cc;

        t [poz]. secvMax = max3 (t [poz * 2]. secvMax, t [poz * 2 + 1]. secvMax, t [poz * 2]. sufix + t [poz * 2 + 1]. prefix);
        if (t [poz * 2 + 1]. sufix == dr - st + 1)
            t [poz]. sufix = t [poz * 2]. sufix + t [poz * 2 + 1]. sufix;
        else
            t [poz]. sufix = t [poz * 2 + 1]. sufix;

        if (t [poz * 2]. sufix == dr - st + 1)
            t [poz]. prefix = t [poz * 2 + 1]. sufix + t [poz * 2]. prefix;
        else
            t [poz]. prefix = t [poz * 2]. prefix;

        c1 = cc;
    }
}

int main ()
{
    int in, c, l;

    fisiere ();

    scanf ("%d %d", & n, & p);

    initTree (1, 1, n);

    for (in = 1; in <= p; in ++)
    {
        scanf ("%d", & c);

        if (c == 3)
            printf ("%d\n", t [1]. secvMax);
        else if (c == 1)
        {
            scanf ("%d %d", & c1, & l);

            c2 = c1 + l - 1;

            actual (true, 1, 1, n);
        }
        else
        {
            scanf ("%d %d", & c1, & l);

            c2 = c1 + l - 1;

            actual (false, 1, 1, n);
        }

    }

    return 0;
}