Cod sursa(job #1437094)

Utilizator vladrochianVlad Rochian vladrochian Data 16 mai 2015 21:03:34
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <algorithm>
using namespace std;

const int kMaxN = 3505;

ifstream fin("cutii.in");
ofstream fout("cutii.out");

struct Chestie {
	int x, y, z;
	bool operator<(const Chestie &other) const {
		return x < other.x;
	}
} a[kMaxN];

int N, T, sol;
int aib[kMaxN][kMaxN];

void UpdMax(int &a, int b) {
	if (b > a)
		a = b;
}

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)
			UpdMax(aib[i][j], val);
}

int Query(int x, int y) {
	int ans = 0;
	for (int i = x; i > 0; i -= i & -i)
		for (int j = y; j > 0; j -= j & -j)
			UpdMax(ans, aib[i][j]);
	return ans + 1;
}

void Clear(int x, int y) {
	for (int i = x; i <= N; i += i & -i)
		for (int j = y; j <= N; j += j & -j)
			aib[i][j] = 0;
}

void Test() {
	for (int i = 0; i < N; ++i)
		fin >> a[i].x >> a[i].y >> a[i].z;
	sort(a, a + N);

	sol = 0;
	for (int i = 0; i < N; ++i) {
		int crt = Query(a[i].y, a[i].z);
		UpdMax(sol, crt);
		Update(a[i].y, a[i].z, crt);
	}

	for (int i = 0; i < N; ++i)
		Clear(a[i].y, a[i].z);

	fout << sol << "\n";
}

int main() {
	fin >> N >> T;
	while (T--)
		Test();
	return 0;
}