Cod sursa(job #2340978)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 11 februarie 2019 13:46:20
Problema Cutii Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.91 kb
//O(T*(N^2+NlogN)))
//putin cam brut
#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){

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

    int query(int x, int y){

        int sum=0;
        for(int i=x;i>0;desc(i))
            for(int j=y;j>0;desc(j))
                sum+=A[i][j];
        return sum;
    }
};

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);
    vector <int> w(N);
    vector <int> lg(N);
    while(T--){

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

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

            w[i]=BIT.query(v[i].y,v[i].z);
            int pos=i;
            for(;i<N and v[pos].x==v[i].x;i++){

                w[i]=w[pos];
                BIT.update(v[i].y,v[i].z);
            }
            --i;
        }

        lg[0]=1;
        int L,best=1;
        for(int i=1;i<N;i++){

            L=0;
            for(int j=0;j<i;j++)
                if(w[j]<w[i])
                    L=max(L,lg[j]);
            lg[i]=L+1;
            best=max(best,lg[i]);
        }

        g<<best<<'\n';
    }

    return 0;
}