Cod sursa(job #2663400)

Utilizator adiaioanaAdia R. adiaioana Data 26 octombrie 2020 12:11:34
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <fstream>
#define NMAX 800100
using namespace std;
ifstream fin("hotel.in");
ofstream fout("hotel.out");
int N;
int arbint[NMAX], ldr[NMAX],lst[NMAX], lmax[NMAX], lazy[NMAX];
void build(int nod,int st,int dr)
{
    arbint[nod]=dr-st+1;
    lst[nod]=ldr[nod]=lmax[nod]=dr-st+1;
    lazy[nod]=-1;
    if(st!=dr)
        build(2*nod,st,(st+dr)/2);
    if(st!=dr)
        build(2*nod+1,(st+dr)/2+1,dr);
}

void propag(int nod,int st, int dr)
{
    if(lazy[nod]!=-1 && st!=dr)
    {
        lazy[2*nod]=lazy[2*nod+1]=lazy[nod];
        int mid=(st+dr)/2;
        arbint[2*nod]=(1-lazy[nod])*(mid-st+1);
        arbint[2*nod+1]=(1-lazy[nod])*(dr-mid);
        lst[2*nod]=ldr[2*nod]=lmax[2*nod]=arbint[2*nod];
        lst[2*nod+1]=ldr[2*nod+1]=lmax[2*nod+1]=arbint[2*nod+1];
    }
    lazy[nod]=-1;
}

void update(int nod, int st,int dr,int L,int R,int val)
{
    if(L<=st && dr<=R)
    {
        arbint[nod]=lst[nod]=ldr[nod]=lmax[nod]=(dr-st+1)*(1-val);
        lazy[nod]=val;
        return;
    }
    if(st==dr)
        return ;
    propag(nod,st,dr);
    int mid=(st+dr)/2;
    if(L<=mid) update(2*nod, st,mid, L,R,val);
    if(mid<R) update(2*nod+1, mid+1,dr,L,R,val);
    lmax[nod]=max(ldr[2*nod]+lst[2*nod+1],max(lmax[2*nod],lmax[2*nod+1]));
    lst[nod]=lst[2*nod];
    ldr[nod]=ldr[2*nod+1];
    if(lmax[2*nod]==mid-st+1)
        lst[nod]=mid-st+1+lst[2*nod+1];
    if(lmax[2*nod+1]==dr-mid)
        ldr[nod]=ldr[2*nod]+dr-mid;
    lmax[nod]=max(lmax[nod],max(ldr[nod],lst[nod]));
    arbint[nod]=arbint[2*nod]+arbint[2*nod+1];
}

int main()
{
    int P, ans,x,y,op;
    fin>>N>>P;
    build(1,1,N);
    while(P--)
    {
        fin>>op;
        if(op<3){
            fin>>x>>y;
            update(1,1,N,x,min(x+y-1,N),(op==1)?1:0);
        }
        else {
            ans=lmax[1];
            fout<<ans<<'\n';
        }
    }

    return 0;
}