Cod sursa(job #2956034)

Utilizator PieleVoinescu David Piele Data 18 decembrie 2022 15:29:18
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.32 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define nmax 100005

using namespace std;
ifstream fin("hotel.in");
ofstream fout("hotel.out");

int n,p;
struct arbore
{
    int st,mij,dr;
} arb[4*nmax];
int lazy[4*nmax];
int cerinta;

void creez(int nod,int st,int dr,int pos)
{
    arb[nod].st=dr-st+1;
    arb[nod].mij=dr-st+1;
    arb[nod].dr=dr-st+1;
    if(st==dr)
        return;
        int mid=(st+dr)/2;
        int f_st=2*nod;
        int f_dr=2*nod+1;
        if(pos<=mid)
            creez(f_st,st,mid,pos);
        else if(pos>mid)
            creez(f_dr,mid+1,dr,pos);

}

void propag(int nod,int st,int dr)
{
    if(lazy[nod]==0)
        return;
    if(lazy[nod]==1)
    {
     arb[nod].st=dr-st+1;
     arb[nod].mij=dr-st+1;
     arb[nod].dr=dr-st+1;
    }
    else if(lazy[nod]==2)
    {
        arb[nod].st=0;
        arb[nod].dr=0;
        arb[nod].mij=0;
    }
    if(st!=dr)
    {
        lazy[2*nod]=lazy[nod];
        lazy[2*nod+1]=lazy[nod];
    }
    lazy[nod]=0;
}

void scoate(int nod,int st,int dr,int ST,int DR,int val)
{
    propag(nod,st,dr);
    if(st>=ST && dr<=DR)
    {
        lazy[nod]=val;
        return;
    }
    int mid=(st+dr)/2;
    int f_st=2*nod;
    int f_dr=2*nod+1;
    if(ST<=mid)
        scoate(f_st,st,mid,ST,DR,val);
    if(DR>mid)
        scoate(f_dr,mid+1,dr,ST,DR,val);
    propag(f_st,st,mid);
    propag(f_dr,mid+1,dr);
    arb[nod].st=arb[f_st].st;
    if(arb[f_st].st==mid-st+1)
        arb[nod].st+=arb[f_dr].st;

        arb[nod].dr=arb[f_dr].dr;
    if(arb[f_dr].dr==dr-mid)
        arb[nod].dr+=arb[f_st].dr;

    arb[nod].mij=max(max(arb[f_st].mij,arb[f_dr].mij),arb[f_st].dr+arb[f_dr].st);

}
void citire()
{
    fin>>n>>p;
    int st,dr;
    for(int i=1; i<=n; ++i)
        creez(1,1,n,i);
    while(p)
    {
        fin>>cerinta;
        if(cerinta==3)
        {
            propag(1,1,n);
            fout<<arb[1].mij<<'\n';
        }
        else if(cerinta==2)
        {
            fin>>st>>dr;
            dr=dr+st-1;
            scoate(1,1,n,st,dr,1);
        }
        else if(cerinta==1)
        {
            fin>>st>>dr;
            dr=dr+st-1;
            scoate(1,1,n,st,dr,2);

        }

        p--;
    }

}

int main()
{
    citire();
    return 0;
}