Cod sursa(job #1893245)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 25 februarie 2017 16:06:30
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using pii = pair<int, int>;
#define dbg(x) cerr<<#x": "<<x<<'\n'
#define dbg_v(x, n) cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<'\n'
#define _sort_stl(v) sort(v.begin(), v.end())
#define _sort(v, n) sort(v, v + n);
#define NMAX 3501
#define INF 2000000

int n;
pii v[NMAX];
int bit[NMAX][NMAX];

void read(ifstream &cin)
{
	int i, a, b, c;

	for(i = 1; i <= n; ++i)
	{
		cin >> a >> b >> c;
		v[a] = {b, c};
	}
}

void update(pii p, int val)
{
	int i, j;

	for(i = p.first; i < NMAX; i += i & -i)
	{
		for(j = p.second; j < NMAX; j += j & -j)
			bit[i][j] = max(bit[i][j], val);
	}
}

void mark(pii p, int val)
{
	int i, j;

	for(i = p.first; i < NMAX; i += i & -i)
	{
		for(j = p.second; j < NMAX; j += j & -j)
			bit[i][j] = val;
	}
}

int query(pii p)
{
	int i, j, ans;

	ans = 0;
	for(i = p.first; i > 0; i &= i - 1)
	{
		for(j = p.second; j > 0; j &= j - 1)
			ans = max(bit[i][j], ans);
	}

	return ans;
}

int solve()
{
	int i, ans, bst;

	ans = 1;
	for(i = 1; i <= n; ++i)
	{
		bst = 1 + query(v[i]);
		update(v[i], bst);

		if(bst > ans) ans = bst;
	}

	for(i = 1; i <= n; ++i)
		mark(v[i], -INF);

	return ans;
}

int main()
{
//#ifndef ONLINE_JUDGE
	ifstream cin("cutii.in");
	ofstream cout("cutii.out");
//#endif
	ios_base::sync_with_stdio(false);

	int t;

	for(int i = 1; i < NMAX; ++i)
		for(int j = 1; j < NMAX; ++j)
			bit[i][j] = -INF;

	for(cin >> n >> t; t; --t)
	{
		read(cin);
		cout << solve() << '\n';
	}

	return 0;
}