Cod sursa(job #597893)

Utilizator cosmyoPaunel Cosmin cosmyo Data 23 iunie 2011 22:13:21
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#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 void 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(y1);
        }
        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(y1);
        }
        x -= lsb(x);
    }
    return max;
}

int main() {
    ifstream fin("cutii.in");
    ofstream fout("cutii.out");
    int i, best;
    fin >> n >> t;
    for(; t; --t){
        for(i = 1; i <= n; ++i)
            fin >> a[i].x >> a[i].y >> a[i].z;
        sort(a + 1, a + n + 1, cmp1);
        int max = 0;
        for(i = 0; i <= n; ++i)
            for(j = 0; j <= n; ++j)
                a[i][j] = 0;
        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;
        }

        fout << max << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}