Cod sursa(job #809699)

Utilizator toranagahVlad Badelita toranagah Data 8 noiembrie 2012 20:53:01
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("cutii.in");
ofstream fout("cutii.out");
const int MAX_N = 3510;

struct box {
	int x, y, z;
} b[MAX_N];
int aib[MAX_N][MAX_N];
bool cmp(box a, box b);
int query(int a, int b);
void update(int x, int y, int val);
void clearAIB();

int N, T;

int main() {
	fin >> N >> T;
	for (int i = 1; i <= T; ++i) {
		for (int j = 1; j <= N; ++j) {
			fin >> b[j].x >> b[j].y >> b[j].z;
		}
		sort(b+1, b+N+1, cmp);
		int result = 0, temp;
		for (int j = 1; j <= N; ++j) {
			temp = query(b[j].y - 1, b[j].z - 1) + 1;
			update(b[j].y, b[j].z, temp);
			result = temp > result ? temp : result;
		}
		clearAIB();
		fout << result << '\n';
	}
	return 0;
}

bool cmp(box a, box b) {
	if (a.x < b.x) return true;
	if (a.x > b.x) return false;
	if (a.y < b.y) return true;
	if (a.y > b.y) return false;
	return a.z < b.z;
}

void update(int x, int y, int val) {
	for (int i = x; i <= N; i += (i & -i)) {
		for (int j = y; j <= N; j += (j & -j)) {
			if (val > aib[i][j]) {
				aib[i][j] = val;
			}
		}
	}
}

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

void clearAIB() {
	for (int i = 1; i <= N; ++i) {
		for (int j = b[i].y; j <= N; j += (j & -j)) {
			for (int k = b[i].z; k <= N; k += (k & -k)) {
				aib[j][k] = 0;
			}
		}
	}
}