Cod sursa(job #3149175)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 6 septembrie 2023 17:06:26
Problema Cutii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.35 kb
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
#include <bits/stdc++.h>
using namespace std;
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

int aib[3505][3505];
struct box
{
    int x, y, z;
}v[3505];
int n, test_cases;
inline bool compare(box a, box b)
{
    return a.x < b.x;
}
void update(int poz, int val, int nr)
{
    for(int i = poz; i <= n; i += (i & (-i)))
    {
        for(int j = val; j <= n; j += (j & (-j)))
        {
            aib[i][j] = max(aib[i][j], nr);
        }
    }
}
int query(int poz, int val)
{
    int nr = 0;
    for(int i = poz; i > 0; i -= (i & (-i)))
    {
        for(int j = poz; j > 0; j -= (j & (-j)))
        {
            nr = max(aib[i][j], nr);
        }
    }
    return nr + 1;
}
void reset(int poz, int val)
{
    for(; poz <= n; poz += (poz & (-poz)))
    {
        for(; val <= n; val += (val & (-val)))
        {
            aib[poz][val] = 0;
        }
    }
}
InParser fin("cutii.in");
ofstream fout("cutii.out");
int32_t main()
{
    fin >> n >> test_cases;
   for(int u = 1; u  <= test_cases; ++u)
    {
        int maxi = 1;
        for(int i = 1; i <= n; ++i)
        {
            fin >> v[i].x >> v[i].y >> v[i].z;
        }
        sort(v + 1, v + n + 1, compare);
        for(int i = 1; i <= n; ++i)
        {
            maxi = max(maxi, query(v[i].y - 1, v[i].z - 1));
            update(v[i].y, v[i].z, query(v[i].y - 1, v[i].z - 1));
        }
        fout << maxi << '\n';
        for(int i = 1; i <= n; ++i)
        {
        reset(v[i].y, v[i].z);
        }
    }
    return 0;
}