Cod sursa(job #643483)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 3 decembrie 2011 19:16:13
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 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];

#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);
}

void goleste(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]=0;
}

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;

}

int main()
{
    short int i,sol;
	freopen(file_in,"r",stdin);
    freopen(file_out,"w",stdout);

    scanf("%hd %hd\n",&n,&Q);
    while(Q--)
    {
        sol=0;
        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);
       }
	   
		for (i=1;i<=n;++i)
			 goleste(p[i].y,p[i].z,0);

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

    }
	
	return 0;
}