Cod sursa(job #810147)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 9 noiembrie 2012 19:01:47
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>

const int MAX_SIZE(3501);

int n;

int boxes [MAX_SIZE] [2];
int bit [MAX_SIZE] [MAX_SIZE];

inline void read (void)
{
	int x, y, z;
	for (int i(1) ; i <= n ; ++i)
	{
		std::scanf("%d%d%d",&x,&y,&z);
		boxes[x][0] = y;
		boxes[x][1] = z;
	}
}

inline int lsb (int x)
{
	return x & -x;
}

inline int query (int x, int y)
{
	int i, j, max(0);
	for (i = x ; i ; i -= lsb(i))
		for (j = y ; j ; j-= lsb(j))
			if (bit[i][j] > max)
				max = bit[i][j];
		i -= lsb(i);
	return max;
}

inline void update (int x, int y, int length)
{
	int i, j;
	for (i = x ; i <= n ; i += lsb(i))
		for (j = y ; j <= n ; j += lsb(x))
			if (bit[i][j] < length)
				bit[i][j] = length;
			else
				break;
}

inline int maximum_incresing_substring (void)
{
	int max(0), length;
	for (int i(1) ; i <= n ; ++i)
	{
		length = query(boxes[i][0],boxes[i][1]) + 1;
		if (length > max)
			max = length;
		update(boxes[i][0],boxes[i][1],length);
	}
	return max;
}

inline void clear_tree (void)
{
	for (int i(1) ; i <= n ; ++i)
		std::memset(bit[i],0,n * sizeof(int) + 1);
}

int main (void)
{
	std::srand(std::time(0));
	std::freopen("cutii.in","r",stdin);
	std::freopen("cutii.out","w",stdout);
	int t;
	std::scanf("%d%d",&n,&t);
	while (true)
	{
		read();
		std::printf("%d\n",maximum_incresing_substring());
		--t;
		if (t)
			clear_tree();
		else
			break;
	}
	std::fclose(stdin);
	std::fclose(stdout);
	return 0;
}