Cod sursa(job #810177)

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

#include <cstdio>
#include <algorithm>

const int MAX_SIZE(3501);

int n;

struct box
{
	int x;
	int y;
	int z;
} boxes [MAX_SIZE];

inline bool operator < (struct box a, struct box b)
{
	return a.x < b.x;
}

int bit [MAX_SIZE] [MAX_SIZE];

inline void read (void)
{
	for (int i(1) ; i <= n ; ++i)
		std::scanf("%d%d%d",&boxes[i].x,&boxes[i].y,&boxes[i].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];
	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 (length > bit[i][j])
				bit[i][j] = length;
}

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

inline void clear_tree (void)
{
	int x, y, z;
	for (x = 1 ; x <= n ; ++x)
		for (y = boxes[x].y; y <= n ; y += lsb(y))
			for (z = boxes[x].z ; z <= n ; z += lsb(z))
				bit[y][z] = 0;
}

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