Cod sursa(job #405569)

Utilizator loginLogin Iustin Anca login Data 28 februarie 2010 12:22:23
Problema Hotel Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.74 kb
# include <fstream>
# include <iostream>
using namespace std;
struct data{
	int c, l, r;};
data v[600003];
int m, i, a, b;

void mod (int nod, int q)
{
	v[nod].c=q;
	v[nod].l=q;
	v[nod].r=q;
}

void init (int nod, int st, int dr, int val)
{
	if (st==dr)
	{
		mod (nod, val);
		return;
	}
	int mij=(st+dr)/2;
	init(2*nod, st, mij, val);
	init(2*nod+1, mij+1, dr, val);
	v[nod].c=v[2*nod].c+v[2*nod+1].c;
	v[nod].l=v[2*nod].l+v[2*nod+1].l;
	v[nod].r=v[2*nod].r+v[2*nod+1].r;
}

void umple (int nod, int st, int dr)
{
	if (a<=st && dr<=b)
	{
//		init (nod, st, dr, 0);
		mod(nod, 0);
		return;
	}
	int mij=(st+dr)/2;
	if (a<=mij)umple(2*nod, st, mij);
	if (mij<b)umple(2*nod+1, mij+1, dr);
	if (v[2*nod].c+v[2*nod+1].c==dr-st+1)
	{
		v[nod].c=v[2*nod].c+v[2*nod+1].c;
		v[nod].l=v[2*nod].l+v[2*nod+1].l;
		v[nod].r=v[2*nod].r+v[2*nod+1].r;
	}
	else
	{
		if (v[2*nod].l==mij-st+1)
			v[nod].l=v[2*nod].l+v[2*nod+1].l;
		else
			v[nod].l=v[2*nod].l;
		if (v[2*nod+1].r==dr-mij)
			v[nod].r=v[2*nod+1].r+v[2*nod].r;
		else
			v[nod].r=v[2*nod+1].r;
		if (v[nod].l>=v[2*nod].r+v[2*nod+1].l && v[nod].l>=v[nod].r)
			v[nod].c=v[nod].l;
		else
			if (v[nod].r>=v[2*nod].r+v[2*nod+1].l && v[nod].r>=v[nod].l)
				v[nod].c=v[nod].r;
			else
				v[nod].c=v[2*nod].r+v[2*nod+1].l;
	}
}

void goleste (int nod, int st, int dr)
{
	if (a<=st && dr<=b)
	{
	//	init (nod, st, dr, 1);
		mod(nod, dr-st+1);
		return;
	}
	int mij=(st+dr)/2;
	if (a<=mij)goleste(2*nod, st, mij);
	if (mij<b)goleste(2*nod+1, mij+1, dr);
	if (v[2*nod].c+v[2*nod+1].c==dr-st+1)
	{
		v[nod].c=v[2*nod].c+v[2*nod+1].c;
		v[nod].l=v[2*nod].l+v[2*nod+1].l;
		v[nod].r=v[2*nod].r+v[2*nod+1].r;
	}
	else
	{
		if (v[2*nod].l==mij-st+1)
			v[nod].l=v[2*nod].l+v[2*nod+1].l;
		else
			v[nod].l=v[2*nod].l;
		if (v[2*nod+1].r==dr-mij)
			v[nod].r=v[2*nod+1].r+v[2*nod].r;
		else
			v[nod].r=v[2*nod+1].r;
		if (v[nod].l>=v[2*nod].r+v[2*nod+1].l && v[nod].l>=v[nod].r)
			v[nod].c=v[nod].l;
		else
			if (v[nod].r>=v[2*nod].r+v[2*nod+1].l && v[nod].r>=v[nod].l)
				v[nod].c=v[nod].r;
			else
				v[nod].c=v[2*nod].r+v[2*nod+1].l;
	}
}

int main ()
{
	ifstream fin ("hotel.in");
	freopen ("hotel.out", "w", stdout);
	int n, p, k;
	fin>>n>>p;
	init(1, 1, n, 1);
	for (;p;--p)
	{
		fin>>k;
		if (k==3)
			printf("%d\n", v[1].c);
		else
		{
			fin>>i>>m;
			if (k==1)
			{
				a=i;
				b=i+m-1;
				umple(1, 1, n);
			}
			else
				if (k==2)
				{
					a=i;
					b=i+m-1;
					goleste(1, 1, n);
				}
		}/*	
		for (int i=1;i<3*n;i++)
			cout<<v[i].c<<" ";
		cout<<endl;
		for (int i=1;i<3*n;i++)
			cout<<v[i].l<<" ";
		cout<<endl;
		for (int i=1;i<3*n;i++)
			cout<<v[i].r<<" ";
		cout<<endl<<endl;*/
	}
	return 0;
}