Cod sursa(job #2065549)

Utilizator leraValeria lera Data 13 noiembrie 2017 21:18:28
Problema Hotel Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.21 kb
    #include <iostream>
    #include <fstream>
    #define Nmax 100005
    using namespace std;

    ifstream fin("hotel.in");
    ofstream fout("hotel.out");
    int lf[4*Nmax], rt[4*Nmax], mj[4*Nmax], n, p, op, poz, m;
    void upd(int nod, int L, int R, int l, int r,int val)// L  R - intervalul actual
    {
        int mij = (L + R)/2;
        if(L > r || R < l)return;
        if(l <= L && R <= r)
        {
            lf[nod] = rt[nod] = mj[nod] = val * ( R - L + 1);
            return;
        }
        if(mj[nod] == 0)lf[2 * nod] = rt[2 * nod] = lf[2 * nod + 1] = rt[2 * nod + 1] = mj[2 * nod] = mj[2 * nod + 1] = 0;
        if(mj[nod] == R - L + 1)
        {
            lf[2 * nod] = rt[2 * nod] = mj[2 * nod] = mij - L + 1;
            lf[2 * nod + 1] = rt[2 * nod + 1] = mj[2 * nod + 1] = R - mij;
        }
        upd(nod * 2 , L, mij, l, r, val);
        upd(nod * 2 + 1, mij + 1, R, l, r, val);
        lf[nod] = lf[2 * nod];
        if(lf[2 * nod] == rt[2 * nod] && lf[2 * nod] != 0)lf[nod] += lf[2 * nod + 1];
        mj[nod] = max(max(mj[2 * nod], mj[2 * nod + 1]), rt[2 * nod] + lf[2 * nod + 1]);
        rt[nod] = rt[2 * nod + 1];
        if(rt[2 * nod + 1] == lf[2 * nod + 1]&& rt[2 * nod + 1] != 0)rt[nod] += rt[2 * nod];
    }
    void build(int nod, int L, int R)
    {
        lf[nod] = rt[nod] = mj[nod] = R - L  +1;
        int mij = (L + R)/2;
        if(L == R)return;
        build(2 * nod, L , mij);
        build(2 * nod + 1, mij + 1, R);
    }
    int main()
    {
        fin >> n >> p;
        build(1, 1, n);
        for(int i = 1; i <= p; i++)
        {
            fin >> op;
            if(op == 1)
            {
                fin >> poz >> m;
                upd(1, 1, n, poz, poz + m - 1, 0);

            }
            else if(op == 2)
            {
                fin >> poz >> m;
                upd(1, 1, n, poz, poz + m - 1, 1);
            }
            else
            {
                fout << mj[1] << '\n';
            }
            //fout << i  <<" : \n" ;
            //for(int j = 1; j <= 4 * n; j++)fout << j << ": " << lf[j] << " " << mj[j] <<" " << rt[j] << '\n';

            //fout << '\n';
        }
        return 0;
    }