Cod sursa(job #597899)

Utilizator cosmyoPaunel Cosmin cosmyo Data 23 iunie 2011 22:17:08
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef short int sh;
const int N = 3505;
struct tip{sh x, y, z;} a[N];
sh d[N][N], n, t;

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

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

inline void update(sh x, sh y, sh val) {
    sh 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 sh query(sh x, sh y) {
    sh 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");
    sh i, best, j;
    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);
        sh max = 0;
        for(i = 0; i <= n; ++i)
            for(j = 0; j <= n; ++j)
                d[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;
}