Cod sursa(job #2644297)

Utilizator andu2006Alexandru Gheorghies andu2006 Data 24 august 2020 10:51:01
Problema Cutii Scor 40
Compilator cpp-64 Status done
Runda prbd2 Marime 1.5 kb
#include<bits/stdc++.h>

using namespace std;
ifstream fin("cutii.in");
ofstream fout("cutii.out");

const short NMAX = 3501;
short aib[NMAX][NMAX],n;
struct cutie{short x, y, z;};
bool cmp(cutie a, cutie b){
    return a.x<b.x || (a.x==b.x && (a.y<b.y || (a.y==b.y && a.z<b.z)));
}
short lastBit(short num){
    return num&-num;
}
void insert2(short pos1, short pos2, short val){
    while(pos2<NMAX){
        aib[pos1][pos2]=max(aib[pos1][pos2],val);
        pos2+=lastBit(pos2);
    }
}
void insert(short pos1,short pos2, short val){
    while(pos1<NMAX){
        insert2(pos1,pos2,val);
        pos1+=lastBit(pos1);
    }
}
short getmax2(short pos1,short pos2){
    short Max=0;
    while(pos2){
        Max=max(Max,aib[pos1][pos2]);
        pos2-=lastBit(pos2);
    }
    return Max;
}
short getmax(short pos1,short pos2){
    short Max=0;
    while(pos1){
        Max=max(Max,getmax2(pos1,pos2));
        pos1-=lastBit(pos1);
    }
    return Max;
}
void tc(){
    short dp[NMAX]{},ans=0;
    cutie v[NMAX];
    for(short i=0;i<n;i++){
        fin>>v[i].x>>v[i].y>>v[i].z;
        for(short j=0;j<n;j++)
            aib[i+1][j+1]=0;
    }
    sort(v,v+n,cmp);
    for(short i=0;i<n;i++){
        dp[i]=getmax(v[i].y-1,v[i].z-1)+1;
        insert(v[i].y,v[i].z,dp[i]);
        ans=max(ans,dp[i]);
    }
    fout<<ans<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false); fin.tie(0); fout.tie(0);
    short k; fin>>n>>k; while(k--)
        tc();
    return 0;
}