Cod sursa(job #3289777)

Utilizator IleaIlea Bogdan Ilea Data 28 martie 2025 14:42:27
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.11 kb
#include <iostream>
using namespace std;

#define NMAX 100001
#define def int nod=1, int st=1, int dr=n

struct tree {
    int mx, pre, suf, ocp, lz = -1, sz;
} aint[NMAX*4];

int n, q, x, y;

void push(def) {
    if (aint[nod].lz == -1) return;

    if (aint[nod].lz == 0) { // free rooms
        aint[nod].mx = aint[nod].pre = aint[nod].suf = aint[nod].sz;
        aint[nod].ocp = 0;
    } else { // occupied rooms
        aint[nod].mx = aint[nod].pre = aint[nod].suf = 0;
        aint[nod].ocp = aint[nod].sz;
    }

    if (st != dr) {
        aint[2*nod].lz = aint[nod].lz;
        aint[2*nod+1].lz = aint[nod].lz;
    }
    aint[nod].lz = -1;
}

void add(def) { // occupy rooms
    push(nod, st, dr);
    if (y < st || dr < x) return;
    if (x <= st && dr <= y) {
        aint[nod].lz = 1;
        push(nod, st, dr);
        return;
    }
    int mij = (st + dr) / 2;
    add(2*nod, st, mij);
    add(2*nod+1, mij+1, dr);

    aint[nod].ocp = aint[2*nod].ocp + aint[2*nod+1].ocp;
    aint[nod].mx = max(aint[2*nod].mx, aint[2*nod+1].mx);
    aint[nod].mx = max(aint[nod].mx, aint[2*nod].suf + aint[2*nod+1].pre);
    aint[nod].pre = aint[2*nod].pre;
    if (aint[2*nod].ocp == 0) aint[nod].pre += aint[2*nod+1].pre;
    aint[nod].suf = aint[2*nod+1].suf;
    if (aint[2*nod+1].ocp == 0) aint[nod].suf += aint[2*nod].suf;
}

void del(def) { // free rooms
    push(nod, st, dr);
    if (y < st || dr < x) return;
    if (x <= st && dr <= y) {
        aint[nod].lz = 0;
        push(nod, st, dr);
        return;
    }
    int mij = (st + dr) / 2;
    del(2*nod, st, mij);
    del(2*nod+1, mij+1, dr);

    aint[nod].ocp = aint[2*nod].ocp + aint[2*nod+1].ocp;
    aint[nod].mx = max(aint[2*nod].mx, aint[2*nod+1].mx);
    aint[nod].mx = max(aint[nod].mx, aint[2*nod].suf + aint[2*nod+1].pre);
    aint[nod].pre = aint[2*nod].pre;
    if (aint[2*nod].ocp == 0) aint[nod].pre += aint[2*nod+1].pre;
    aint[nod].suf = aint[2*nod+1].suf;
    if (aint[2*nod+1].ocp == 0) aint[nod].suf += aint[2*nod].suf;
}

void init(def) {
    aint[nod].sz = dr - st + 1;
    if (st == dr) {
        aint[nod] = {1, 1, 1, 0, -1, 1};
        return;
    }
    int mij = (st + dr) / 2;
    init(2*nod, st, mij);
    init(2*nod+1, mij+1, dr);

    aint[nod].mx = aint[nod].pre = aint[nod].suf = aint[nod].sz;
    aint[nod].ocp = 0;
    aint[nod].lz = -1;
}

void solve(void) {
    cin >> n >> q;
    init();
    while (q--) {
        int op, i, m;
        cin >> op;
        if (op == 1) {
            cin >> i >> m;
            x = i, y = i + m - 1;
            add();
        } else if (op == 2) {
            cin >> i >> m;
            x = i, y = i + m - 1;
            del();
        } else {
            cout << aint[1].mx << "\n";
        }
    }
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifdef LOCAL
        freopen("date.in", "r", stdin);
        freopen("log.txt", "w", stdout);
    #else
        freopen("hotel.in", "r", stdin);
        freopen("hotel.out", "w", stdout);
    #endif
    solve();
    return 0;
}