Cod sursa(job #1240379)

Utilizator assa98Andrei Stanciu assa98 Data 11 octombrie 2014 10:50:37
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

ifstream fin("cutii.in");
ofstream fout("cutii.out");

struct tr {
    int x, y, z;
    tr(int _x = 0, int _y = 0, int _z = 0) {
        x = _x;
        y = _y;
        z = _z;
    }
    inline bool operator < (const tr &a) const {
        return z < a.z;
    }
};

const int MAX_N = 3500;

int aib[MAX_N + 10][MAX_N + 10];

void update(int x, int cy, int val) {
    for(; x <= MAX_N; x += (x & (-x))) {
        for(int y = cy; y <= MAX_N; y += (y & (-y))) {
            aib[x][y] = max(aib[x][y], val);
        }
    }
}

int query(int x, int cy) {
    int ans = 0;
    for(; x > 0; x -= (x & (-x))) {
        for(int y = cy; y > 0; y -= (y & (-y))) {
            ans = max(ans, aib[x][y]);
        }
    }
    return ans;
}

void set(int x, int cy) {
    for(; x <= MAX_N; x += (x & (-x))) {
        for(int y = cy; y <= MAX_N; y += (y & (-y))) {
            aib[x][y] = 0;
        }
    }
}

int solve(int n) {
    vector<tr> v;
    
    for(int i = 1; i <= n; i++) {
        int x, y, z;
        fin >> x >> y >> z;
        v.push_back(tr(x, y, z));
    }
    sort(v.begin(), v.end());
    
    int ans = 0;
    for(auto it : v) {
        int d = query(it.x - 1, it.y - 1) + 1;
        update(it.x, it.y, d);
        ans = max(ans, d);
    }
    
    for(auto it : v) {
        set(it.x, it.y);
    }

    return ans;
}

int main() {
    int n, t;
    fin >> n >> t;
    
    for(int i = 1; i <= t; i++) {
        fout << solve(n) << '\n';
    }

    return 0;
}