Cod sursa(job #493458)

Utilizator AndreyPAndrei Poenaru AndreyP Data 18 octombrie 2010 12:55:47
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 3505
struct cutie {
    short x,y,z;
};

short n;
cutie v[N];
short a[N][N];

inline short nrb(int x) {
    return ((x&(x-1))^x);
}

inline void citire() {
    for(short i=1; i<=n; ++i)
        scanf("%hd%hd%hd",&v[i].x,&v[i].y,&v[i].z);
}

inline void update(int x,int y,int val) {
    for(short i=x; i<=n; i+=nrb(i)) {
        for(short j=y; j<=n; j+=nrb(j))
            a[i][j] = ( (a[i][j]<val) ? val : a[i][j] );
    }
}

inline short query(int x,int y) {
    short ret = 0;
    for(short i=x; i>0; i-=nrb(i)) {
        for(short j=y; j>0; j-=nrb(j))
            ret = ( (ret<a[i][j]) ? a[i][j] : ret );
    }
    return ret;
}

bool cmpx(const cutie &x,const cutie &y) {
    return (x.x<y.x);
}

bool cmpy(const cutie &x,const cutie &y) {
    return (x.y<y.y);
}

bool cmpz(const cutie &x,const cutie &y) {
    return (x.z<y.z);
}

inline void rezolva() {
    short rez = 0,aux;

    memset(a,0,sizeof(a));
    sort(v+1,v+n+1,cmpx);
    for(short i=1; i<=n; ++i) {
        aux = query(v[i].y,v[i].z);
        ++aux;
        update(v[i].y,v[i].z,aux);
        if(aux>rez)
            rez = aux;
    }

    memset(a,0,sizeof(a));
    sort(v+1,v+n+1,cmpy);
    for(short i=1; i<=n; ++i) {
        aux = query(v[i].x,v[i].z);
        ++aux;
        update(v[i].x,v[i].z,aux);
        if(aux>rez)
            rez = aux;
    }

    memset(a,0,sizeof(a));
    sort(v+1,v+n+1,cmpz);
    for(short i=1; i<=n; ++i) {
        aux = query(v[i].x,v[i].y);
        ++aux;
        update(v[i].x,v[i].y,aux);
        if(aux>rez)
            rez = aux;
    }

    printf("%hd\n",rez);
}
int main() {
    freopen("cutii.in","r",stdin);
    freopen("cutii.out","w",stdout);

    short T;
    scanf("%hd%hd",&n,&T);
    for(; T!=0; --T) {
        citire();
        rezolva();
    }

    return 0;
}