Cod sursa(job #2349266)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 20 februarie 2019 12:34:30
Problema Cutii Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <bits/stdc++.h>

using namespace std;

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

int T,N;

class Fenwick_2D{

private:
    int N;
    int **A;

    void asc(int &x){

        x+=(x&(-x));
    }

    void desc(int &x){

        x-=(x&(-x));
    }

public:
    Fenwick_2D(const int &n){

        N=n;
        A=new int*[n+5]();
        for(int i=0;i<n+5;i++)
            A[i]=new int[n+5]();
    }

    void update(int x,int y,int val){

        for(int i=x;i<=N;asc(i))
            for(int j=y;j<=N;asc(j))
                A[i][j]=max(A[i][j],val);
    }

    int query(int x, int y){

        int maxx=0;
        for(int i=x;i>0;desc(i))
            for(int j=y;j>0;desc(j))
                maxx=max(A[i][j],maxx);
        return maxx;
    }

    void _clear(){

        for(int i=1;i<=N;i++)
            for(int j=1;j<=N;j++)
                A[i][j]=0;
    }
};

struct box{

    int x,y,z;
    bool operator < (const box &other) const{

        if(x==other.x){

            if(y==other.y)
                return z<other.z;
            return y<other.y;
        }
        return x<other.x;
    }
};

int main(){

    ios_base::sync_with_stdio(false);

    f>>N>>T;
    vector <box> v(N);
    Fenwick_2D BIT(N);
    while(T--){

        for(int i=0;i<N;i++)
            f>>v[i].x>>v[i].y>>v[i].z;
        sort(v.begin(),v.end());

        int ans=0;

        for(int i=0;i<N;i++){

            int val=BIT.query(v[i].y,v[i].z);
            BIT.update(v[i].y,v[i].z,val+1);
            ans=max(ans,val+1);
        }

        BIT._clear();

        g<<ans<<'\n';
    }

    return 0;
}