Cod sursa(job #597884)

Utilizator cosmyoPaunel Cosmin cosmyo Data 23 iunie 2011 21:03:33
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 3505;
struct tip{int x, y, z;} a[N];
int d[N][N], n, t;

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

int cmp1(tip a, tip b) {
    return a.z < b.z;
}

inline int update(int x, int y, int val) {
    int y1;
    while(x <= n){
        y1 = y;
        while(y1 <= n) {
            if(d[x][y1] < val)
                d[x][y1] = val;
            y1 += lsb(y);
        }
        x += lsb(x);
    }
}

inline int query(int x, int y) {
    int max = 0, y1;
    while(x) {
        y1 = y;
        while(y1) {
            if(d[x][y1] > max)
                max = d[x][y1];
            y1 -= lsb(y);
        }
        x -= lsb(x);
    }
    return max;
}

int main() {
    freopen("cutii.in", "r", stdin);
    freopen("cutii.out", "w", stdout);
    int i, best;
    scanf("%d %d",&n, &t);
    for(; t; --t){
        for(i = 1; i <= n; ++i)
            scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].z);
        sort(a + 1, a + n + 1, cmp1);
        int max = 0;
        memset(d, 0, sizeof(d));
        for(i = 1; i <= n; ++i) {
            best = query(a[i].x - 1, a[i]. y - 1);
            update(a[i].x, a[i].y, best + 1);
            if(best + 1 > max)
                max = best + 1;
        }

        printf("%d\n", max);
    }

    return 0;
}