Cod sursa(job #733925)

Utilizator scipianusFMI Ciprian Olariu scipianus Data 13 aprilie 2012 10:41:13
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.02 kb
#include<fstream>
#include<cmath>
using namespace std;
int n,m;
struct Nod{int lgst,lgdr,lgmax;};
Nod AInt[300100];
int op,a,b;

inline void Merge(int nod,int st,int dr)
{
	int mij=(st+dr)/2;
	
	AInt[nod].lgst=AInt[2*nod].lgst;
	if(AInt[2*nod].lgst==mij-st+1) //toate camerele fiului stang sunt ocupate
		AInt[nod].lgst+=AInt[2*nod+1].lgst; //avansez cu camerele din stanga si in fiul drept
	
	AInt[nod].lgdr=AInt[2*nod+1].lgdr;
	if(AInt[2*nod+1].lgdr==dr-mij) //toate camerele fiului drept sunt ocupate
		AInt[nod].lgdr+=AInt[2*nod].lgdr; //avansez cu camerele din dreapta si in fiul stang
	
	AInt[nod].lgmax=max(AInt[2*nod].lgmax,AInt[2*nod+1].lgmax);
	AInt[nod].lgmax=max(AInt[nod].lgmax,max(AInt[nod].lgst,AInt[nod].lgdr));
	AInt[nod].lgmax=max(AInt[nod].lgmax,AInt[2*nod].lgdr+AInt[2*nod+1].lgst);
}

inline void Update(int nod,int st,int dr)
{
	if(a<=st && dr<=b)
	{
		if(op==1) //ocup toate camerele din [st,dr]
			AInt[nod].lgst=AInt[nod].lgdr=AInt[nod].lgmax=0;
		else //eliberez toate camerele din [st,dr]
			AInt[nod].lgst=AInt[nod].lgdr=AInt[nod].lgmax=dr-st+1;
		return;
	}
	int mij=(st+dr)/2;
	if(AInt[nod].lgmax==dr-st+1) //actualizez fii (eliberez toate camerele din subintervale)
	{
		AInt[2*nod].lgst=AInt[2*nod].lgdr=AInt[2*nod].lgmax=mij-st+1;
		AInt[2*nod+1].lgst=AInt[2*nod+1].lgdr=AInt[2*nod+1].lgmax=dr-mij;
	}
	if(AInt[nod].lgmax==0) //actualizez fii (ocup toate camerele din subintervale)
	{
		AInt[2*nod].lgst=AInt[2*nod].lgdr=AInt[2*nod].lgmax=0;
		AInt[2*nod+1].lgst=AInt[2*nod+1].lgdr=AInt[2*nod+1].lgmax=0;
	}
	if(a<=mij)
		Update(2*nod,st,mij);
	if(mij+1<=b)
		Update(2*nod+1,mij+1,dr);
	Merge(nod,st,dr);
}

int main()
{
	int i;
	ifstream fin("hotel.in");
	ofstream fout("hotel.out");
	fin>>n>>m;
	////eliberez initial toate camerele
	op=2;
	a=1;
	b=n;
	Update(1,1,n);
	//
	for(i=1;i<=m;i++)
	{
		fin>>op;
		if(op==3)
			fout<<AInt[1].lgmax<<"\n";
		else
		{
			fin>>a>>b;
			b=a+b-1;
			Update(1,1,n);
		}
	}
	fin.close();
	fout.close();
	return 0;
}