Cod sursa(job #2125212)

Utilizator filip.mihalutMihalut Filip filip.mihalut Data 8 februarie 2018 11:20:38
Problema Hotel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <iostream>
#include <fstream>
#define dim 100005
using namespace std;

ifstream f ("hotel.in");
ofstream g ("hotel.out");

bool arb[4*dim + 50]; int x,a,b,m,n,i,ultim,s,Max,caz;

inline void update(int st,int dr,int a,int b,int nod,bool val)
{
    if(st == dr)
    {
        arb[nod] = val;
        return;
    }

    int m = (st + dr) / 2;

    if(m >= a)
        update(st,m,a,b,2*nod,val);
    if(m < b)
        update(m + 1,dr,a,b,2*nod + 1,val);

    if(arb[2*nod + 1] == 0 && arb[2*nod] == 0)
        arb[nod] = 0;
    else
        arb[nod] = 1;
}

inline void query(int st,int dr,int nod)
{
    if(arb[nod] == 0)
    {
        if(st == ultim + 1)
            s += (dr - st + 1) , ultim = dr;
        else
            Max = max(Max,s), s = dr - st + 1, ultim = dr;
        return;
    }

    if(st == dr)
        return;

    int m = (st + dr) / 2;

    query(st,m,2*nod);
    query(m + 1,dr,2*nod + 1);
}

int main()
{
    f >> n >> m;
    for(i = 1;i <= m;i++)
    {
        f >> caz;

        switch(caz)
        {
        case 1:
            f >> a >> b;
            b = a + b - 1;
            update(1,n,a,b,1,1);
            break;
        case 2:
            f >> a >> b;
            b = a + b - 1;
            update(1,n,a,b,1,0);
            break;
        case 3:
            s = ultim = Max = 0;
            query(1,n,1);
            g << max(s,Max) << '\n';
            break;
        }
    }
    return 0;
}