Cod sursa(job #3201843)

Utilizator vvvvvvvvvvvvvVusc David vvvvvvvvvvvvv Data 9 februarie 2024 20:46:41
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <fstream>

using namespace std;

struct nod{
    int max, st, dr;
};

ifstream fin("hotel.in");
ofstream fout("hotel.out");
const int NMAX = 100001, INF = 1e9;
int n, v[NMAX], lai[4 * NMAX], q, sol;
nod ai[4 * NMAX];

void update_lazy(int nod, int st, int dr){
    if(lai[nod]){
        if(lai[nod] == 1){
            ai[nod] = {0, 0, 0};
            if(st != dr)
                lai[2 * nod] = lai[2 * nod + 1] = 1;
        }
        else{
            int l = dr - st + 1;
            ai[nod] = {l, l, l};
            if(st != dr)
                lai[2 * nod] = lai[2 * nod + 1] = 2;
        }
        lai[nod] = 0;
    }
}

void update(int nod, int st, int dr, int a, int b, int val){

    if(st >= a && dr <= b){
        lai[nod] = val;
        return;
    }

    update_lazy(nod, st, dr);

    int mij = (st + dr) / 2;
    if(a <= mij) update(nod * 2, st, mij, a, b, val);
    if(b > mij) update(nod * 2 + 1, mij + 1, dr, a, b, val);

    update_lazy(2 * nod, st, mij);
    update_lazy(2 * nod + 1, mij + 1, dr);

    if(ai[2 * nod].st == mij - st + 1) ai[nod].st = ai[2 * nod].st + ai[2 * nod + 1].st;
    else ai[nod].st = ai[2 * nod].st;

    if(ai[2 * nod + 1].dr == dr - mij) ai[nod].dr = ai[2 * nod].dr + ai[2 * nod + 1].dr;
    else ai[nod].dr = ai[2 * nod + 1].dr;

    ai[nod].max = max(ai[2 * nod].dr + ai[2 * nod + 1].st, max(ai[2 * nod].max, ai[2 * nod + 1].max));
}

int main()
{
    fin >> n >> q;
    lai[1] = 2;
    for(int i = 1; i <= q; i++){
        int c, x, y;
        fin >> c;
        if(c == 3) update_lazy(1, 1, n), fout << ai[1].max << '\n';
        else if(c == 1){
            fin >> x >> y;
            update(1, 1, n, x, x + y - 1, 1);
        }
        else{
            fin >> x >> y;
            update(1, 1, n, x, x + y - 1, 2);
        }
    }
    fin.close();
    fout.close();
    return 0;
}