Cod sursa(job #600826)

Utilizator rumburakrumburak rumburak Data 3 iulie 2011 17:16:16
Problema Hotel Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>

using namespace std;

const int N = 1 + (1<<18);

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

struct nod
{
	int st,dr,max;
};

int n, a, b;
nod t[N];

inline int max(int x, int y)
{
	return x>y ? x : y;
}

void modifica(int tip, int p, int st, int dr)
{
	if(st == dr)
	{
		if(tip == 1)
			t[p].st = t[p].dr = t[p].max = 0;
		else
			t[p].st = t[p].dr = t[p].max = dr - st + 1;
		return;
	}
	
	int mij = (st + dr) >> 1;
	
	if(t[p].max == dr-st+1)
	{
		t[p<<1].st = t[p<<1].dr = t[p<<1].max = mij - st + 1;
		t[(p<<1)+1].st = t[(p<<1)+1].dr = t[(p<<1)+1].max = dr - mij;
	}
	
	if(t[p].max == 0)
	{
		t[p<<1].st = t[p<<1].dr = t[p<<1].max = 0;
		t[(p<<1)+1].st = t[(p<<1)+1].dr = t[(p<<1)+1].max = 0;
	}
	
	if(a <= mij)
		modifica(tip, p<<1, st, mij);
	if(b > mij)
		modifica(tip, (p<<1)+1, mij+1, dr);
	
	t[p].st = t[p<<1].st;
	if(t[p<<1].st == mij - st + 1)
		t[p].st += t[(p<<1)+1].st;
	
	t[p].dr = t[(p<<1)+1].dr;
	if(t[(p<<1)+1].dr == dr - mij)
		t[p].dr += t[p<<1].dr;
	
	t[p].max = max(max(t[p<<1].max, t[(p<<1)+1].max), t[p<<1].dr + t[(p<<1)+1].st);
}

int main()
{
	int m,tip,nr;
	in>>n>>m;
	t[1].st = t[1].dr = t[1].max = n;
	while(m--)
	{
		in>>tip;
		if(tip == 1 || tip == 2)
		{
			in>>a>>nr;
			b = a + nr - 1;
			modifica(tip, 1, 1, n);
			continue;
		}
		out<<t[1].max<<"\n";
	}
	return 0;
}