Cod sursa(job #2182158)

Utilizator popabogdanPopa Bogdan Ioan popabogdan Data 22 martie 2018 10:38:20
Problema Cutii Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <bits/stdc++.h>

#define lsb(x) ((x ^ (x - 1)) & x)
#define Nmax 3505

using namespace std;

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

struct box
{
    int x, y, z;
};

int T, N;
box B[Nmax];
int Y[Nmax], Z[Nmax];
int srt[Nmax];
int dp[Nmax];
stack <int> toUpdate;
int bit[Nmax][Nmax];

bool qx(box A, box B)
{
    return make_pair(make_pair(A.x, A.y), A.z) < make_pair(make_pair(B.x, B.y), B.z);
}

void update(int x, int y, int val)
{
    for(int i = x; i <= N; i += lsb(i))
        for(int j = y; j <= N; j += lsb(j))
            bit[i][j] = max(bit[i][j], val);
}

int query(int x, int y)
{
    int ret = 0;
    for(int i = x; i >= 1; i -= lsb(i))
        for(int j = y; j >= 1; j -= lsb(j))
            ret = max(ret, bit[i][j]);
    return ret;
}

void normalize(int *A)
{
    for(int i = 1; i <= N; i++)
        srt[i] = A[i];
    sort(srt + 1, srt + N + 1);
    int L = unique(srt + 1, srt + N + 1) - srt - 1;
    for(int i = 1; i <= N; i++)
        A[i] = lower_bound(srt + 1, srt + L + 1, A[i]) - srt;
}

int main()
{
    fin >> N >> T;
    while(T--)
    {
        for(int i = 1; i <= N; i++)
        {
            fin >> B[i].x >> B[i].y >> B[i].z;
            Y[i] = B[i].y;
            Z[i] = B[i].z;
        }
        normalize(Y);
        normalize(Z);
        for(int i = 1; i <= N; i++)
            B[i].y = Y[i], B[i].z = Z[i];
        sort(B + 1, B + N + 1, qx);
        int ans = 0;
        B[0].x = -1;
        memset(bit, 0, sizeof(bit));
        while(!toUpdate.empty())
            toUpdate.pop();
        for(int i = 1; i <= N; i++)
        {
            if(B[i].x != B[i - 1].x)
                while(!toUpdate.empty())
                {
                    int idx = toUpdate.top();
                    update(B[idx].y, B[idx].z, dp[idx]);
                    toUpdate.pop();
                }
            dp[i] = 1 + query(B[i].y - 1, B[i].z - 1);
            ans = max(ans, dp[i]);
            toUpdate.push(i);
        }
        fout << ans << "\n";
    }
    return 0;
}