Cod sursa(job #1799050)

Utilizator grimmerFlorescu Luca grimmer Data 5 noiembrie 2016 18:28:07
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

const int NMAX = 3505;
int BITree[NMAX + 5][NMAX + 5], n, t;

struct point{
	int x, y, z;
}box[NMAX + 5];

bool cmp(point a, point b){
	if(a.z == b.z){
		if(a.x == b.x){
			return a.y > b.y;
		}
		return a.x > b.x;
	}

	return a.z < b.z;
}

int Query(int y, int x){
	int x1, i, j, ans = 0;

	for(i = y; i > 0; i -= i & (-i)){

		x1 = x;
		for(j = x1; j > 0; j -= j & (-j)){
			if(BITree[i][j] > ans)  ans = BITree[i][j];
		}
	}

	return ans;
}

void Update(int y, int x, int val){
	int i, j, x1;

	for(i = y; i <= n; i += i & (-i)){

		x1 = x;
		for(j = x1; j <= n; j += j & (-j)){
            if(BITree[i][j] < val)  BITree[i][j] = val;
		}

	}

}

void empty_BITree(){
	int i, j;

	for(i = 1; i <= n; ++i){
		for(j = 1; j <= n; ++j){
			BITree[i][j] = 0;
		}
	}

}

int main()
{
	int i, z, x, y, j, len, ans;
	fin>>n>>t;

	for(i = 1; i <= t; ++i){

		for(j = 1; j <= n; ++j){

			fin>>x>>y>>z;
			box[j] = {z, y, x};
		}

		sort(box + 1, box + 1 + n, cmp);
		empty_BITree();
		ans = 0;

        for(j = 1; j <= n; ++j){
			len = Query(box[j].y - 1, box[j].x - 1);
			Update(box[j].y, box[j].x, len + 1);
        }

		ans = max(ans, Query(n, n));
		fout<< ans << "\n";
	}

    return 0;
}