Cod sursa(job #999888)

Utilizator andreiiiiPopa Andrei andreiiii Data 21 septembrie 2013 16:58:29
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <cstdio>
#include <algorithm>
#define N 3501
#define zeros(x) ( (x ^ (x - 1)) & x )

using namespace std;

int a[N][N];
int n;

struct nr
{
	int x, y, z;
	bool operator < (const nr &e) const
	{
		return x<e.x;
	}
} b[N];

void update(int y, int zz, int val)
{
	int z;
	while(y<=n)
	{
		z=zz;
		while(z<=n)
		{
			a[y][z]=max(a[y][z], val);
			z+=zeros(z);
		}
		y+=zeros(y);
	}
}

int query(int y, int zz)
{
	int ret=0, z;
	while(y)
	{
		z=zz;
		while(z)
		{
			ret=max(a[y][z], ret);
			z-=zeros(z);
		}
		y-=zeros(y);
	}
	return ret;
}

void clear(int y, int zz)
{
	int z;
	while(y<=n)
	{
		z=zz;
		while(z<=n)
		{
			a[y][z]=0;
			z+=zeros(z);
		}
		y+=zeros(y);
	}
}

int main()
{
	freopen("cutii.in", "r", stdin);
	freopen("cutii.out", "w", stdout);
	int t, i, dp, sol;
	scanf("%d%d", &n, &t);
	for(;t;t--)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d", &b[i].x, &b[i].y, &b[i].z);
		}
		sort(b+1, b+n+1);
		sol=0;
		for(i=1;i<=n;i++)
		{
			dp=query(b[i].y-1, b[i].z-1)+1;
			update(b[i].y, b[i].z, dp);
			if(dp>sol) sol=dp;
		}
		for(i=1;i<=n;i++)
		{
			clear(b[i].y, b[i].z);
		}
		printf("%d\n", sol);
	}
}