Cod sursa(job #2013796)

Utilizator victoreVictor Popa victore Data 22 august 2017 13:58:40
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.43 kb
#include<cstdio>

using namespace std;

const int nmax=400010;

struct al
{
    int smax,sdr,sst;
}arb[nmax];

int lazy[nmax],x,y,type;

inline int max(int a,int b)
{
    if(a>b)
        return a;
    return b;
}

inline void buildtree(int node,int st,int dr)
{
    if(st==dr)
    {
        arb[node].sst=arb[node].sdr=arb[node].smax=1;
        return;
    }
    int med=((dr-st)>>1)+st;
    buildtree(node<<1,st,med);
    buildtree(node<<1|1,med+1,dr);
    arb[node].smax = max(arb[node<<1].smax ,max( arb[node<<1|1].smax , arb[node<<1].sdr + arb[node<<1|1].sst ));
    arb[node].sst = arb[node<<1].sst;
    if(arb[node<<1].smax == med - st + 1)
        arb[node].sst += arb[node<<1|1].sst;
    arb[node].sdr = arb[node<<1|1].sdr;
    if(arb[node<<1|1].smax == dr - med)
        arb[node].sdr += arb[node<<1].sdr;
}

inline void process(int node,int st,int dr)
{
    if(lazy[node] == 1)
        arb[node].smax = arb[node].sdr = arb[node].sst = 0;
    else
        arb[node].smax = arb[node].sdr = arb[node].sst = dr-st+1;
    if(st!=dr)
        lazy[node<<1] = lazy[node<<1|1] = lazy[node];
    lazy[node]=0;
}

inline void update(int node,int st,int dr)
{
    if(lazy[node])
        process(node,st,dr);

    if(dr < x || y < st)
        return;

    if(x <= st && dr <= y)
    {
        if(type == 1)
            arb[node].smax = arb[node].sst = arb[node].sdr = 0;
        else
            arb[node].smax = arb[node].sst = arb[node].sdr = dr-st+1;
        if(st!=dr)
            lazy[node<<1] = lazy[node<<1|1] = type;
        return;
    }

    int med=((dr-st)>>1) + st;
    update(node<<1 , st , med);
    update(node<<1|1 , med+1 , dr);
    arb[node].smax = max(arb[node<<1].smax ,max( arb[node<<1|1].smax , arb[node<<1].sdr + arb[node<<1|1].sst ));
    arb[node].sst = arb[node<<1].sst;
    if(arb[node<<1].smax == med - st + 1)
        arb[node].sst += arb[node<<1|1].sst;
    arb[node].sdr = arb[node<<1|1].sdr;
    if(arb[node<<1|1].smax == dr - med)
        arb[node].sdr += arb[node<<1].sdr;
}

int main()
{
    freopen("hotel.in","r",stdin);
    freopen("hotel.out","w",stdout);
    int n,q;
    scanf("%d%d",&n,&q);
    buildtree(1,1,n);
    while(q--)
    {
        scanf("%d",&type);
        if(type == 3)
            printf("%d\n",arb[1].smax);
        else
        {
            scanf("%d%d",&x,&y);
            y=x+y-1;
            update(1,1,n);
        }
    }
}