Cod sursa(job #227192)

Utilizator alex_mircescuAlex Mircescu alex_mircescu Data 3 decembrie 2008 21:41:13
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

#define MAXN 3500

short int test, n, t, i, j, k, sol, x, cut[MAXN][3], v[MAXN], aib[MAXN][MAXN];

bool cmp(long x, long y) {
    return cut[x][2] < cut[y][2];
}

inline long LSB(long num) {
	return num ^ (num & (num - 1));
}
inline long query(long x, long y) {
    long ret = 0;
    for (long i = x; i > 0; i -= LSB(i)) {
		for (long j = y; j > 0; j -= LSB(j)) {
            if (ret < aib[i][j]) {
				ret = aib[i][j];
			}
		}
	}
    return ret; 
}
inline void update(long x, long y, long k) {
	for (long i = x; i <= n; i += LSB(i)) {
		for (long j = y; j <= n; j += LSB(j)) {
			if (aib[i][j] < k) {
				aib[i][j] = k;
			}
		}
	}
}

int main() {
	freopen("cutii.in", "r", stdin);
    freopen("cutii.out", "w", stdout);
    scanf("%hd %hd", &n, &t);
    for (test = 1; test <= t; ++test) {
        sol = 0; 
        for (i = 1; i <= n; v[i] = i, ++i) {
            scanf("%hd %hd %hd", &cut[i][0], &cut[i][1], &cut[i][2]);
		}
        for (i = 0; i <= n; ++i) {
            for (j = 0; j <= n; ++j) {
                aib[i][j] = 0;
			}
		}
		sort(v + 1, v + n + 1, cmp);
		for (i = 1; i <= n; ++i) {
			x = query(cut[v[i]][0] - 1, cut[v[i]][1] - 1) + 1;
			if (x > sol) {
				sol = x;
			}
			update(cut[v[i]][0], cut[v[i]][1], x);
		}
		printf("%hd\n", sol);
	}
    return 0;
}