Cod sursa(job #3196506)

Utilizator not_anduAndu Scheusan not_andu Data 24 ianuarie 2024 09:41:32
Problema Cutii Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("03")

using namespace std;

#define INFILE "cutii.in"
#define OUTFILE "cutii.out"

const int MINIM = -1;

struct Box {

    int x, y, z;

    Box() : x(0), y(0), z(0) {}
    Box(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}

    bool operator<(const Box &other) const {
        return (this -> x < other.x);
    }

};

struct Fenwick2D {

private:
    int n;
    vector<vector<int> > bit;

public:
    
    Fenwick2D() : n(0) {}
    Fenwick2D(int _n) : n(_n) {
        bit.resize(n + 1, vector<int>(n + 1));
        for(int i = 0; i <= n; ++i)
            for(int j = 0; j <= n; ++j)
                bit[i][j] = 0;
    }

    int query(int x, int y){
        int ans = MINIM;
        for(int i = x; i > 0; i -= (i & -i)){
            for(int j = y; j > 0; j -= (j & -j)){
                ans = max(ans, bit[i][j]);
            }
        }
        return ans;
    }

    void update(int x, int y, int value){
        for(int i = x; i <= n; i += (i & -i)){
            for(int j = y; j <= n; j += (j & -j)){
                bit[i][j] = max(bit[i][j], value);
            }
        }
    }

};

int n;

void solve(){

    int ans = 0;
    vector<Box> v(n);
    Fenwick2D bit(n);

    for(int i = 0; i < n; ++i){
        int x, y, z; cin >> x >> y >> z; v[i] = Box(x, y, z);
    }

    sort(v.begin(), v.end());

    for(int i = 0; i < n; ++i){
        auto aux = bit.query(v[i].y, v[i].z) + 1;
        bit.update(v[i].y, v[i].z, aux);
        ans = max(ans, aux);
    }

    cout << ans << '\n';

}

int main(){
    ios_base::sync_with_stdio(false);
    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);
    cin.tie(0), cout.tie(0);
    int tests; cin >> n >> tests;
    while(tests--) solve();
    return 0;
}