Cod sursa(job #1024602)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 8 noiembrie 2013 20:47:25
Problema Hotel Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.49 kb

#include <cstdio>
#include <algorithm>

const int MAX_N(200010);

int n, p;

struct Node
{
	long long a = 0, b = 0, c = 0;
	long long sum = 0, add = 0;
} It [MAX_N << 2];

inline int Left_son (const int INDEX)
{
	return INDEX << 1;
}

inline int Right_son (const int INDEX)
{
	return (INDEX << 1) + 1;
}

void ItBuild (int index, int left, int right)
{
	if (left == right)
	{
		It[index].a = It[index].b = It[index].c = 1;
		It[index].sum = 1;
		return;
	}
	int mid((left + right) >> 1);
	ItBuild(Left_son(index),left,mid);
	ItBuild(Right_son(index),mid + 1,right);
	It[index].sum = It[Left_son(index)].sum + It[Right_son(index)].sum;
	It[index].a = std::max(It[Left_son(index)].a,It[Left_son(index)].sum + It[Right_son(index)].a);
	It[index].b = std::max(std::max(It[Left_son(index)].b,It[Right_son(index)].b),It[Left_son(index)].c + It[Right_son(index)].a);
	It[index].c = std::max(It[Right_son(index)].c,It[Right_son(index)].sum + It[Left_son(index)].c);
}

void ItUpdate (int index, int left, int right, int ul, int ur, int value)
{
	if ((left == right) || (ul <= left && right <= ur))
	{
		It[index].add = value;
		It[index].a = It[index].b = It[index].c = (It[index].add >= 0) * (right - left + 1LL);
		It[index].sum = It[index].add * (right - left + 1LL);
		if (left == right)
			It[index].add = 0;
		return;
	}
	int mid((left + right) >> 1);
	if (It[index].add)
	{
		ItUpdate(Left_son(index),left,mid,left,mid,It[index].add);
		ItUpdate(Right_son(index),mid + 1,right,mid + 1,right,It[index].add);
		It[index].add = 0;
	}
	if (ul <= mid)
		ItUpdate(Left_son(index),left,mid,ul,ur,value);
	if (ur > mid)
		ItUpdate(Right_son(index),mid + 1,right,ul,ur,value);
	It[index].sum = It[Left_son(index)].sum + It[Right_son(index)].sum;
	It[index].a = std::max(It[Left_son(index)].a,It[Left_son(index)].sum + It[Right_son(index)].a);
	It[index].b = std::max(std::max(It[Left_son(index)].b,It[Right_son(index)].b),It[Left_son(index)].c + It[Right_son(index)].a);
	It[index].c = std::max(It[Right_son(index)].c,It[Right_son(index)].sum + It[Left_son(index)].c);	
}

int main (void)
{
	std::freopen("hotel.in","r",stdin);
	std::freopen("hotel.out","w",stdout);
	std::scanf("%d %d",&n,&p);
	ItBuild(1,1,n);
	int a, b, c;
	while (p)
	{
		std::scanf("%d",&a);
		if (a == 3)
			std::printf("%lld\n",std::max(std::max(It[1].a,It[1].b),It[1].c));
		else
		{
			std::scanf("%d %d",&b,&c);
			if (a == 1)
				ItUpdate(1,1,n,b,b + c - 1,-MAX_N);
			else
				ItUpdate(1,1,n,b,b + c - 1,1);
		}
		--p;
	}
	std::fclose(stdin);
	std::fclose(stdout);
	return 0;
}