Cod sursa(job #1205751)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 7 iulie 2014 23:13:16
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <cstdio>
#include <vector>
#include <algorithm>

#define Nmax 3505

using namespace std;
int N,T,tst;
short tip[Nmax][Nmax];

inline int maxi(int a,int b){
    if(a<b) return b;
    return a;
}

struct cutie{
    int x,y,z;
};
class cmp{
public:
    bool operator ()(const cutie &c1,const cutie &c2) const
    {
        if( c1.x < c2.x ) return 1;
        if( c1.x == c2.x && c1.y < c2.y ) return 1;
        if( c1.x == c2.x && c1.y == c2.y && c1.z < c2.z) return 1;
        return 0;
    }
};
vector<cutie> v;

class BinaryIndexedTree{
private:
    short aib[Nmax][Nmax];
    short maxim;
public:
    int Querry(int x,int y){
        maxim = 0;
        for(int i = x; i; i -= ((i^(i-1))&i))
            for(int j = y; j; j -= ((j^(j-1))&j))
                if(tip[i][j] == tst)
                    maxim = maxi(maxim,aib[i][j]);
        return maxim;
    }
    void Update(int x,int y,int value)
    {
        for(int i = x; i <= N; i += ((i^(i-1))&i))
            for(int j = y; j <= N; j += ((j^(j-1))&j))
                if(tip[i][j] == tst)
                    aib[i][j] = maxi(aib[i][j],value);
                else
                {
                    aib[i][j] = value;
                    tip[i][j] = tst;
                }
    }

};
BinaryIndexedTree Aib;

void solve()
{
    int nr,best = 0;
    for(int i = 0; i < v.size(); ++i)
    {
        nr = Aib.Querry(v[i].y - 1,v[i].z - 1) + 1;
        best = maxi(best,nr);
        Aib.Update(v[i].y,v[i].z,nr);
    }
    printf("%d\n",best);
}

void read()
{
    scanf("%d%d",&N,&T);
    cutie c;
    for(tst = 1; tst <= T; ++tst)
    {
        for(int i = 1; i <= N; ++i)
        {
            scanf("%d%d%d",&c.x,&c.y,&c.z);
            v.push_back(c);
        }
        sort(v.begin(),v.end(),cmp());
        solve();
        v.clear();
    }
}

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

    read();

    return 0;
}