Cod sursa(job #2643992)

Utilizator De_Azi_Ne_DopamAlex Ardelean De_Azi_Ne_Dopam Data 22 august 2020 19:00:00
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream in("cutii.in");
ofstream out("cutii.out");
vector <int> Ys[3501];
vector <int> aib[3501];
struct mazan
{
    int x,y,z;
} v[3501];
int n;
int lsb(int x)
{
    return x&-x;
}
bool cmp(mazan a,mazan b)
{
    return a.x<b.x;
}
void upd(int x,int y,int val)
{
    for(; x<=n; x+=lsb(x))
    {
        int normy=1+(lower_bound(Ys[x].begin(),Ys[x].end(),y)-Ys[x].begin());
        for(; normy<=Ys[x].size(); normy+=lsb(normy))
            aib[x][normy]=max(aib[x][normy],val);
    }
}
int ans(int x,int y)
{
    int max1=0;
    for(; x>0; x-=lsb(x))
    {
        int normy=1+(upper_bound(Ys[x].begin(),Ys[x].end(),y)-Ys[x].begin())-1;
        for(; normy>0; normy-=lsb(normy))
            max1=max(max1,aib[x][normy]);
    }
    return max1;
}
void preupdate(int x,int y)
{
    for(; x<=n; x+=lsb(x))
        Ys[x].push_back(y);
}
int main()
{
    int t,i,max1,x;
    in>>n>>t;
    while(t--)
    {
        max1=0;
        for(i=1; i<=n; i++)
        {
            in>>v[i].x>>v[i].y>>v[i].z;
            preupdate(v[i].y,v[i].z);
        }
        for(i=1; i<=n; i++)
        {
            aib[i].resize(Ys[i].size()+1,0);
            sort(Ys[i].begin(),Ys[i].end());
        }
        sort(v+1,v+n+1,cmp);
        for(i=1; i<=n; i++)
        {
            x=ans(v[i].y,v[i].z)+1;
            max1=max(max1,x);
            upd(v[i].y,v[i].z,x);
        }
        out<<max1<<'\n';
        for(i=1; i<=n; i++)
        {
            Ys[i].clear();
            aib[i].clear();
        }
    }
    return 0;
}