Cod sursa(job #548506)

Utilizator david_raucaRauca Ioan David david_rauca Data 7 martie 2011 15:03:24
Problema Hotel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.66 kb
#include<fstream>
#include<vector>
using namespace std;

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

int n, m, start, finish, v;

vector<pair<int, pair<int, int> > > arb;

void Read();
void Initializare(int nod, int st, int dr );
void Update(int nod, int st, int dr );
//void Update1(int nod, int st, int dr );

int main()
{
	Read();
	
	fin.close();
	fout.close();
	
	return 0;
}

void Read()
{
	fin >> n >> m;
	int a, b, t;
	int l = 4*n;
	arb.resize(l+2);
	Initializare(1, 1, n );
	for( int i = 1; i <= m; ++i )
	{
		fin >> t;
		if( t == 1 )
		{
			fin >> a >> b;
			v = 0;
			start = a;
			finish = a+b-1;
			Update( 1, 1, n );
		}
		if( t == 2 )
		{
			fin >> a >> b;
			v = 1;
			start = a;
			finish = a+b-1;
			Update(1, 1, n);
		}
		if( t == 3 )
				fout << arb[1].first << '\n';
    }
}

void Update(int nod, int st, int dr )
{
	if( st == dr )
	{
		arb[nod].first = v;
		arb[nod].second.first = v;
		arb[nod].second.second = v;
		return;
	}
	
	int mij = (st+dr) >> 1;
	
	int stnod = (nod << 1);
	int drnod = stnod + 1;
	
	if( start <= mij )
		Update( stnod, st, mij );
	if( finish > mij )
		Update( drnod, mij+1, dr );
	
	arb[nod].second.first = arb[stnod].second.first;
	arb[nod].second.second = arb[drnod].second.second;
	
	if( arb[stnod].second.first == (dr - st + 1)- (dr - mij) /*&& arb[2*nod].second.first != 0 */)
		arb[nod].second.first = arb[stnod].second.first + arb[drnod].second.first;
	
	if( arb[drnod].second.first == dr - mij /*&& arb[2*nod+1].second.first != 0 */)
		arb[nod].second.second = arb[stnod].second.second + arb[drnod].second.first; 
	
	arb[nod].first = max( arb[stnod].first, arb[drnod].first );
	
	if( arb[nod].first < arb[nod].second.first )
		arb[nod].first = arb[nod].second.first;
	
	if( arb[nod].first < arb[nod].second.second )
		arb[nod].first = arb[nod].second.second;
	
	if( arb[nod].first < arb[stnod].second.second + arb[drnod].second.first )
	    arb[nod].first = arb[stnod].second.second + arb[drnod].second.first;
}
/*
void Update1(int nod, int st, int dr )
{
	if( st == dr )
	{
		arb[nod].first = 1;
		arb[nod].second.first = 1;
		arb[nod].second.second = 1;
		return;
	}
	
	int mij = (st + dr) / 2;
	
	if( start <= mij )
		Update1( 2*nod, st, mij );
	if( finish > mij )
		Update1( 2*nod+1, mij+1, dr );
	
	arb[nod].second.first = arb[2*nod].second.first;
	arb[nod].second.second = arb[2*nod+1].second.second;
	
	if( arb[2*nod].second.first == (dr- st + 1)- (dr - mij) && arb[2*nod].second.first != 0 )
		arb[nod].second.first = arb[2*nod].second.first + arb[2*nod+1].second.first;
	
	if( arb[2*nod+1].second.first == dr - mij && arb[2*nod+1].second.first != 1 )
		arb[nod].second.second = arb[2*nod].second.second + arb[2*nod+1].second.second; 
	
	arb[nod].first = max( arb[2*nod].first, arb[2*nod+1].first );
	
	if( arb[nod].first < arb[nod].second.first )
		arb[nod].first = arb[nod].second.first;
	
	if( arb[nod].first < arb[nod].second.second )
		arb[nod].first = arb[nod].second.second;
	
	if( arb[nod].first < arb[2*nod].second.second + arb[2*nod+1].second.first )
	    arb[nod].first = arb[2*nod].second.second + arb[2*nod+1].second.first;
}
*/
void Initializare( int nod, int st, int dr )
{
	if( st == dr )
	{
		arb[nod].first = 1;
		arb[nod].second.first = 1;
		arb[nod].second.second = 1;
		return;
	}
	
	int mij = (st+dr)/2;
	
	Initializare( 2*nod, st, mij );
	Initializare( 2*nod+1, mij+1, dr );
	
	arb[nod].first = arb[2*nod].first + arb[2*nod+1].first;
	arb[nod].second.first = arb[2*nod].second.first + arb[2*nod+1].second.first;
	arb[nod].second.second = arb[2*nod].second.second + arb[2*nod+1].second.second;
}