Cod sursa(job #3362339)

Utilizator Cyb3rBoltSbora Ioan-David Cyb3rBolt Data 6 august 2026 17:25:14
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.86 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("hotel.in");
ofstream fout("hotel.out");
const int DIM = 1e5;
int n, lazy[4 * DIM + 3];

struct Iris {
    int lung; ///nr de poz acoperite de nod
    int prefix, sufix, maxLiber;
}aint[4 * DIM + 3];

inline Iris combina(Iris st, Iris dr) {
    Iris rez;
    rez.lung = st.lung + dr.lung;
    if(st.lung == st.prefix) rez.prefix = st.lung + dr.prefix;
    else rez.prefix = st.prefix;
    if(dr.lung == dr.sufix) rez.sufix = dr.lung + st.sufix;
    else rez.sufix = dr.sufix;
    rez.maxLiber = max({st.maxLiber, dr.maxLiber, st.sufix + dr.prefix});
    return rez;
}

inline void updateNod(int nod, int st, int dr) {
    if(lazy[nod] == 0) {
        ///blochez
        aint[nod] = {aint[nod].lung, 0, 0, 0};
        if(st != dr) lazy[2 * nod] = lazy[nod], lazy[2 * nod + 1] = lazy[nod];
        lazy[nod] = -1;
    }
    else if(lazy[nod] == 1) {
        ///deblochez
        aint[nod] = {aint[nod].lung, aint[nod].lung, aint[nod].lung, aint[nod].lung};
        if(st != dr) lazy[2 * nod] = lazy[nod], lazy[2 * nod + 1] = lazy[nod];
        lazy[nod] = -1;
    }
}

inline void build(int nod, int st, int dr) {
    if(st == dr) aint[nod] = {1, 1, 1, 1}, lazy[nod] = -1;
    else {
        int mid = (st + dr) / 2;
        build(2 * nod, st, mid);
        build(2 * nod + 1, mid + 1, dr);
        aint[nod] = combina(aint[2 * nod], aint[2 * nod + 1]);
        lazy[nod] = -1;
    }
}

inline void update(int nod, int st, int dr, int a, int b, bool op) {
    updateNod(nod, st, dr);
    if(a <= st && dr <= b) {
        if(op == 0) aint[nod] = {aint[nod].lung, 0, 0, 0}; ///blochez
        else aint[nod] = {aint[nod].lung, aint[nod].lung, aint[nod].lung, aint[nod].lung}; ///deblochez
        if(st != dr) lazy[2 * nod] = lazy[2 * nod + 1] = op;
    }
    else if(a > dr || b < st) return ;
    else {
        int mid = (st + dr) / 2;
        update(2 * nod, st, mid, a, b, op);
        update(2 * nod + 1, mid + 1, dr, a, b, op);
        aint[nod] = combina(aint[2 * nod], aint[2 * nod + 1]);
    }
}

inline Iris query(int nod, int st, int dr, int a, int b) {
    updateNod(nod, st, dr);
    if(a <= st && dr <= b) return aint[nod];
    else if(a > dr || b < st) return {0, 0, 0, 0};
    else {
        int mid = (st + dr) / 2;
        return combina(query(2 * nod, st, mid, a, b), query(2 * nod + 1, mid + 1, dr, a, b));
    }
}

int main()
{
    int tt; fin >> n >> tt;
    build(1, 1, n);
    while(tt--) {
        int tip; fin >> tip;
        if(tip == 1) {
            int poz, cnt; fin >> poz >> cnt;
            update(1, 1, n, poz, poz + cnt - 1, 0);
        }
        else if(tip == 2) {
            int poz, cnt; fin >> poz >> cnt;
            update(1, 1, n, poz, poz + cnt - 1, 1);
        }
        else fout << aint[1].maxLiber << '\n';
    }

    return 0;
}