Cod sursa(job #2074500)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 24 noiembrie 2017 17:53:49
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.06 kb
#include<fstream>
using namespace std;
ifstream fi("hotel.in");
ofstream fo("hotel.out");
typedef struct boschet{int st,dr;} BOSCHET;
BOSCHET AI[262145];
int Lazy[262145],Best[262145];
int n,p,i,tip,lft,rgt,x;

void update(int nod, int st, int dr, int op)
{
    if(Lazy[nod])
    {
        if(Lazy[nod]==1)
        {
            //s-au ocupat camerele din intervalul [a,b]
            AI[nod].st=0;
            AI[nod].dr=0;
            Best[nod]=0;
        }
        else
        {
            AI[nod].st=dr-st+1;
            AI[nod].dr=dr-st+1;
            Best[nod]=dr-st+1;
        }
        if(st!=dr)
        {
            Lazy[2*nod]=Lazy[nod];
            Lazy[2*nod+1]=Lazy[nod];
        }
        Lazy[nod]=0;
    }
    if(dr<lft || st>rgt)
        return;
    int mij=(st+dr)/2;
    if(lft<=st && dr<=rgt)
    {
        if(op==1)
        {
            AI[nod].st=0;
            AI[nod].dr=0;
            Best[nod]=0;
        }
        else
        {
            AI[nod].st=dr-st+1;
            AI[nod].dr=dr-st+1;
            Best[nod]=dr-st+1;
        }
        if(st!=dr)
        {
            Lazy[2*nod]=op;
            Lazy[2*nod+1]=op;
        }
        return;
    }
    update(2*nod,st,mij,op);
    update(2*nod+1,mij+1,dr,op);
    AI[nod].st=AI[2*nod].st;
    if(AI[2*nod].st==(mij-st+1))
        AI[nod].st+=AI[2*nod+1].st;
    AI[nod].dr=AI[2*nod+1].dr;
    if(AI[2*nod+1].dr==(dr-mij))
        AI[nod].dr+=AI[2*nod].dr;
    Best[nod]=max(AI[nod].st,max(AI[nod].dr,max(AI[2*nod].dr+AI[2*nod+1].st,max(Best[2*nod],Best[2*nod+1]))));
}

int main()
{
    fi>>n>>p;
    lft=1;
    rgt=n;
    update(1,1,n,2);
    for(i=1; i<=p; i++)
    {
        fi>>tip;
        if(tip==1)
        {
            fi>>lft>>x;
            rgt=lft+x-1;
            update(1,1,n,1);
        }
        if(tip==2)
        {
            fi>>lft>>x;
            rgt=lft+x-1;
            update(1,1,n,2);
        }
        if(tip==3)
            fo<<Best[1]<<"\n";
    }
    fi.close();
    fo.close();
    return 0;
}