Cod sursa(job #979795)

Utilizator Corneliu10Dumitru Corneliu Corneliu10 Data 2 august 2013 19:35:46
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <algorithm>
 
#define In "cutii.in"
#define Out "cutii.out"
#define Nmax 3502
#define f(x) F[x]
#define max(a,b) ((a)>(b)?(a):(b))
 
using namespace std;
 
struct str
{
    int x,y,z;
    bool operator <(const str &A)const
    {
        return x<A.x;
    }
};
 
str A[Nmax];
short N,F[Nmax],aib[Nmax][Nmax];
ifstream in(In);
ofstream out(Out);
 
inline void Update(const int x,const int y,const short value)
{
    int i,j;
    for(i = x;i <= N;i += f(i))
        for(j = y;j <= N;j += f(j))
            aib[i][j] = max(aib[i][j],value);
}
inline void Init(const int x,const int y)
{
    int i,j;
    for(i = x;i <= N;i += f(i))
        for(j = y;j <= N;j += f(j))
                aib[i][j] = 0;
}
inline int Query(const int x,const int y)
{
    short i, j, value = 0;
    for(i = x; i; i -= f(i))
        for(j = y; j;j -= f(j))
                value = max(aib[i][j],value);
    return value;
}
 
inline void Read()
{
    for(int i=1;i<=N;++i)
        scanf("%d %d %d",&A[i].x,&A[i].y,&A[i].z);
}
 
inline int Solve()
{
    int i,best = 0, x;
    sort(A+1,A+N+1);
    for(i = 1;i <= N; ++i)
    {
        x = 1 + Query(A[i].y-1,A[i].z-1);
        Update(A[i].y,A[i].z,x);
        best = max(best,x);
    }
    for(i = 1;i <= N; ++i)
        Init(A[i].y,A[i].z);
    return best;
}
inline void Calculate()
{
    int i;
    for(i=1;i<=N;++i)
        F[i] = (i& -i);
}
 
int main()
{
    int T;
    freopen(In,"r",stdin);
    freopen(Out,"w",stdout);
    scanf("%hd %d",&N,&T);
    for(Calculate(); T ;--T)
    {
        Read();
        printf("%d\n",Solve());
    }
    return 0;
}