Cod sursa(job #998496)

Utilizator Mihai22eMihai Ionut Enache Mihai22e Data 17 septembrie 2013 10:31:59
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <fstream>
#include <algorithm>

#include <cstring>
using namespace std;

const int MAX_N = 3502;

struct box {
    int x, y, z;
};

int N, T;
int dp[MAX_N], A[MAX_N][MAX_N];
box v[MAX_N];

inline bool box_cmp(box a, box b) {
    return (a.x < b.x || (a.x == b.x && a.y < b.y) || (a.x == b.x && a.y == b.y && a.z < b.z));
}

inline void Update(int pos1, int pos2, int val) {
    while(pos1 <= N) {
        int y = pos2;
        while(y <= N) {
            A[pos1][y] = max(A[pos1][y], val);
            y += y^(y&(y-1));
        }
        pos1 += pos1^(pos1&(pos1-1));
    }
}

inline int Query(int pos1, int pos2) {
    int Max = 0;
    while(pos1) {
        int y = pos2;
        while(y) {
            Max = max(Max, A[pos1][y]);
            y -= y^(y&(y-1));
        }
    pos1 -= pos1^(pos1&(pos1-1));
    }

    return Max;
}

int main() {
    ifstream f("cutii.in");
    ofstream g("cutii.out");

    f >> N >> T;
    while(T--) {
        for(int i = 1; i <= N; ++i)
            f >> v[i].x >> v[i].y >> v[i].z;

        memset(A, 0, sizeof(A));
        sort(v+1, v+N+1, box_cmp);
        int sol = 0;
        for(int i = 1; i <= N; ++i) {
            dp[i] = Query(v[i].y-1, v[i].z-1) + 1;
            Update(v[i].y, v[i].z, dp[i]);
            sol = max(sol, dp[i]);
        }

        g << sol << "\n";
    }

    f.close();
    g.close();

    return 0;
}