Cod sursa(job #2329055)

Utilizator triscacezarTrisca Vicol Cezar triscacezar Data 26 ianuarie 2019 12:16:42
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("hotel.in");
ofstream g("hotel.out");

int n,p,z,i,j,c;

struct aint{
    int mex,lft,rgt,lazy;
};

aint A[300010];

void propag(int poz,int lef,int rig)
{
    if(A[poz].lazy==-1)return ;
    if(!A[poz].lazy)
        A[poz].mex=A[poz].lft=A[poz].rgt=0;
    else
        A[poz].mex=A[poz].lft=A[poz].rgt=rig-lef+1;
    if(lef<rig)
        A[poz*2].lazy=A[poz*2+1].lazy=A[poz].lazy;
    A[poz].lazy=-1;
}

void upd(int poz,int lef,int rig,int a,int b,int val)
{
    if(a<=lef&&rig<=b)
    {
        A[poz].lazy=val;
        propag(poz,lef,rig);
        return ;
    }
    propag(poz,lef,rig);
    int mij=(lef+rig)/2;
    if(a<=mij)
        upd(poz*2,lef,mij,a,b,val);
    else
        propag(poz*2,lef,mij);
    if(mij<b)
        upd(poz*2+1,mij+1,rig,a,b,val);
    else
        propag(poz*2+1,mij+1,rig);

    A[poz].mex=max(A[2*poz].rgt+A[2*poz+1].lft,max(A[2*poz].mex,A[2*poz+1].mex));

    A[poz].lft=A[2*poz].lft;
    if(A[2*poz].lft==mij-lef+1)
        A[poz].lft+=A[2*poz+1].lft;

    A[poz].rgt=A[2*poz+1].rgt;
    if(A[2*poz+1].rgt==rig-mij)
        A[poz].rgt+=A[2*poz].rgt;
}

int main()
{
    f>>n>>p;
    z=1;
    while(z<=n)z<<=1;z--;
    for(i=1;i<=2*z+1;i++)
        A[i].lazy=-1;
    upd(1,1,z+1,1,n,1);
    for(;p;p--)
    {
        f>>c;
        if(c==3)
            g<<A[1].mex<<'\n';
        else
        {
            int a,b;
            f>>a>>b;
            b+=a-1;
            upd(1,1,z+1,a,b,c-1);
        }
    }
    return 0;
}