Cod sursa(job #1294940)

Utilizator RaduVisanRadu Visan RaduVisan Data 18 decembrie 2014 15:41:08
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstring>
using namespace std;

const int NMAX = 4000;

int T, N, Aib[NMAX][NMAX], Dp[NMAX];
pair<int, pair<int, int> > Box[NMAX];

int LSB(int X)
{
    return (X & (X - 1)) ^ X;
}

void Update(int X, int Y, int Val)
{
    for(int i = X; i <= N; i += LSB(i))
        for(int j = Y; j <= N; j += LSB(j))
            Aib[i][j] = max(Aib[i][j], Val);
}

int Query(int X, int Y)
{
    int Ans = 0;
    for(int i = X; i; i -= LSB(i))
        for(int j = Y; j; j -= LSB(j))
            Ans = max(Ans, Aib[i][j]);
    return Ans;
}

const int MaxB = 8192;
char Buf[MaxB];
int Ptr;

inline int Get()
{
    int Nr = 0;
    while(!isdigit(Buf[Ptr]))
        if(++ Ptr >= MaxB)
            fread(Buf, 1, MaxB, stdin), Ptr = 0;

    while(isdigit(Buf[Ptr]))
    {
        Nr = Nr * 10 + Buf[Ptr] - '0';
        if(++Ptr >= MaxB)
            fread(Buf, 1, MaxB, stdin), Ptr = 0;
    }
    return Nr;
}

int main()
{
    freopen("cutii.in", "r", stdin);
    freopen("cutii.out", "w", stdout);
    fread(Buf, 1, MaxB, stdin);

    N = Get();
    T = Get();
    for(; T; T --)
    {
        for(int i = 1; i <= N; ++ i)
            for(int j = 1; j <= N; ++ j)
                Aib[i][j] = 0;
        for(int i = 1; i <= N; ++ i)
            Dp[i] = 0;

        for(int i = 1; i <= N; ++ i)
        {
            Box[i].first = Get();
            Box[i].second.first = Get();
            Box[i].second.second = Get();
        }

        sort(Box + 1, Box + N + 1);

        for(int i = 1; i <= N; ++ i)
        {
            Dp[i] = 1 + Query(Box[i].second.first, Box[i].second.second);
            Update(Box[i].second.first, Box[i].second.second, Dp[i]);
        }

        int Ans = 0;
        for(int i = 1; i <= N; ++ i) Ans = max(Ans, Dp[i]);

        printf("%i\n", Ans);
    }
}