Cod sursa(job #544638)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 1 martie 2011 21:03:58
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include<cstdio>
#include<algorithm>
#define sh short int
using namespace std;
struct pct
{
  sh x,y,z;
};
const sh N=3505;
pct a[N];
sh n,aib[N][N];
bool comp(const pct &A,const pct &B)
{
  return A.x<B.x;
}
void read()
{
  for(sh i=1;i<=n;i++)
    scanf("%hd%hd%hd",&a[i].x,&a[i].y,&a[i].z);
}
void init()
{
  for(sh i=1;i<=n;i++)
    for(sh j=1;j<=n;j++)
      aib[i][j]=0;
  sort(a+1,a+n+1,comp);
}
inline sh bit(sh x)
{
  return (x&(-x));
}
inline sh max(sh x,sh y)
{
  return x>y?x:y;
}
inline sh query(sh x,sh y)
{
  sh val=0,yi=y;
  while(x)
  {
    y=yi;
    while(y)
    {
      val=max(val,aib[x][y]);
      y-=bit(y);
    }
    x-=bit(x);
  }
  return val;
}
inline void update(sh x,sh y,sh val)
{
  sh yi=y;
  while(x<=n)
  {
    y=yi;
    while(y<=n)
    {
      aib[x][y]=max(aib[x][y],val);
      y+=bit(y);
    }
    x+=bit(x);
  }
}
void solve()
{
  sh aux,sol=0;
  for(sh i=1;i<=n;i++)
  {
    aux=query(a[i].y,a[i].z);
    aux++;
    sol=max(sol,aux);
    update(a[i].y,a[i].z,aux);
  }
  printf("%hd\n",sol);
}
int main()
{
  freopen("cutii.in","r",stdin);
  freopen("cutii.out","w",stdout);
  sh t;
  scanf("%hd%hd",&n,&t);
  while(t--)
  {
    read();
    init();
    solve();
  }
  return 0;
}