Cod sursa(job #1108066)

Utilizator romircea2010FMI Trifan Mircea Mihai romircea2010 Data 15 februarie 2014 13:04:02
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <fstream>
#include <cstring>
#include <algorithm>

using namespace std;

const int NMax = 3510, TMax = 110;

int n, t;
struct cutie
{
    int x, y, z;
    cutie(){x = y = z = 0;}
    cutie(const int _x, const int _y, const int _z)
    {
        x = _x, y = _y, z = _z;
    }
    bool operator < (const cutie &other) const
    {
        return x < other.x;
    }
};
cutie a[NMax];
int aib[NMax][NMax];
int dp[NMax];

inline int Query(const int x, const int y)
{
    int ret = 0;
    for (int i = x; i > 0; i -= (i & -i))
        for (int j = y; j > 0; j -= (j & -j))
            ret = max(ret, aib[i][j]);
    return ret;
}

inline void Update(const int x, const int y, const int val)
{
    for (int i = x; i <= n; i += (i & -i))
        for (int j = y; j <= n; j += (j & -j))
            aib[i][j] = max(aib[i][j], val);
}

inline void Memset(const int x, const int y)
{
    for (int i = x; i <= n; i += (i & -i))
        for (int j = y; j <= n; j += (j & -j))
            aib[i][j] = 0;
}

int main()
{
    ifstream f ("cutii.in");
    ofstream g ("cutii.out");
    f>>n>>t;
    while(t--)
    {
        for (int i = 1; i<=n; ++i)
        {
            int x, y, z; f>>x>>y>>z;
            a[i] = cutie(x, y, z);
        }
        sort(a+1, a+n+1);
        int ans = 0;
        for (int i = 1; i<=n; ++i)
        {
            dp[i] = 1 + Query(a[i].y - 1, a[i].z - 1);
            ans = max(ans, dp[i]);
            Update(a[i].y, a[i].z, dp[i]);
        }
        g<<ans<<"\n";
        if (t)
            for (int i = 1; i<=n; ++i)
                Memset(a[i].y, a[i].z);
    }
    f.close();
    g.close();
    return 0;
}