Cod sursa(job #3149158)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 6 septembrie 2023 16:44:47
Problema Cutii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
#include <bits/stdc++.h>
using namespace std;
int aib[3505][3505], maxi;
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; i -= (i & -i))
    {
        for(int j = poz; j; j -= (j & -j))
        {
            nr = max(aib[i][j], nr);
        }
    }
    return nr + 1;
}
void reset(int poz, int val)
{
    for(; poz >= 1; poz -= (poz & -poz))
    {
        for(; val >= 1; val -= (val & -val))
        {
            aib[poz][val] = 0;
        }
    }
}
ifstream fin("cutii.in");
ofstream fout("cutii.out");
int32_t main(int argc, char * argv[])
{
    fin >> n >> test_cases;
    while(test_cases--)
    {
        maxi = 0;
        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].x, v[i].y, query(v[i].x - 1, v[i].y - 1));
        }
        fout << maxi << '\n';
        for(int i = 1; i <= n; ++i)
        {
        reset(v[i].y, v[i].z);
        }
    }
    return 0;
}