Cod sursa(job #3247551)

Utilizator iordache_Matei Iordache iordache_ Data 8 octombrie 2024 13:02:35
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
int lazy[4*MAXN];
int st[4*MAXN], dr[4*MAXN],ans[4*MAXN];
void propg(int node, int l, int r)
{
    if(lazy[node])
    {
        st[node]=dr[node]=ans[node]=0;
        if(l!=r) lazy[node*2]=lazy[node*2+1]=1;
    }
    else
    {
        st[node]=dr[node]=ans[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+1]+dr[node*2];
    else dr[node]=dr[node*2+1];
    ans[node]=max({ans[node*2],ans[node*2+1],dr[node*2]+st[node*2+1]});
    //cout<<node<<" "<<ans[node]<<"  ";
}
signed main()
{
    ifstream cin("hotel.in");
    ofstream cout("hotel.out");
    int n,q;
    cin>>n>>q;
    for(int i=0;i<=n*4+1;++i) lazy[i]=-1;
    update(1,1,n,1,n,0);
    while(q--)
    {
        int op;cin>>op;
        if(op==3) cout<<ans[1]<<'\n';
        else
        {
            int poz,sz;cin>>poz>>sz;
            update(1,1,n,poz,poz+sz-1,(op&1));
        }
    }
}