Cod sursa(job #3136618)

Utilizator ciacliboiiiciacli stefan ciacliboiii Data 7 iunie 2023 11:28:51
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("cutii.in");
ofstream fout("cutii.out");
int n, t, aib[3501][3501], ans;
struct cutie{
    int x, y, z;
}v[3501];
int cmp(cutie &a, cutie &b)
{
    if(a.x == b.x)
    {
        if(a.y == b.y)
            return a.z > b.z;
        return a.y > b.y;
    }
    return a.x < b.x;
}
void update(int posy, int posz, int val)
{
    for(int i = posy; i <= n; i += i & (-i))
        for(int j = posz; j <= n; j += j & (-j))
            aib[i][j] = max(aib[i][j], val);
}
int query(int posy, int posz)
{
    int m = 0;
    for(int i = posy; i > 0; i -= i & (-i))
        for(int j = posz; j > 0; j -= j & (-j))
            m = max(m, aib[i][j]);
    return m;
}
void ers(int posy, int posz)
{
    for(int i = posy; i <= n; i += i & (-i))
        for(int j = posz; j <= n; j += j & (-j))
            aib[i][j] = 0;
}
int main()
{
    fin >> n >> t;
    while(t--)
    {
        for(int i = 1; i <= n; ++ i)
        {
            fin >> v[i].x >> v[i].y >> v[i].z;
        }
        sort(v + 1, v + n + 1, cmp);
        ans = 0;
        for(int i = 1; i <= n; ++ i)
        {
            int sol = query(v[i].y - 1, v[i].z - 1) + 1;
            update(v[i].y, v[i].z, sol);
            ans = max(ans, sol);
        }
        fout << ans << '\n';
        for(int i = 1; i <= n; ++ i)
            ers(v[i].y, v[i].z);
    }
    return 0;
}