Cod sursa(job #2767694)

Utilizator cezar_titianuTitianu Cezar cezar_titianu Data 7 august 2021 14:07:03
Problema Hotel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.27 kb
#include <fstream>
#include <bitset>

struct interval {
    int cleft;
    int cmax;
    int cright;
};

struct lazymod {

};

interval aint[400000];
std::bitset <400000> lazy;
std::bitset <400000> set;

void prop(int left, int right, int nod) {
    if (lazy[nod]) {
        lazy[nod] = false;
        if (left != right) {
            lazy[2 * (long long int)nod] = true;
            lazy[2 * (long long int)nod + 1] = true;
            set[2 * (long long int)nod] = set[nod];
            set[2 * (long long int)nod + 1] = set[nod];
            if (set[nod]) {
                aint[2 * nod].cleft = 0;
                aint[2 * nod].cmax = 0;
                aint[2 * nod].cright = 0;
                aint[2 * nod + 1].cleft = 0;
                aint[2 * nod + 1].cmax = 0;
                aint[2 * nod + 1].cright = 0;
            }
            else {
                int mid = (left + right) / 2;
                aint[2 * nod].cleft = mid - left + 1;
                aint[2 * nod].cmax = aint[2 * nod].cleft;
                aint[2 * nod].cright = aint[2 * nod].cleft;
                aint[2 * nod + 1].cleft = right - mid;
                aint[2 * nod + 1].cmax = aint[2 * nod + 1].cleft;
                aint[2 * nod + 1].cright = aint[2 * nod + 1].cleft;
            }
        }
    }
}

void update(int left, int right, int nod, int ileft, int iright, bool mod) {
    if (left == ileft && right == iright) {
        lazy[nod] = true;
        set[nod] = mod;
        if (mod) {
            aint[nod].cleft = 0;
            aint[nod].cmax = 0;
            aint[nod].cright = 0;
        }
        else {
            aint[nod].cleft = right - left + 1;
            aint[nod].cmax = aint[nod].cleft;
            aint[nod].cright = aint[nod].cleft;
        }
    }
    else {
        int mid = (left + right) / 2;
        prop(left, right, nod);
        if (ileft <= mid) {
            update(left, mid, 2 * nod, ileft, std::min(iright, mid), mod);
        }
        if (mid < iright) {
            update(mid + 1, right, 2 * nod + 1, std::max(ileft, mid + 1), iright, mod);
        }
        if (aint[2 * nod].cleft == mid - left + 1) {
            aint[nod].cleft = mid - left + 1 + aint[2 * nod + 1].cleft;
        }
        else {
            aint[nod].cleft = aint[2 * nod].cleft;
        }
        aint[nod].cmax = std::max(aint[2 * nod].cright + aint[2 * nod + 1].cleft, std::max(aint[2 * nod].cmax, aint[2 * nod + 1].cmax));
        if (aint[2 * nod + 1].cright == right - mid) {
            aint[nod].cright = right - mid + aint[2 * nod].cright;
        }
        else {
            aint[nod].cright = aint[2 * nod + 1].cright;
        }
    }
}

int main() {
    std::ifstream fin("hotel.in");
    std::ofstream fout("hotel.out");
    int nrn, nrm, cer, first, size;
    fin >> nrn >> nrm;
    update(1, nrn, 1, 1, nrn, 0);
    for (int index = 0; index < nrm; index++) {
        fin >> cer;
        if (cer == 1) {
            fin >> first >> size;
            update(1, nrn, 1, first, first + size - 1, 1);
        }
        if (cer == 2) {
            fin >> first >> size;
            update(1, nrn, 1, first, first + size - 1, 0);
        }
        if (cer == 3) {
            fout << aint[1].cmax << '\n';
        }
    }
}