Cod sursa(job #1237087)

Utilizator sebinechitasebi nechita sebinechita Data 3 octombrie 2014 08:48:14
Problema Hotel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.96 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;
        }
    }
    int mij=(st+dr)>>1;
    if(l<=mij)
    {
        if(a[nod].rez==0)
        {
            a[2*nod].rez=a[2*nod].st=a[2*nod].dr=0;
        }
        if(a[nod].rez==dr-st+1)
        {
            a[2*nod].rez=a[2*nod].st=a[2*nod].dr=mij-st+1;
        }
    }
    if(mij+1<=r)
    {
        if(a[nod].rez==0)
        {
            a[2*nod+1].rez=a[2*nod+1].st=a[2*nod+1].dr=0;
        }
        if(a[nod].rez==dr-(mij+1)+1)
        {
            a[2*nod].rez=a[2*nod].st=a[2*nod].dr=dr-(mij+1)+1;
        }
    }
    if(l<=mij)
    {
        update(2*nod, st, mij);
    }
    if(mij+1<=r)
    {
        update(2*nod+1, mij+1, dr);
    }
    if(a[2*nod].st==mij-st+1)
        a[nod].st=a[2*nod].st+a[2*nod].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].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;
    a[1].rez = a[1].st = a[1].dr = n;
    while(m--)
    {
        cout<<m<<" ";
        fin>>t;
        if(t==3)
        {
            cout<<a[1].rez<<"\n";
        }
        if(t==1)
        {
            fin>>x>>y;
            U(x, y, 0);
        }
        if(t==2)
        {
            fin>>x>>y;
            U(x, y, 1);
        }
    }
    return 0;
}