Cod sursa(job #1113529)

Utilizator SpiriFlaviuBerbecariu Flaviu SpiriFlaviu Data 20 februarie 2014 17:59:32
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <fstream>
#include <algorithm>
#define l first.first
#define r first.second
#define t second
using namespace std;

ifstream fin("hotel.in");
ofstream fout("hotel.out");

typedef pair<pair<int, int>,int> Node;

Node arb[4*100001];

void update(int nod, int st, int dr, int lq, int rq, int val)
{
    int mij = (st+dr)/2;

    if(lq <= st && dr <= rq)
    {
        arb[nod].t = arb[nod].r = arb[nod].l = (!val)*(dr-st+1);
        return;
    }
    if(arb[nod].t == 0)
    {
        arb[2*nod].t = arb[2*nod].r = arb[2*nod].l = 0;
        arb[2*nod+1].t = arb[2*nod+1].r = arb[2*nod+1].l = 0;
    }
    if(arb[nod].t == dr - st + 1)
    {
        arb[2*nod].t = arb[2*nod].r = arb[2*nod].l = mij - st + 1;
        arb[2*nod+1].t = arb[2*nod+1].r = arb[2*nod+1].l = dr - mij;
    }
    if(lq <= mij)
        update(2*nod,st,mij,lq,rq,val);
    if(rq >= mij+1)
        update(2*nod+1,mij+1,dr,lq,rq,val);


    arb[nod].t = max(arb[2*nod].t,arb[2*nod+1].t);
    arb[nod].t = max(arb[nod].t,arb[2*nod].r + arb[2*nod+1].l);
    if(mij-st+1 == arb[2*nod].t)
        arb[nod].l = arb[2*nod].t + arb[2*nod+1].l;
    else arb[nod].l = arb[2*nod].l;
    if(arb[2*nod+1].t == dr-mij)
        arb[nod].r = arb[2*nod+1].t + arb[2*nod].r;
    else arb[nod].r = arb[2*nod+1].r;


}

int main()
{
    int n,m,x,y,op;
    fin>>n>>m;
    update(1,1,n,1,n,0);
    for(int i=1;i<=m;i++)
    {
        fin>>op;
        if(op == 3)
            fout<<arb[1].t<<'\n';
        else
        {
            fin>>x>>y;
            update(1,1,n,x,x+y-1,!(op-1));
        }
    }
    fin.close();
    fout.close();
    return 0;
}