Cod sursa(job #1342962)

Utilizator ovidiuz98Zamfir Ovidiu ovidiuz98 Data 14 februarie 2015 18:30:41
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#include <cstring>
#include <algorithm>
#define DIM 3502
#define bit(x) (x&-x)
using namespace std;

ifstream fin("cutii.in");
ofstream fout("cutii.out");
int N,T,L[DIM],solmax,A[DIM][DIM];
struct cutie{
    int first;
    int second;
    int third;
};
cutie v[DIM];
int cmp(const cutie a,const cutie b){
    return a.first<b.first;
}
void update(int x,int y,int val){
    for(int i=x;i<=N;i+=bit(i))
        for(int j=y;j<=N;j+=bit(j))
            A[i][j]=max(A[i][j],val);
}
int query(int a,int b){
    int sol=0;
    for(int i=a;i>=1;i-=bit(i))
        for(int j=b;j>=1;j-=bit(j))
            sol=max(sol,A[i][j]);
    return sol;
}
void init(){
    for(int i=1;i<=N;i++)
        for(int j=v[i].second;j<=N;j+=bit(j))
            for(int d=v[i].third;d<=N;d+=bit(j))
                A[i][j]=0;
}
int main(){
    fin>>N>>T;
    while(T--){
        for(int i=1;i<=N;i++)
            fin>>v[i].first>>v[i].second>>v[i].third;
        sort(v+1,v+N+1,cmp);
        solmax=0;
        for(int i=1;i<=N;i++){
            int maxim=1+query(v[i].second-1,v[i].third-1);
            update(v[i].second,v[i].third,maxim);
            solmax=max(maxim,solmax);
        }
        fout<<solmax<<"\n";
        init();
    }
    fin.close();fout.close();
    return 0;
}