Cod sursa(job #6824)

Utilizator victorsbVictor Rusu victorsb Data 20 ianuarie 2007 23:46:44
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.95 kb
#include <cstdio>
#include <algorithm>
#define Nmax 100010

using namespace std;

int n, p, i, j, c, a, b;
int mst[262144], mdr[262144], tip[262144], mx[262144];

void update1(int nod, int st, int dr)
{
    if (a <= st && dr <= b)
    {
        tip[nod] = 2;
        mst[nod] = mdr[nod] = mx[nod] = 0;
    }
    else
    {
        int mij = (st + dr) >> 1;
        if (tip[nod] == 0)
        {
            tip[nod << 1] = tip[(nod << 1) + 1] = 0;
            mst[nod << 1] = mdr[nod << 1] = mx[nod << 1] = mij - st + 1;
            mst[(nod << 1) + 1] = mdr[(nod << 1) + 1] = mx[(nod << 1) + 1] = dr - (mij + 1) + 1;
        }
        tip[nod] = 1;
        if (a <= mij)
            update1(nod << 1, st, mij);
        if (mij < b)
            update1((nod << 1) + 1, mij + 1, dr);
        mx[nod] = max(mdr[nod << 1] + mst[(nod << 1) + 1], max(mx[nod << 1], mx[(nod << 1) + 1]));
        mdr[nod] = mdr[(nod << 1) + 1];
        if (tip[(nod << 1) + 1] == 0)
            mdr[nod] += mdr[nod << 1];
        mst[nod] = mst[nod << 1];
        if (tip[nod << 1] == 0)
            mst[nod] += mst[(nod << 1) + 1];
    }
}

void update2(int nod, int st, int dr)
{
    if (a <= st && dr <= b)
    {
        tip[nod] = 0;
        mst[nod] = mdr[nod] = mx[nod] = dr - st + 1;
    }
    else
    {
        int mij = (st + dr) >> 1;
        if (tip[nod] == 2)
        {
            tip[nod << 1] = tip[(nod << 1) + 1] = 2;
            mst[nod << 1] = mdr[nod << 1] = mx[nod << 1] = 0;
            mst[(nod << 1) + 1] = mdr[(nod << 1) + 1] = mx[(nod << 1) + 1] = 0;
        }
        tip[nod] = 1;
        if (a <= mij)
            update2(nod << 1, st, mij);
        if (mij < b)
            update2((nod << 1) + 1, mij + 1, dr);
        if (tip[nod << 1] == 0 && tip[(nod << 1) + 1] == 0)
        {
            tip[nod] = 0;
            mst[nod] = mdr[nod] = mx[nod] = dr - st + 1;
        }
        else
        {
            mx[nod] = max(mdr[nod << 1] + mst[(nod << 1) + 1], max(mx[nod << 1], mx[(nod << 1) + 1]));
            mdr[nod] = mdr[(nod << 1) + 1];
            if (tip[(nod << 1) + 1] == 0)
                mdr[nod] += mdr[nod << 1];
            mst[nod] = mst[nod << 1];
            if (tip[nod << 1] == 0)
                mst[nod] += mst[(nod << 1) + 1];
        }
    }
}

void citire()
{
    int i;
    scanf("%d %d\n", &n, &p);
    tip[1] = 0;
    mdr[1] = mst[1] = mx[1] = n;
    for (i=1; i<=p; ++i)
    {
        scanf("%d", &c);
        if (c == 1)
        {
            scanf("%d %d\n", &a, &b);
            b += a - 1;
            update1(1,1,n);
        }
        else if (c == 2)
        {
            scanf("%d %d\n", &a, &b);
            b += a - 1;
            update2(1,1,n);
        }
        else if (c == 3)
        {
            printf("%d\n", mx[1]);
        }
    }
}

int main()
{
    freopen("hotel.in","r",stdin);
    freopen("hotel.out","w",stdout);
    citire();
    return 0;
}