Cod sursa(job #464468)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 20 iunie 2010 12:36:40
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;

#define file_in "cutii.in"
#define file_out "cutii.out"

#define nmax 3520

struct cutie
{
    short int x,y,z;
}
p[nmax];

short int n,Q;

short int aib[nmax][nmax];

void citire()
{
    freopen(file_in,"r",stdin);
    freopen(file_out,"w",stdout);

    scanf("%hd %hd\n",&n,&Q);

}

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

inline int max(short int a,short int b) { return a>b?a:b; }

int cmp(cutie a, cutie b)
{
    return a.x<b.x;
}

void baga(short int x,short int y,short int val)
{
    short int i,j;

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

short int afla(short int x,short int y)
{
    short int i,j;
    short int maxx=0;

    for (i=x;i;i-=lsb(i))
         for (j=y;j;j-=lsb(j))
              maxx=max(maxx,aib[i][j]);
     return maxx;

}

void solve()
{
    short int i,sol;
    while(Q--)
    {
        sol=0;
        memset(aib,0,sizeof(aib));
       for (i=1;i<=n;++i)
       {
           scanf("%hd %hd %hd\n", &p[i].x, &p[i].y, &p[i].z);
       }

       sort(p+1,p+n+1,cmp);

       for (i=1;i<=n;++i)
       {
            short int val=afla(p[i].y-1,p[i].z-1)+1;
            if (val>sol) sol=val;
            baga(p[i].y,p[i].z,val);
       }

       printf("%hd\n", sol);

    }
}

int main()
{
    citire();
    solve();

    fclose(stdin);
    fclose(stdout);

    return 0;
}