Cod sursa(job #1392195)

Utilizator teo.serbanescuTeo Serbanescu teo.serbanescu Data 18 martie 2015 14:01:07
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.31 kb
#include <stdio.h>
#include <string.h>

using namespace std;

FILE *f,*g;

const long nmax=100005;

long n,m,val,i,j,a,b,x,st[3*nmax],dr[3*nmax],mij[3*nmax],maxim;

int cod;

void update(int nod, int ic, int sf)
{
    long mi,nr;
    if (ic==sf)
    {
        st[nod]=val;
        dr[nod]=val;
        mij[nod]=val;
    }
    else
    {
        mi=(ic+sf)/2;
        if (x<=mi) update(2*nod,ic,mi);
                else update(2*nod+1,mi+1,sf);
        if (mij[2*nod]>=mij[2*nod+1]) mij[nod]=mij[2*nod];
                        else mij[nod]=mij[2*nod+1];
        if (dr[2*nod]+st[2*nod+1]>mij[nod]) mij[nod]=dr[2*nod]+st[2*nod+1];
        st[nod]=st[2*nod];
        dr[nod]=dr[2*nod+1];
        nr=mi-ic+1;
        if (st[2*nod]==nr) st[nod]+=st[2*nod+1];
        nr=sf-mi;
        if (dr[2*nod+1]==nr) dr[nod]+=dr[2*nod];

    }

}

void initialize(int nod, int ic, int sf)
{
    long mi,nr;
    if (ic==sf)
    {
        st[nod]=1;dr[nod]=1;mij[nod]=1;
    }
    else
    {
        mi=(ic+sf)/2;
        initialize(2*nod,ic,mi);
        initialize(2*nod+1,mi+1,sf);
        if (mij[2*nod]>=mij[2*nod+1]) mij[nod]=mij[2*nod];
                        else mij[nod]=mij[2*nod+1];
        if (dr[2*nod]+st[2*nod+1]>mij[nod]) mij[nod]=dr[2*nod]+st[2*nod+1];
        st[nod]=st[2*nod];
        dr[nod]=dr[2*nod+1];
        nr=mi-ic+1;
        if (st[2*nod]==nr) st[nod]+=st[2*nod+1];
        nr=sf-mi;
        if (dr[2*nod+1]==nr) dr[nod]+=dr[2*nod];
    }

}

void updatee(int nod, int ic, int sf)
{
    long mi,nr;

    if ((a <= ic) && (sf <= b))
    {
        if (val)
        {
            st[nod] = sf - ic + 1;
            dr[nod] = sf - ic + 1;
            mij[nod] = sf - ic + 1;
        }
        else
        {
            st[nod] = 0;
            dr[nod] = 0;
            mij[nod] = 0;
        }
    }
    else
    {
        mi = (ic + sf) / 2;
        if (val)
            {
                if ((st[nod] == 0) && (mij[nod] == 0) && (dr[nod] == 0))
                {
                    st[2 * nod] = 0; mij[2 * nod] = 0; dr[2 * nod] = 0;
                    st[2 * nod + 1] = 0; mij[2 * nod + 1] = 0; dr[2 * nod + 1] = 0;
                    st[nod]=mij[nod]=dr[nod] = sf - ic + 1;
                }
            }
            else
            {
                if ((mij[nod] == sf - ic + 1) && (st[nod] == sf - ic + 1) && (dr[nod] == sf - ic + 1))
                {
                    st[2*nod]=mij[2*nod]=dr[2*nod]= mi - ic + 1;
                    st[2*nod+1]=mij[2*nod+1]=dr[2*nod+1]= sf - mi;
                    st[nod]=mij[nod]=dr[nod] = 0;
                }
            }
        if (a <= mi) updatee(2 * nod, ic, mi);
        if (mi < b) updatee(2 * nod + 1, mi + 1, sf);
        mi = (ic + sf) / 2;
        if (mij[2*nod]>=mij[2*nod+1]) mij[nod]=mij[2*nod];
                        else mij[nod]=mij[2*nod+1];
        if (dr[2*nod]+st[2*nod+1]>mij[nod]) mij[nod]=dr[2*nod]+st[2*nod+1];
        st[nod]=st[2*nod];
        dr[nod]=dr[2*nod+1];
        nr=mi-ic+1;
        if (st[2*nod]==nr) st[nod]+=st[2*nod+1];
        nr=sf-mi;
        if (dr[2*nod+1]==nr) dr[nod]+=dr[2*nod];
    }
}


void solve()
{
    memset(st,0,sizeof(st));
    memset(dr,0,sizeof(dr));
    memset(mij,0,sizeof(mij));
    initialize(1,1,n);
    for (i=1;i<=m;i++)
    {
        fscanf(f,"%d",&cod);
        if (cod==3)
        {
            if (st[1]>dr[1]) maxim=st[1];
                    else maxim=dr[1];
            if (mij[1]>maxim) maxim=mij[1];
            fprintf(g,"%ld\n",maxim);
        }
        if (cod==1)
        {
            fscanf(f,"%ld%ld",&a,&b);
            b=a+b-1;
            val = 0;
            updatee(1, 1, n);
            /*for (j=a;j<=b;j++) {
                                x=j;val=0;
                                update(1,1,n);
            }*/
        }
        if (cod==2)
        {
            fscanf(f,"%ld%ld",&a,&b);
            b=a+b-1;
            val = 1;
            updatee(1, 1, n);
            /*for (j=a;j<=b;j++) {
                                x=j;val=1;
                                update(1,1,n);
            }*/
        }
    }
}

int main()
{
    f=fopen("hotel.in","r");
    g=fopen("hotel.out","w");
    fscanf(f,"%ld%ld",&n,&m);
    solve();
    fclose(f);fclose(g);
    return 0;
}