Cod sursa(job #3166011)

Utilizator AlexSerban21Serban Alexandru AlexSerban21 Data 7 noiembrie 2023 14:22:13
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("cutii.in");
ofstream fout ("cutii.out");
short int q,aib[3501][3501],n,maxc,sol,i,t;
struct cutie
{
    short x,y,z;
};
cutie v[3501];
short int query (short int a,short int b)
{
    short int sol=0;
    for (short int i=a; i>0; i-=(i&-i))
    {
        for (short int j=b; j>0; j-=(j&-j))
        {
            if (aib[i][j]>sol)
                sol=aib[i][j];
        }
    }
    return sol;
}
void update (short int a,short int b,short int val)
{
    for (short int i=a; i<=n; i+=(i&-i))
    {
        for (short int j=b; j<=n; j+=(j&-j))
        {
            if (val>aib[i][j])
                aib[i][j]=val;
        }
    }
}
void update_del (short int a,short int b)
{
    for (short int i=a; i<=n; i+=(i&-i))
    {
        for (short int j=b; j<=n; j+=(j&-j))
            aib[i][j]=0;
    }
}
short int cmp (const cutie &a,const cutie &b)
{
    return a.x<b.x;
}
int main ()
{
    fin>>n>>t;
    for (q=1; q<=t; q++)
    {
        for (i=1; i<=n; i++)
            fin>>v[i].x>>v[i].y>>v[i].z;
        sort (v+1,v+n+1,cmp);
        maxc=0;
        for (i=1; i<=n; i++)
        {
            sol=query (v[i].y-1,v[i].z-1);
            update (v[i].y,v[i].z,sol+1);
            if (sol+1>maxc)
                maxc=sol+1;
        }
        for (i=1; i<=n; i++)
            update_del (v[i].y,v[i].z);
        fout<<maxc<<"\n";
    }
    return 0;
}