Cod sursa(job #2965532)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 15 ianuarie 2023 15:23:19
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

#define lsb(x)(x & (-x))

const int max_size = 3501;

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

str cutii[max_size];
int aib[max_size][max_size], n;

void upd (int x, int y, int val)
{
    for (int i = x; i <= n; i += lsb(i))
    {
        for (int j = y; j <= n; j += lsb(j))
        {
            aib[i][j] = max(aib[i][j], val);
        }
    }
}

int querry (int x, int y)
{
    int ans = 0;
    for (int i = x; i > 0; i -= lsb(i))
    {
        for (int j = y; j > 0; j -= lsb(j))
        {
            ans = max(ans, aib[i][j]);
        }
    }
    return ans + 1;
}

void reset (int x, int y)
{
    for (int i = x; i <= n; i += lsb(i))
    {
        for (int j = y; j <= n; j += lsb(j))
        {
            aib[i][j] = 0;
        }
    }
}

bool cmp (str a, str b)
{
    return a.z < b.z;
}

void solve ()
{
    for (int i = 1; i <= n; i++)
    {
        in >> cutii[i].x >> cutii[i].y >> cutii[i].z;
    }
    sort(cutii + 1, cutii + n + 1, cmp);
    int ans = 1;
    for (int i = 1; i <= n; i++)
    {
        int mx = querry(cutii[i].x - 1, cutii[i].y - 1);
        upd(cutii[i].x, cutii[i].y, mx);
        ans = max(ans, mx);
    }
    for (int i = 1; i <= n; i++)
    {
        reset(cutii[i].x, cutii[i].y);
    }
    out << ans << '\n';
}

int main ()
{
    int t;
    in >> n >> t;
    while (t--)
    {
        solve();
    }
    in.close();
    out.close();
    return 0;
}