Cod sursa(job #3159913)

Utilizator TeodorVTeodorV TeodorV Data 22 octombrie 2023 14:47:45
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.04 kb
#include <bits/stdc++.h>

using namespace std;

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


int n;

class AINT
{
private:
    static const int NAint=1<<19;
    struct nod
    {
        int lmax=0,prefix=0,sufix=0,l=0;
    };
    void combinNod(nod& out, const nod& st, const nod& dr)
    {
        out.l=st.l+dr.l;
        out.prefix=st.prefix+(st.prefix==st.l)*dr.prefix;
        out.sufix=dr.sufix+(dr.sufix==dr.l)*st.sufix;

        out.lmax=max(st.lmax, dr.lmax);
        out.lmax=max(out.lmax, st.sufix+dr.prefix);
    }
public:
    void build(int node=0, int st=1, int dr=n)
    {
        data[node].l=data[node].lmax=data[node].prefix=data[node].sufix=(dr-st+1);
        lazy[node]=-1;
        if(st==dr)
            return;
        int mij=(st+dr)/2;
        build(2*node+1, st, mij);
        build(2*node+2, mij+1, dr);
    }
    void update(const int& qst, const int& qdr, const bool& newVal, int node=0, int st=1, int dr=n)
    {
        if(lazy[node]!=-1)
        {
            data[node].lmax=data[node].prefix=data[node].sufix=(dr-st+1)*lazy[node];
            if(st!=dr)
                lazy[2*node+1]=lazy[2*node+2]=lazy[node];
            lazy[node]=-1;
        }
        if(dr<qst || qdr<st)
            return;
        if(qst<=st && dr<=qdr)
        {
            data[node].lmax=data[node].prefix=data[node].sufix=(dr-st+1)*newVal;
            if(st!=dr)
                lazy[2*node+1]=lazy[2*node+2]=newVal;
            return;
        }
        int mij=(st+dr)/2;
        update(qst, qdr, newVal, 2*node+1, st, mij);
        update(qst, qdr, newVal, 2*node+2, mij+1, dr);
        combinNod(data[node], data[2*node+1], data[2*node+2]);
    }
    int query()
    {
        //refresh();
        return data[0].lmax;
    }
    void afis(int node=0, int st=1, int dr=n)
    {
        if(st==dr)
        {
            cout<<!data[node].lmax<<' ';
            if(st==n)
                cout<<'\n';
            return;
        }
        int mij=(st+dr)/2;
        afis(2*node+1, st, mij);
        afis(2*node+2, mij+1, dr);
    }
    void refresh(int node=0, int st=1, int dr=n)
    {
        if(lazy[node]!=-1)
        {
            data[node].lmax=data[node].prefix=data[node].sufix=(dr-st+1)*lazy[node];
            if(st!=dr)
                lazy[2*node+1]=lazy[2*node+2]=lazy[node];
            lazy[node]=-1;
        }
        if(st==dr)
            return;
        int mij=(st+dr)/2;
        refresh(2*node+1, st, mij);
        refresh(2*node+2, mij+1, dr);
    }
private:
    nod data[NAint];
    int lazy[NAint];
};

AINT aint;

int main()
{
    int m;
    fin>>n>>m;
    aint.build();
    //aint.afis();
    for(int q=1; q<=m; q++)
    {
        int C;
        fin>>C;
        if(C==3)
        {
            fout<<aint.query()<<'\n';
        }
        else
        {
            int a,b;
            fin>>a>>b; b=a+b-1;
            bool newVal=(C==2);
            aint.update(a, b, newVal);
            //aint.afis();
        }
    }
    return 0;
}