Cod sursa(job #2083667)

Utilizator amaliarebAmalia Rebegea amaliareb Data 7 decembrie 2017 22:52:18
Problema Hotel Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.52 kb
#include <iostream>
#include <fstream>
#include <set>
#define iter set<pair<int, int> >::iterator
#define mp make_pair

using namespace std;
ifstream f("hotel.in");
ofstream g("hotel.out");
int n, m;
set< pair<int, int> > s;
multiset<int> ans;

inline iter mergge(iter it)
{
    int st, dr;
    st = it->first;
    ++it;
    dr = it->second;
    auto aux = it;
    --aux;
    s.erase(aux);
    s.erase(it);
    auto ret = s.insert(mp(st, dr));
    return ret.first;
}

inline void upd1()
{
    int st, dr, xst, xdr;
    f >> st >> dr;
    dr = st + dr - 1;
    iter it = s.lower_bound(mp(st, dr));
    if (it == s.end() || it->first > st) --it;
    xst = it->first;
    xdr = it->second;
    auto aux = it;
    ++it;
    s.erase(aux);
    ans.erase(ans.find(xdr - xst + 1));
    aux = s.insert(mp(st, dr)).first;
    ans.insert(xdr - dr);
    ans.insert(st - xst);
    if (xst == st && xst > 1)
    {
        auto idfk = s.lower_bound(mp(st, 0));
        --idfk;
        aux = mergge(idfk);
        ans.erase(ans.find(st - xst));
    }
    else if(st > 1) s.insert(mp(xst, st - 1));
    if (xdr == dr && dr < n)
    {
        aux = mergge(aux);
        ans.erase(ans.find(xdr - dr));
    }
    else if(dr < n) s.insert(mp(dr + 1, xdr));
    it = s.begin();
}

inline void upd2()
{
    int st, dr, xst, xdr;
    f >> st >> dr;
    dr = st + dr - 1;
    iter it = s.lower_bound(mp(st, dr));
    if (it == s.end() || it->first > st) --it;
    xst = it->first;
    xdr = it->second;
    auto aux = it;
    ++it;
    s.erase(aux);
    ans.insert(dr - st + 1);
    aux = s.insert(mp(st, dr)).first;
    if (xst == st && xst > 1)
    {
        auto idfk = s.lower_bound(mp(st, 0));
        --idfk;
        ans.erase(ans.find(idfk->second - idfk->first + 1));
        aux = mergge(idfk);
        ans.erase(ans.find(dr - st + 1));
        ans.insert(aux->second - aux->first + 1);
    }
    else if(st > 1) s.insert(mp(xst, st - 1));
    if (xdr == dr && dr < n)
    {
        ans.erase(ans.find(aux->second - aux->first + 1));
        aux = mergge(aux);
        ans.insert(aux->second - aux->first + 1);
    }
    else if(dr < n) s.insert(mp(dr + 1, xdr));
    it = s.begin();
}

int main()
{
    f >> n >> m;
    s.insert(mp(1, n));
    ans.insert(n);
    for (int i = 1; i <= m; i++)
    {
        int tip;
        f >> tip;auto it = ans.end();
        --it;
        if (tip == 1) upd1();
        else if (tip == 2) upd2();

        else g << *it << '\n';
    }
    return 0;
}