Cod sursa(job #2891725)

Utilizator Langa_bLanga Radu Langa_b Data 19 aprilie 2022 17:20:58
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("cutii.in");
ofstream cout("cutii.out");
short n, t;
struct paralel {
	short x, y, z;
};
bool cmp(paralel a, paralel b) {
	return a.x < b.x;
}
paralel c[3502];
short aib[3502][3502];
void update(short x, short &y, short &val) {
	for (; x <= n; x += (x & -x)) {
		for (int yy = y; yy <= n; yy += (yy & -yy)) {
			aib[x][yy] = max(aib[x][yy], val);
		}
	}
}
short query(short x, short &y) {
	short ans = 0;
	for (; x > 0; x -= (x & -x)) {
		for (int yy = y; yy > 0; yy -= (yy & -yy)) {
			ans = max(ans, aib[x][yy]);
		}
	}
	return ans;
}
void clear(short x, short y) {
	for (; x <= n; x += (x & -x)) {
		for (int yy = y; yy <= n; yy += (yy & -yy)) {
			aib[x][yy] = 0;
		}
	}
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> t;
	for (short z = 1; z <= t; z++) {
		for (short i = 1; i <= n; i++) {
			cin >> c[i].x >> c[i].y >> c[i].z;
		}
		sort(c + 1, c + n + 1, cmp);
		short bst = 0;
		for (short i = 1; i <= n; i++) {
			bst = query(c[i].y, c[i].z) + 1;
			update(c[i].y, c[i].z, bst);
		}
		cout << query(n, n) << '\n';
		for (short i = 1; i <= n; i++) {
			clear(c[i].y, c[i].z);
		}
	}
}