Cod sursa(job #222401)

Utilizator CezarMocanCezar Mocan CezarMocan Data 22 noiembrie 2008 11:56:35
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAXN 151

using namespace std;

struct cutie {
	short int x, y, z;
};

short int n, t, i, j, aib[MAXN][MAXN], rez;
cutie v[MAXN];

bool cmp(cutie a, cutie b) {
	if ((a.z < b.z) || ((a.z == b.z) && (a.x < b.x)) || ((a.z == b.z) && (a.x == b.x) && (a.y < b.y))) 
		return true;
	else
		return false;

}

void read() {
	int i;
	memset(v, 0, sizeof(v));
	memset(aib, 0, sizeof(aib));
	rez = 0;

	for (i = 1; i <= n; i++)
		scanf("%d%d%d", &v[i].x, &v[i].y, &v[i].z);

	sort(v + 1, v + n + 1, cmp);
}

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

int query(int x, int y) {
	int smax = 0, i, j;
	for (i = x; i >= 1; i -= lsb(i))
		for (j = y; j >= 1; j -= lsb(j))
			if (aib[i][j] > smax)
				smax = aib[i][j];
	return smax;
}

void update(int x, int y, int up) {
	int i, j;
	for (i = x; i <= n; i += lsb(i))
		for (j = y; j <= n; j += lsb(i))
			if (up > aib[i][j])
				aib[i][j] = up;
}

void solve() {
	int i, mx;

	for (i = 1; i <= n; i++) {
		mx = query(v[i].x - 1, v[i].y - 1);
		if (mx > rez)
			rez = mx;
		update(v[i].x, v[i].y, mx + 1);
	}

}

int main() {
	freopen("cutii.in", "r", stdin);
	freopen("cutii.out", "w", stdout);

	scanf("%d%d", &n, &t);

	for (i = 1; i <= t; i++) {
		read();
		solve();
		printf("%d\n", rez + 1);
	}

	return 0;
}