Cod sursa(job #2740955)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 14 aprilie 2021 21:06:55
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.44 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define MOD 1000000007
using namespace std;

const int INF = (1 << 30), NMAX(100005);
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;
using Point = array<int, 2>;

void BUNA(const string& task = "")
{
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
                freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PA()
{
    exit(0);
}

struct arbore
{
    int rez, suf, pref, sum;
} Arb[4 * NMAX + 5];

int lazy[4 * NMAX + 5];
arbore uneste(arbore a, arbore b, int st1, int dr1, int st2, int dr2)
{
    arbore rez;
    rez.sum = a.sum + b.sum;
    rez.rez = max(a.rez, max(b.rez, a.suf + b.pref));
    rez.pref = a.pref;
    rez.suf = b.suf;
    if(a.sum == dr1 - st1 + 1)
        rez.pref = max(rez.pref, a.sum + b.pref);
    if(b.sum == dr2 - st2 + 1)
        rez.suf = max(rez.suf, a.suf + b.sum);
    return rez;
}
void propag(int nod, int st, int dr)
{
    if(lazy[nod] != -1)
    {
        if(lazy[nod] == 0)
            Arb[nod].pref = Arb[nod].suf = Arb[nod].sum = Arb[nod].rez = 0;
        else if(lazy[nod] == 1)
            Arb[nod].pref = Arb[nod].suf = Arb[nod].sum = Arb[nod].rez = dr - st + 1;
        if(st != dr)
        lazy[2 * nod] = lazy[nod], lazy[2 * nod + 1] = lazy[nod];
        lazy[nod] = -1;
    }
}
void update(int nod, int st, int dr, int a, int b, int tip)
{
    if(a <= st && dr <= b)
    {
        lazy[nod] = tip;
        propag(nod, st, dr);
        return;
    }
    int mij = (st + dr) / 2;
    propag(2 * nod, st, mij);
    propag(2 * nod + 1, mij + 1, dr);
    if(a <= mij)
        update(2 * nod, st, mij, a, b, tip);
    if(mij < b)
        update(2 * nod + 1, mij + 1, dr, a, b, tip);
    Arb[nod] = uneste(Arb[2 * nod], Arb[2 * nod + 1], st, mij, mij + 1, dr);
}

int main()
{
    BUNA("hotel");
    int n, m;
    cin >> n >> m;

    for(int i = 1; i <= 4 * NMAX; ++i)
        lazy[i] = -1;
    update(1, 1, n, 1, n, 1);
    for(int i = 1; i <= m; ++i){
        int c;
        cin >> c;

        if(c == 3)
            cout << Arb[1].rez << '\n';
        else {
            int st, m;
            cin >> st >> m;

            if(c == 1)
                update(1, 1, n, st, st + m - 1, 0);
            else update(1, 1, n, st, st + m - 1, 1);
        }
    }
    PA();
}