Cod sursa(job #459687)

Utilizator darrenRares Buhai darren Data 30 mai 2010 18:41:48
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include<fstream>
#include<algorithm>
using namespace std;

struct point
{
	int x, y, z;
};
bool cmp(point a, point b)
{
	return a.z <= b.z;
}

void update(int p1, int p2, int val);
int query(int p1, int p2);
int n, t, aib[3501][3501];
point p[3501];

int main()
{
	ifstream fin("cutii.in");
	ofstream fout("cutii.out");
	fin >> n >> t;
	for (int i = 1; i <= t; ++i)
	{
		int mx = 0;
		for (int j = 1; j <= n; ++j)
			fin >> p[j].x >> p[j].y >> p[j].z;
		sort(p + 1, p + n + 1, cmp);
		for (int j = 1; j <= n; ++j)
		{
			int x = query(p[j].x, p[j].y);
			update(p[j].x, p[j].y, x + 1);
			if (x + 1 > mx)
				mx = x + 1;
		}
		for (int j = 1; j <= n; ++j)
			memset(aib[j], 0, sizeof(aib[j]));
		fout << mx << '\n';
	}
}

void update(int p1, int p2, int val)
{
	int c1 = 0;
	while (p1 <= n)
	{
		int c2 = 0, aux = p2;
		while (aux <= n)
		{
			aib[p1][aux] = max(aib[p1][aux], val);
			while (!(aux & 1 << c2))
				++c2;
			aux += 1 << c2;
			++c2;
		}
		while (!(p1 & 1 << c1))
			++c1;
		p1 += 1 << c1;
		++c1;
	}
}

int query(int p1, int p2)
{
	int c1 = 0, mx = 0;
	while (p1 >= 1)
	{
		int c2 = 0, aux = p2;
		while (aux >= 1)
		{
			mx = max(aib[p1][aux], mx);
			while (!(aux & 1 << c2))
				++c2;
			aux -= 1 << c2;
			++c2;
		}
		while (!(p1 & 1 << c1))
			++c1;
		p1 -= 1 << c1;
		++c1;
	}
	return  mx;
}