Cod sursa(job #1511608)

Utilizator tudormaximTudor Maxim tudormaxim Data 26 octombrie 2015 22:46:15
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <fstream>
using namespace std;
const int nmax = 100005;
int x, y, arb[nmax*4], a[nmax*4], b[nmax*4], c[nmax*4];

void build(int nod, int st, int dr)
{
    arb[nod]=a[nod]=b[nod]=dr-st+1;
    if (st<dr)
    {
        int mij=(st+dr)/2;
        build(nod*2, st, mij);
        build(nod*2+1, mij+1, dr);
    }
}

void update(int nod, int st, int dr, bool caz)
{
    if (x<=st && dr<=y)
    {
        if (!caz) arb[nod]=a[nod]=b[nod]=dr-st+1;
        else arb[nod]=a[nod]=b[nod]=0;
        return;
    }
    else
    {
        if (arb[nod]==0)
        {
            arb[nod*2]=a[nod*2]=b[nod*2]=0;
            arb[nod*2+1]=b[nod*2+1]=a[nod*2+1]=0;
        }
        if (arb[nod]==dr-st+1)
        {
            arb[nod*2]=a[nod*2]=b[nod*2]=(st+dr)/2-st+1;
            arb[nod*2+1]=a[nod*2+1]=b[nod*2+1]=dr-(st+dr)/2;
        }
        int mij=(st+dr)/2;
        if (x<=mij) update(nod*2, st, mij, caz);
        if (y>mij) update(nod*2+1, mij+1, dr, caz);

        arb[nod]=max(max(arb[nod*2],arb[nod*2+1]),b[nod*2]+a[nod*2+1]);
        a[nod]=a[nod*2];
        b[nod]=b[nod*2+1];
        if (a[nod*2]==(st+dr)/2-st+1)
            a[nod]+=a[nod*2+1];
        if (b[nod*2+1]==dr-(st+dr)/2)
            b[nod]+=b[nod*2];
    }
}

int main()
{
    ifstream fin ("hotel.in");
    ofstream fout ("hotel.out");
    int n, p, i, op;
    fin >> n >> p;
    x=1; y=n;
    build(1, 1, n);
    for (i=1; i<=p; i++)
    {
        fin >> op;
        if (op==1)
        {
            fin >> x >> y;
            y=y+x-1;
            update(1, 1, n, true);
        }
        else if (op==2)
        {
            fin >> x >> y;
            y=y+x-1;
            update(1, 1, n, false);
        }
        else fout << arb[1] << "\n";
    }
    return 0;
}