Cod sursa(job #3247552)

Utilizator Mihai_AritonMihai Ariton Mihai_Ariton Data 8 octombrie 2024 13:04:43
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;

int aint[400005], lazy[400005], st[400005], dr[400005], x[400005];
void propg(int node, int l, int r)
{
    if(lazy[node]!=-1)
    {
        if(lazy[node])
        {
            st[node]=dr[node]=x[node]=0;
            if(l!=r)
                lazy[node*2]=lazy[node*2+1]=1;
        }
        else
        {
            st[node]=dr[node]=x[node]=r-l+1;
            if(l!=r)
                lazy[node*2]=lazy[node*2+1]=0;
        }
    }
    lazy[node]=-1;
}
void update(int node, int l, int r, int ql, int qr, bool val)
{
    if(lazy[node]!=-1)
        propg(node, l, r);
    if(qr<l || r<ql)
        return;
    if(ql<=l && r<=qr)
    {
        lazy[node]=val;
        propg(node, l, r);
        return;
    }
    int mij=(l+r)/2;
    update(node*2, l, mij, ql, qr, val);
    update(node*2+1, mij+1, r, ql, qr, val);
    if(st[node*2]==mij-l+1)
        st[node]=st[node*2]+st[node*2+1];
    else
        st[node]=st[node*2];
    if(dr[node*2+1]==r-mij)
        dr[node]=dr[node*2]+dr[node*2+1];
    else
        dr[node]=dr[node*2+1];
    x[node]=max({x[node*2], x[node*2+1], st[node*2+1]+dr[node*2]});
}
signed main()
{
    ifstream cin("hotel.in");
    ofstream cout("hotel.out");

    int n, p, tip, a, b;
    cin>>n>>p;
    for(int i=0; i<=n*4+1; i++)
        lazy[i]=-1;
    update(1, 1, n, 1, n, 0);
    for(int i=1; i<=p; i++)
    {
        cin>>tip;
        if(tip==1)
        {
            cin>>a>>b;
            update(1, 1, n, a, a+b-1, 1);
        }
        else if(tip==2)
        {
            cin>>a>>b;
            update(1, 1, n, a, a+b-1, 0);
        }
        else if(tip==3)
            cout<<x[1]<<'\n';
    }

    return 0;
}