Cod sursa(job #808555)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 6 noiembrie 2012 21:53:36
Problema Hotel Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.86 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("hotel.in");
ofstream g("hotel.out");
#define LE 500000

int Arbst[LE],Arbdr[LE],Snext[LE],Arb[LE];
int i,n,m,tip,X,Y;


void update(int st,int dr,int nod,int value)
{
    int mij=(st+dr)/2;

    if (Snext[nod]!=0)
    {
        int K=Snext[nod]==1?(dr-st+1):0;

        Arbst[nod]=K;
        Arbdr[nod]=K;
        Arb[nod]=K;

        Snext[nod*2]=Snext[nod];
        Snext[nod*2+1]=Snext[nod];
    }
    int nodi=nod;
    nod=nod*2;

       if (Snext[nod]!=0)
    {
        int K=Snext[nod]==1?(dr-st+1):0;

        Arbst[nod]=K;
        Arbdr[nod]=K;
        Arb[nod]=K;

        Snext[nod*2]=Snext[nod];
        Snext[nod*2+1]=Snext[nod];
    }
    nod=nodi*2+1;

       if (Snext[nod]!=0)
    {
        int K=Snext[nod]==1?(dr-st+1):0;

        Arbst[nod]=K;
        Arbdr[nod]=K;
        Arb[nod]=K;

        Snext[nod*2]=Snext[nod];
        Snext[nod*2+1]=Snext[nod];
    }
    nod=nodi;

    if (st>=X&&dr<=Y)
    {
        int V=value==1?(dr-st+1):0;

        Arb[nod]=V;
        Arbst[nod]=V;
        Arbdr[nod]=V;

        Snext[nod*2]=value;
        Snext[nod*2+1]=value;

        int Kdr,Kst;

        if (value==0)
        {
            Kdr=0;
            Kst=0;
        }
        else
        {
            Kdr=dr-(mij+1)+1;
            Kst=mij-st+1;
        }

        Arb[nod*2]=Kst;
        Arb[nod*2+1]=Kdr;

        Arbst[nod*2]=Kst;
        Arbst[nod*2+1]=Kdr;

        Arbdr[nod*2]=Kst;
        Arbdr[nod*2+1]=Kdr;

        if (value==1)
            Arb[nod]=dr-st+1;
        else Arb[nod]=0;

        return ;
    }

    if (st<=Y&&mij>=X)
        update(st,mij,nod*2,value);

    if (mij+1<=Y&&dr>=X)
        update(mij+1,dr,nod*2+1,value);

    Arb[nod]=max(Arb[nod*2],Arb[nod*2+1]);

    Arb[nod]=max(Arb[nod],Arbdr[nod*2]+Arbst[nod*2+1]);


    if (Arbdr[nod*2+1]!= (dr-(mij+1)+1)  )
        Arbdr[nod]=Arbdr[nod*2+1];
    else
        Arbdr[nod]=Arbdr[nod*2+1]+Arbdr[nod*2];


    if (Arbst[nod*2]!=(mij-st+1))
        Arbst[nod]=Arbst[nod*2];
    else
        Arbst[nod]=Arbst[nod*2]+Arbst[nod*2+1];

}


int query(int nod)
{
    return Arb[nod];
}
void init(int nod,int st,int dr)
{
    Arb[nod]=(dr-st)+1;
    Arbst[nod]=dr-st+1;
    Arbdr[nod]=dr-st+1;
    int mij=(st+dr)/2;

    if (st==dr) return;


    init(nod*2,st,mij);
    init(nod*2+1,mij+1,dr);
}
int main()
{


    f>>n>>m;
    init(1,1,n);

    for(i=1; i<=m; ++i)
    {
        f>>tip;
        if (tip==1||tip==2)
        {
            f>>X>>Y;
            Y=X+Y-1;

            if (tip==1)
                update(1,n,1,-1);
            else
                update(1,n,1,1);
        }

        if (tip==3)
            g<<query(1)<<'\n';
    }



    f.close();
    g.close();
    return 0;
}