Cod sursa(job #3132524)

Utilizator NeganAlex Mihalcea Negan Data 22 mai 2023 22:24:27
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.23 kb
#include<bits/stdc++.h>

using namespace std;
const int NMAX = 100005;

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

int n, t;
int A[4 * NMAX];
int B[4 * NMAX];
int C[4 * NMAX];
int SUM[4 * NMAX];
int lazy[4 * NMAX];
int a[NMAX];

int Leftson(int node)
{
    return 2 * node;
}
int Rightson(int node)
{
    return 2 * node + 1;
}
void Build(int node, int left, int right)
{
    if(left == right)
    {
        A[node] = a[left];
        B[node] = a[left];
        C[node] = a[left];
        SUM[node] = a[left];
        return;
    }
    int mid = (left + right) / 2;
    Build(Leftson(node), left, mid);
    Build(Rightson(node), mid + 1, right);
    A[node] = A[Rightson(node)];
    if(B[Rightson(node)] == SUM[Rightson(node)])
       A[node] = max(A[node], A[Leftson(node)] + B[Rightson(node)]);
    B[node] = B[Leftson(node)];
    if(A[Leftson(node)] == SUM[Leftson(node)])
       B[node] = max(B[node], A[Leftson(node)] + B[Rightson(node)]);
    C[node] = max(max(C[Leftson(node)], C[Rightson(node)]), A[Leftson(node)] + B[Rightson(node)]);
    SUM[node] = SUM[Leftson(node)] + SUM[Rightson(node)];

}

void Propag(int node, int left, int right)
{
    if(lazy[node] == -1)
    {
        A[node] = 0;
        B[node] = 0;
        C[node] = 0;
        if(left != right)
        {
            lazy[Leftson(node)] = -1;
            lazy[Rightson(node)] = -1;
        }
        lazy[node] = 0;
    }
    else if(lazy[node] == 1)
    {
        A[node] = SUM[node];
        B[node] = SUM[node];
        C[node] = SUM[node];
        if(left != right)
        {
            lazy[Leftson(node)] = 1;
            lazy[Rightson(node)] = 1;
        }
        lazy[node] = 0;
    }
}
void Update(int node, int left, int right, int leftup, int rightup, int val)
{

    if(left > right || right < leftup || left > rightup)
        return;
    if(leftup <= left && right <= rightup)
    {
        lazy[node] = val;
        return;
    }
    Propag(node, left, right);
    int mid = (left + right) / 2;
    Update(Leftson(node), left, mid, leftup, rightup, val);
    Update(Rightson(node), mid + 1, right, leftup, rightup, val);
    Propag(Leftson(node), left, mid);
    Propag(Rightson(node), mid + 1, right);
    A[node] = A[Rightson(node)];
    if(B[Rightson(node)] == SUM[Rightson(node)])
       A[node] = max(A[node], A[Leftson(node)] + B[Rightson(node)]);
    B[node] = B[Leftson(node)];
    if(A[Leftson(node)] == SUM[Leftson(node)])
       B[node] = max(B[node], A[Leftson(node)] + B[Rightson(node)]);
    C[node] = max(max(C[Leftson(node)], C[Rightson(node)]), A[Leftson(node)] + B[Rightson(node)]);
}
void Citire()
{
    fin >> n >> t;
    int i;
    for(i = 1;i <= n;i++)
        a[i] = 1;
    Build(1, 1, n);
    cout << C[1];
    while(t--)
    {
        int a, b, op;
        fin >> op;
        if(op == 1)
        {
            fin >> a >> b;
            Update(1, 1, n, a, a + b - 1, -1);
        }
        else if(op == 2)
        {
            fin >> a >> b;
            Update(1, 1, n, a, a + b - 1, 1);
        }
        else
        {
            Propag(1, 1, n);
            fout << C[1] << "\n";
        }
    }
}
int main()
{
    Citire();
    return 0;
}