Cod sursa(job #1474941)

Utilizator fluture.godlikeGafton Mihnea Alexandru fluture.godlike Data 23 august 2015 10:57:16
Problema Hotel Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.1 kb
#include <cstdio>
#include <algorithm>

#define NMAX 100007

using namespace std;
FILE *fin, *fout;
int n, p, c, in, m, tip;
struct arbore
{
    int sol;
    int prefix;
    int sufix;
} arb[3*NMAX];

void init(int st, int dr, int nod)
{
    if(st == dr)
    {
        arb[nod].sol = 1;
        arb[nod].prefix = 1;
        arb[nod].sufix = 1;
        return ;
    }
    int mij = (st+dr)/2;
    init(st, mij, 2*nod);
    init(mij+1, dr, 2*nod+1);
    arb[nod].sol = dr -st + 1;
    arb[nod].prefix = dr -st + 1;
    arb[nod].sufix = dr -st + 1;
}
void pun(int st, int dr, int st1, int dr1, int nod)
{
    if(st == st1 && dr == dr1)
    {
        if(tip == 1)
        {
            arb[nod].sol = 0;
            arb[nod].prefix = 0;
            arb[nod].sufix = 0;
        }
        else
        {
            arb[nod].sol = dr - st +1;
            arb[nod].prefix = dr - st +1;
            arb[nod].sufix = dr - st +1;
        }
        return ;
    }
    int mij = (st+dr)/2, f = 0;
    if(arb[nod].sol == arb[nod].prefix && arb[nod].sol == arb[nod].sufix)
    {
        if(arb[nod].sol == 0)
        {
            arb[2*nod].sol = 0;
            arb[2*nod].prefix = 0;
            arb[2*nod].sufix = 0;
            arb[2*nod+1].sol = 0;
            arb[2*nod+1].prefix = 0;
            arb[2*nod+1].sufix = 0;
        }
        else
        {
            arb[2*nod].sol = mij-st+1;
            arb[2*nod].prefix = mij-st+1;
            arb[2*nod].sufix = mij-st+1;
            arb[2*nod+1].sol = dr - mij;
            arb[2*nod+1].prefix = dr - mij;
            arb[2*nod+1].sufix = dr - mij;
        }
    }
    if(dr1 <= mij)
    {
        pun(st, mij, st1, dr1, 2*nod);
        f =1;
    }
    if(st1 > mij)
    {
        pun(mij+1, dr, st1, dr1, 2*nod+1);
        f = 1;
    }
    if(!f)
    {
        pun(st, mij, st1, mij, 2*nod);
        pun(mij+1, dr, mij+1, dr1, 2*nod+1);
    }
    arb[nod].prefix = arb[2*nod].prefix;
    if(arb[2*nod].sol == mij - st + 1) arb[nod].prefix = arb[2*nod].sol + arb[2*nod+1].prefix;
    arb[nod].sufix = arb[2*nod+1].sufix;
    if(arb[2*nod+1].sol == dr - mij) arb[nod].sufix = arb[2*nod+1].sol + arb[2*nod].sufix;
    arb[nod].sol = max(arb[2*nod].sufix + arb[2*nod+1].prefix, max(arb[2*nod].sol, arb[2*nod+1].sol));
}

int main()
{
    fin = freopen("hotel.in", "r", stdin);
    fout = freopen("hotel.out", "w", stdout);
    scanf("%d%d", &n, &p);
    init(1, n, 1);
    for(int i = 1; i<= p; ++i)
    {
        scanf("%d", &c);
        if(c == 1)
        {
            scanf("%d%d", &in, &m);
            tip = 1;
            pun(1, n, in, in+m-1, 1);
            //printf("check %d %d %d\n", arb[1].sol, arb[1].prefix, arb[1].sufix);
        }
        if(c == 2)
        {
            scanf("%d%d", &in, &m);
            tip = 2;
            pun(1, n, in, in+m-1, 1);
            //printf("check %d %d %d\n", arb[1].sol, arb[1].prefix, arb[1].sufix);
        }
        if(c == 3)
        {
            printf("%d\n", arb[1].sol);
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}