Cod sursa(job #2065503)

Utilizator leraValeria lera Data 13 noiembrie 2017 20:38:48
Problema Hotel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 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 == R)
        {
            lf[nod] = val;
            rt[nod] = val;
            mj[nod] = val;
            return;

        }
        upd(nod * 2 , L, mij, l, r, val);
        upd(nod * 2 + 1, mij + 1, R, l, r, val);
        if(l <= L && R <= r)
        {
            if(val == 0)
            {
                lf[nod] = 0;
                rt[nod] = 0;
                mj[nod] = 0;
            }
            else
            {
                lf[nod] = R - L + 1;
                rt[nod] = R - L + 1;
                mj[nod] = R - L + 1;
            }
        }
        else
        {
            lf[nod] = lf[2 * nod];
            if(lf[2 * nod] == rt[2 * nod] && lf[2 * nod] != 0)lf[nod] += lf[2 * nod + 1];
            mj[nod] = 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];
        }

    }
    int main()
    {
        fin >> n >> p;
        upd(1, 1, n, 1, n, 1);
        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;
    }