Cod sursa(job #187739)

Utilizator fogabFodor Gabor fogab Data 5 mai 2008 12:05:08
Problema Cutii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.9 kb
//Quadtrees

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>

#define LL long long
#define ULL unsigned long long
#define PB push_back

using namespace std;

struct P{
   int X,Y,Z;
};

struct Tree{
   int val,Y,Z;
   Tree *A1,*A2,*B1,*B2;
   
   Tree(int r1,int r2,int r3) {
            val = r1;
            Y = r2;
            Z = r3;                        
            A1 = NULL;
            A2 = NULL;
            B1 = NULL;
            B2 = NULL;            
         }   
};

vector <P> A;
vector<P>::iterator it;

void get(P *who,Tree *&cur){
     if (cur == NULL) return;      
     if ((who->Y > cur -> Y) && (who->Z > cur -> Z)){
        if (cur->val> who->X) who->X = cur->val;
        get(who,cur->A1);
        get(who,cur->A2);
        get(who,cur->B2);                
        //cout << who->X<<" ";
        return;
     }  
     if ((who->Y > cur -> Y) && (who->Z < cur -> Z)){
        get(who,cur->B1);
        get(who,cur->B2);
        return;
     }
     if ((who->Y < cur -> Y) && (who->Z > cur -> Z)){
        get(who,cur->A1);
        get(who,cur->B1);
        return;
     }                 
     if ((who->Y < cur -> Y) && (who->Z < cur -> Z)){
        get(who,cur->B1);
        return;
     }
     return;
}     
     
void set(P *who,Tree *&cur){
     if (cur == NULL){
        cur = new Tree( who->X, who->Y, who->Z);
        return;
        //cout << who->Y << " " << who->Z << " " << cur-> val << "\n";
     }           
     if ((who->Y > cur -> Y) && (who->Z > cur -> Z)){
        //cout << who->X<<" ";
        set(who,cur->A2);
        return;
     }  
     if ((who->Y > cur -> Y) && (who->Z < cur -> Z)){       
        set(who,cur->B2);
        return;
     }
     if ((who->Y < cur -> Y) && (who->Z > cur -> Z)){
        set(who,cur->A1);
        return;
     }
     if ((who->Y < cur -> Y) && (who->Z < cur -> Z)){
        set(who,cur->B1);
        return;
     }
}    

int comp(P a,P b){
    return (a.X) < (b.X);
    }

int main(void){

   ifstream in("cutii.in");
   ofstream out("cutii.out"); 

   int N,T,i,max,sol;
   Tree *root;

   in >> N >> T;
   P np;

   while (T){
      
      for (i=0;i<N;i++){    
          in >> np.X >> np.Y >> np.Z;            
          A.PB(np);
          }
          
      sort(A.begin(),A.end(),comp);    
      
      max = 0;
      root = NULL; //not so nice :D
      
      for (it=A.begin();it<A.end();it++){    
          np = *it;          
          np.X = 0;
          get(&np,root);
          np.X++;
          set(&np,root);
          //cout << "|" <<  np.X << "|\n";
          if (np.X>max) max = np.X;
          }      
          
      out << max << "\n";
      A.clear();    
      T--;      
   }
   //cin >> N;
   in.close();
   out.close();

   return 0;
}