Cod sursa(job #1237338)

Utilizator sebinechitasebi nechita sebinechita Data 3 octombrie 2014 20:59:42
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("hotel.in");
ofstream fout("hotel.out");
#define MAX 100002

struct sebi{
    int st, dr, rez;
};

sebi a[4*MAX];
int l, r, val, n;

void update(int nod, int st, int dr)
{
    if(l<=st && dr<=r)
    {
        if(val==0)
            a[nod].st=a[nod].dr=a[nod].rez=0;
        else
            a[nod].st=a[nod].dr=a[nod].rez=dr-st+1;
        return;
    }
    int mij=(st+dr)>>1;
    if(a[nod].rez==0)
    {
        a[2*nod].rez=a[2*nod].st=a[2*nod].dr=0;
        a[2*nod+1].rez=a[2*nod+1].st=a[2*nod+1].dr=0;
    }
    if(a[nod].rez==dr-st+1)
    {
        a[2*nod].rez=a[2*nod].st=a[2*nod].dr=mij-st+1;
        a[2*nod+1].rez=a[2*nod+1].st=a[2*nod+1].dr=dr-(mij+1)+1;
    }
    if(mij+1<=r)
    {
        update(2*nod+1, mij+1, dr);
    }
    if(l<=mij)
    {
        update(2*nod, st, mij);
    }
    if(a[2*nod].st==mij-st+1)
        a[nod].st=a[2*nod].st+a[2*nod+1].st;
    else
        a[nod].st=a[2*nod].st;
    if(a[2*nod+1].dr==dr-(mij+1)+1)
        a[nod].dr=a[2*nod+1].dr+a[2*nod].dr;
    else
        a[nod].dr=a[2*nod+1].dr;
    a[nod].rez=max(a[2*nod].rez, max(a[2*nod+1].rez, a[2*nod].dr+a[2*nod+1].st));
}

void U(int st, int dr, int v)
{
    l=st;
    r=dr;
    val=v;
    update(1, 1, n);
}

int main()
{
    int m, i, t, x, y;
    fin>>n>>m;
    U(1, n, 1);
    while(m--)
    {
        fin>>t;
        if(t==3)
        {
            fout<<a[1].rez<<"\n";
        }
        if(t==1)
        {
            fin>>x>>y;
            U(x, x+y-1, 0);
        }
        if(t==2)
        {
            fin>>x>>y;
            U(x, x+y-1, 1);
        }
    }
    return 0;
}