Cod sursa(job #2146133)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 27 februarie 2018 20:17:08
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <fstream>

using namespace std;

ifstream f("hotel.in");
ofstream g("hotel.out");

const int nmax = 100005;
struct arb{
    int zero, lft, rgt;
}arb[4*nmax];
int zero[4*nmax], zero_lft[4*nmax], zero_rgt[4*nmax], n, p, gx, gy, m, t, cant, i;

void update(int cod, int st, int dr) {
    if (gx <= st && dr <= gy) {
        int diff = (dr-st+1)*cant;
        arb[cod] = {diff,diff,diff};
        return;
    }
    int mij = (st+dr)/2;
    if (arb[cod].zero == dr-st+1) {
        int diff = mij-st+1;
        arb[2*cod] = {diff,diff,diff};
        diff = dr-mij;
        arb[2*cod+1] = {diff,diff,diff};
    }
    else if (arb[cod].zero == 0) arb[2*cod] = arb[2*cod+1] = {0,0,0};

    if (gx <= mij) update(2*cod, st, mij);
    if (mij <  gy) update(2*cod+1, mij+1, dr);

    arb[cod].rgt = arb[2*cod].rgt;
    if (arb[2*cod].rgt == (mij-st+1))
        arb[cod].rgt += arb[2*cod+1].rgt;
    arb[cod].lft = arb[2*cod+1].lft;
    if (arb[2*cod+1].lft == (dr-mij))
        arb[cod].lft += arb[2*cod].lft;
    arb[cod].zero = max( max(arb[2*cod].zero, arb[2*cod+1].zero), arb[2*cod].lft+arb[2*cod+1].rgt);
}

int main() {
    f >> n >> p;
    for (i = 1; i <= n; i++) {
        gx=gy=i;
        cant = 1;
        update(1,1,n);
    }
    while (p--) {
        f >> t;
        if (t == 3) {
            g << arb[1].zero << '\n';
        } else {
            f >> gx >> m;
            gy = gx+m-1;
            cant = t-1;
            update(1,1,n);
        }
    }
    return 0;
}