Cod sursa(job #1170316)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 13 aprilie 2014 10:42:23
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("cutii.in");
ofstream g("cutii.out");
int T,N;
struct Element{
int x;
int y;
int z;
} Array[3505];
int AIB[3505][3505];
int Test[3505][3505];
int Vcar[3505],Vcar2[3505],number;
inline bool compare(Element a,Element b)
{
    return a.x<b.x;
}
void initialize()
{
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)
            Test[i][j]=-1;
}
void Update(int x,int y,int value)
{
    int i,j;
    for(i=x;i<=N;i+=i&(-i))
        for(j=y;j<=N;j+=j&(-j))
        {
            if(Test[i][j]==T)
                AIB[i][j]=max(AIB[i][j],number);
            else
            {
                Test[i][j]=T;
                AIB[i][j]=number;
            }
        }
}
int Query(int x,int y)
{
    int i,j,result=0;
    for(i=x;i>0;i-=i&(-i))
        for(j=y;j>0;j-=j&(-j))
        {
            if(Test[i][j]==T)
                result=max(result,AIB[i][j]);
        }
    return result;
}
void Browse()
{
    int Max=0;
    for(int i=1;i<=N;i++)
    {
        number=Query(Array[i].y,Array[i].z)+1;
        Update(Array[i].y,Array[i].z,1);
        Max=max(Max,number);
    }
    g<<Max<<"\n";
}
void Read()
{
    int i;
    for(i=1;i<=N;i++)
        f>>Array[i].x>>Array[i].y>>Array[i].z;
    sort(Array+1,Array+N+1,compare);
    for(i=1;i<=N;i++)
        Vcar[Array[i].y]=Array[i].z,Vcar2[Array[i].y]=i;
}
int main()
{
    int i,j;
    f>>N>>T;
    while(T--)
    {
        Read();
        Browse();
    }
    return 0;
}