Cod sursa(job #2954017)

Utilizator Giulian617Buzatu Giulian Giulian617 Data 12 decembrie 2022 23:58:05
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.06 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("hotel.in");
ofstream g("hotel.out");
const int NMAX=100005;
int n,Q,op,add,start,finish,val;
int best[4*NMAX],max_left[4*NMAX],max_right[4*NMAX],lazy[4*NMAX];
///best represents the longest sequence of ones
///max_left represents the longest prefix of ones
///max_right represents the longest suffix of ones
///lazy represents the value on our nodes,hence we start with all elements to 1 and invert the logic of the operations
void update(int node,int left,int right)
{
    ///first we propagate
    if(lazy[node]==-1)///we set the whole segment to 0
    {
        lazy[node]=0;
        lazy[2*node]=-1;
        lazy[2*node+1]=-1;
    }
    else if(lazy[node]==1)///we set the whole segment to 1
    {
        lazy[2*node]=1;
        lazy[2*node+1]=1;
    }
    if(val==-1)
        lazy[node]=0;
    if(start<=left && right<=finish)
    {
        lazy[node]=val;
        return;
    }
    int middle=(left+right)/2;
    if(start<=middle)
        update(2*node,left,middle);///update left son
    if(middle<finish)
        update(2*node+1,middle+1,right);///update right son
}
void query(int node,int left,int right)
{
    ///first we propagate
    if(lazy[node]==1)///we "set" the whole segment to 1
    {
        best[node]=right-left+1;
        max_left[node]=right-left+1;
        max_right[node]=right-left+1;
        return;
    }
    else if(lazy[node]==-1)///we "set" the whole segment to 0
    {
        best[node]=0;
        max_left[node]=0;
        max_right[node]=0;
        return;
    }
    if(left==right)
        return;
    int middle=(left+right)/2;
    query(2*node,left,middle);///query left son
    query(2*node+1,middle+1,right);///query right son

    max_left[node]=max_left[2*node];
    if(max_left[2*node]==middle-left+1)///the max_left for the left son is the full segment
        max_left[node]+=max_left[2*node+1];///so we can add to it the max_left for the right son

    max_right[node]=max_right[2*node+1];
    if(max_right[2*node+1]==right-(middle+1)+1)///the max_right for the right son is the full segment
        max_right[node]+=max_right[2*node];///so we can add to it the max_right for the left son

    best[node]=max({best[2*node],best[2*node+1],max_right[2*node]+max_left[2*node+1]});///the new best is either the best on the left son, the best on the right son
    ///or a combination of max_right of the left son and max_left of the right son
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    f>>n>>Q;
    for(int i=1; i<=4*n; i++)
        lazy[i]=1;
    for(int q=1; q<=Q; q++)
    {
        f>>op;
        if(op==1)
        {
            f>>start>>add;
            finish=start+add-1;
            val=-1;
            update(1,1,n);
        }
        else if(op==2)
        {
            f>>start>>add;
            finish=start+add-1;
            val=1;
            update(1,1,n);
        }
        else
        {
            query(1,1,n);
            g<<best[1]<<'\n';
        }
    }
    return 0;
}