Cod sursa(job #35437)

Utilizator amadaeusLucian Boca amadaeus Data 22 martie 2007 01:50:36
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cassert>

using namespace std;

struct ent {
	int x, y, z;
};

struct cmp {
	bool operator() ( ent a, ent b ) {
		return a.z < b.z;
	}
};

#define NX 3501

ent v[ NX ];
int N;


inline
void umax( int& x, int y ) {
	if( x < y )
		x = y;
}

struct BIT {
	int a[NX][NX];

	void shift( int& x ) {
		x += x & ( x ^ (x-1) );
	}
	void strip( int& x ) {
		x &= x-1;
	}

	void update( int l, int c, int x ) {
	
		assert( l!=0 && c!=0 );
		
		for( int i = l; i <= N; shift( i ) )
			for( int j = c; j <= N; shift( j ) )
				umax( a[i][j], x );
	}
	int query( int l, int c ) {
		int r = a[l][c];
		for( int i = l; i; strip( i ) )
			for( int j = c; j; strip( j ) )
				umax( r, a[i][j] );
		return r;
	}
	void clear() {
		memset( a, 0, sizeof( a ) );
	}
} A;

void cit() {
	int i, vmax, tmax, T;
	
	scanf( "%d%d", &N, &T );
	
	for( ; T; T-- ) {
		for( i = 0; i < N; i++ )
			scanf( "%d%d%d", &v[i].x, &v[i].y, &v[i].z );

		sort( v, v + N, cmp() );

		A.clear();
		A.update( v[0].x, v[0].y, 1 );
		tmax = 0;
		for( i = 1; i < N; i++ ) {
			vmax = 0;
			umax( vmax, A.query( v[i].x, v[i].y ) );
			vmax = vmax ? vmax + 1 : 1;
			A.update( v[i].x, v[i].y, vmax );
			umax( tmax, vmax );
		}

		printf( "%d\n", tmax );
	}
}
			
int main() {
	freopen( "cutii.in", "r", stdin );
	freopen( "cutii.out", "w", stdout );

	cit();

	return 0;
}