Cod sursa(job #825765)

Utilizator auRSTARHreapca Aurelian auRSTAR Data 29 noiembrie 2012 15:35:48
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include<cstdio>
#define max(a,b) a>b?a:b
void build(int,int,int),update(int,int,int),goleste(int,int,int);
int n,m,t,CURR[300010],ST[300010],DR[300010],st,x,fn;
int main()
{
    freopen("hotel.in","r",stdin);
    freopen("hotel.out","w",stdout);
    scanf("%d%d",&n,&m);
    CURR[1]=ST[1]=DR[1]=n;
    for(;m;--m)
    {
        scanf("%d",&t);
        if(t==3)printf("%d\n",CURR[1]);
        if(t==1)
        {
            scanf("%d%d",&st,&x);
            fn=st+x-1;
            update(1,n,1);
        }
        if(t==2)
        {
            scanf("%d%d",&st,&x);
            fn=st+x-1;
            goleste(1,n,1);
        }
    }
}
void update(int X,int Y,int cnt)
{
    if(st<=X&&Y<=fn)
    {
        CURR[cnt]=0;
        ST[cnt]=0;
        DR[cnt]=0;
    }
    else
    {
        int mij=(X+Y)/2;
        if(CURR[cnt]==Y-X+1)
        {
            CURR[cnt+cnt]=ST[cnt+cnt]=DR[cnt+cnt]=mij-X+1;
            CURR[cnt+cnt+1]=ST[cnt+cnt+1]=DR[cnt+cnt+1]=Y-mij;
        }
        if(st<=mij)update(X,mij,cnt+cnt);
        if(fn>=mij+1)update(mij+1,Y,cnt+cnt+1);
        CURR[cnt]=max(CURR[cnt+cnt],CURR[cnt+cnt+1]);
        CURR[cnt]=max(CURR[cnt],ST[cnt+cnt+1]+DR[cnt+cnt]);
        if(ST[cnt+cnt]==mij-X+1)ST[cnt]=ST[cnt+cnt]+ST[cnt+cnt+1]; else ST[cnt]=ST[cnt+cnt];
        if(DR[cnt+cnt+1]==Y-mij)DR[cnt]=DR[cnt+cnt]+DR[cnt+cnt+1]; else DR[cnt]=DR[cnt+cnt+1];
    }
}
void goleste(int X,int Y,int cnt)
{
    if(st<=X&&Y<=fn)
    {
        CURR[cnt]=Y-X+1;
        ST[cnt]=Y-X+1;
        DR[cnt]=Y-X+1;
    }
    else
    {
        int mij=(X+Y)/2;
        if(!CURR[cnt])CURR[cnt+cnt]=CURR[cnt+cnt+1]=ST[cnt+cnt]=ST[cnt+cnt+1]=DR[cnt+cnt]=DR[cnt+cnt+1]=0;
        if(st<=mij)goleste(X,mij,cnt+cnt);
        if(fn>=mij+1)goleste(mij+1,Y,cnt+cnt+1);
        CURR[cnt]=max(CURR[cnt+cnt],CURR[cnt+cnt+1]);
        CURR[cnt]=max(CURR[cnt],ST[cnt+cnt+1]+DR[cnt+cnt]);
        if(ST[cnt+cnt]==mij-X+1)ST[cnt]=ST[cnt+cnt]+ST[cnt+cnt+1]; else ST[cnt]=ST[cnt+cnt];
        if(DR[cnt+cnt+1]==Y-mij)DR[cnt]=DR[cnt+cnt]+DR[cnt+cnt+1]; else DR[cnt]=DR[cnt+cnt+1];
    }
}