Cod sursa(job #1156861)

Utilizator PatrikStepan Patrik Patrik Data 28 martie 2014 08:09:38
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    #define NMAX 3501
    int N , T  , AIB[NMAX][NMAX] , best , maxx;
    struct entry
    {
        int x , y , z;
    }V[NMAX];
    bool f(entry A , entry B)
    {
        return A.z < B.z;
    }
    void update(int x , int y , int val);
    int query(int x , int y);
    void del(int x , int y);

    int main()
    {
        freopen("cutii.in" , "r" , stdin );
        freopen("cutii.out" , "w" , stdout );
        scanf("%d%d" , &N , &T );
        for(int k = 1 ; k <= T ; ++k )
        {
            for(int i = 1 ; i<= N ; ++i )
                scanf("%d%d%d" , &V[i].z , &V[i].x , &V[i].y);
            sort(V+1,V+N+1,f);
            update(V[1].x , V[1].y , 1);
            best = 1;
            for(int i = 2 ; i<= N  ; ++i )
            {
                maxx = 1;
                maxx = max(maxx,query(V[i].x-1,V[i].y-1)+1);
                best = max(best,maxx);
                update(V[i].x , V[i].y , maxx );
            }
            for(int i = 1 ; i<= N ; ++i )
                del(V[i].x , V[i].y);
            printf("%d\n" , best );
        }
        return 0;
    }

    void update(int x , int y , int val)
    {
        for(;x <= N ; x += x^(x&(x-1)))
            for(int col = y ; col <= N ; col+= col^(col&(col-1)))
                AIB[x][col] =max( val , AIB[x][col]);
    }

    int query(int x , int y)
    {
        int rez = 0;
        for(;x;x-=x^(x&(x-1)))
            for(int col = y;col;col-=col^(col&(col-1)))
                rez = max(AIB[x][col],rez);
        return rez;
    }

    void del(int x , int y)
    {
        for(;x<=N;x+=x^(x&(x-1)))
            for(int col = y ; col <= N; col+=col^(col&(col-1)))
                AIB[x][col] = 0;
    }