Cod sursa(job #2153158)

Utilizator dani_mocanuDani Mocanu dani_mocanu Data 5 martie 2018 23:22:25
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("hotel.in");
ofstream fout("hotel.out");
const int nmax = 400025;
int N,M;
int aint[nmax], LFT[nmax], RGT[nmax];


inline void Update(int nod, int st, int dr, int x, int y, int val)
{
    if(x > dr || y < st)
        return;
    if(x <= st && dr <= y)
    {
        aint[nod] = LFT[nod] = RGT[nod] = val * (dr - st + 1);
        return;
    }
    int mij = (st + dr) / 2;
    if(aint[nod] == 0) aint[2*nod] = aint[2*nod+1] = LFT[2*nod] = LFT[2*nod+1] = RGT[2*nod] = RGT[2*nod+1] = 0;
    if(aint[nod] == (dr - st + 1))
    {
        aint[2 * nod] = LFT[2 * nod] = RGT[2 * nod] = mij - st + 1;
        aint[2 * nod + 1] = LFT[2 * nod + 1] = RGT[2 * nod + 1] = dr - mij;
    }
    Update(2 * nod, st, mij, x, y, val);
    Update(2 * nod + 1, mij + 1, dr, x, y, val);

    LFT[nod] = LFT[2 * nod]; RGT[nod] = RGT[2 * nod + 1];
    if(LFT[2 * nod] == mij - st + 1) LFT[nod] += LFT[2 * nod + 1];
    if(RGT[2 * nod + 1] == dr - mij) RGT[nod] += RGT[2 * nod];

    aint[nod] = max(aint[2 * nod], max(aint[2 * nod + 1], RGT[2 * nod] + LFT[2 * nod + 1]));
}

int main()
{
    int i,tip,x,y;
    fin >> N >> M;
    Update(1,1,N,1,N,1);
    while(M--)
    {
        fin >> tip;
        if(tip == 3) fout << aint[1] << "\n";
        else
        {
            fin >> x >> y;
            y = x + y - 1;
            Update(1,1,N,x,y,1 - tip % 2);
        }
    }
    return 0;
}