Cod sursa(job #2739002)

Utilizator AlexandruGabrielAliciuc Alexandru AlexandruGabriel Data 6 aprilie 2021 17:39:48
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.53 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 100005;
int n, m;
struct tree
{
    int maxEmpty;
    int emptyLeft;
    int emptyRight;
    int add;
};
tree aint[4 * N];

inline int maxim(int v1, int v2, int v3)
{
    return max(v1, max(v2, v3));
}

void Update(int node, int left, int right, int qLeft, int qRight, int type)
{
    if (aint[node].add != 0)
    {
        if (aint[node].add == 1)
            aint[node].maxEmpty = aint[node].emptyLeft = aint[node].emptyRight = 0;
        else if (aint[node].add == -1)
            aint[node].maxEmpty = aint[node].emptyLeft = aint[node].emptyRight = right - left + 1;
        
        if (left != right)
            aint[2 * node].add = aint[2 * node + 1].add = aint[node].add;
        aint[node].add = 0;
    }

    if (left > qRight || right < qLeft)
        return;

    if (left >= qLeft && right <= qRight)
    {
        if (type == 1) //adauga
            aint[node].maxEmpty = aint[node].emptyLeft = aint[node].emptyRight = 0;
        else if (type == -1) //elimina
            aint[node].maxEmpty = aint[node].emptyLeft = aint[node].emptyRight = right - left + 1;

        if (left != right)
            aint[2 * node].add = aint[2 * node + 1].add = type;
        return;
    }

    int mid = (left + right) / 2;
    Update(2 * node, left, mid, qLeft, qRight, type);
    Update(2 * node + 1, mid + 1, right, qLeft, qRight, type);

    aint[node].maxEmpty = maxim(aint[2 * node].maxEmpty, aint[2 * node + 1].maxEmpty, aint[2 * node].emptyRight + aint[2 * node + 1].emptyLeft);
    aint[node].emptyLeft = aint[2 * node].emptyLeft;
    if (aint[2 * node].emptyLeft == mid - left + 1)
        aint[node].emptyLeft += aint[2 * node + 1].emptyLeft;
    aint[node].emptyRight = aint[2 * node + 1].emptyRight;
    if (aint[2 * node + 1].emptyRight == right - mid)
        aint[node].emptyRight += aint[2 * node].emptyRight;
    
}

int main()
{
    fin >> n >> m;
    Update(1, 1, n, 1, n, -1);

    while (m--)
    {
        int op;
        fin >> op;
        if (op == 1)
        {
            int pos, count;
            fin >> pos >> count;

            Update(1, 1, n, pos, pos + count - 1, 1);
        }
        else if (op == 2)
        {
            int pos, count;
            fin >> pos >> count;

            Update(1, 1, n, pos, pos + count - 1, -1);
        }
        else
        {
            fout << aint[1].maxEmpty << "\n";
        }
    }
    return 0;
}