Cod sursa(job #734748)

Utilizator elfusFlorin Chirica elfus Data 14 aprilie 2012 19:49:55
Problema Cutii Scor 100
Compilator cpp Status done
Runda simulare_elf2 Marime 1.71 kb
#include <stdio.h>
#include <algorithm>
#define LMAX 3600

using namespace std;

struct box
{
    int x, y, z;
} x[LMAX];

int zero[LMAX], AIB[LMAX][LMAX];

inline bool comp (box A, box B)
{
    return A.z < B.z;
}

void set (int x0, int y0, int N)
{
    int i, j;

    for (i = x0; i <= N; i = i + zero[i])
        for (j = y0; j <= N; j = j + zero[j])
           AIB[i][j] = 0;
}

int Query (int x0, int y0)
{
    int i, j, best = 0;

    for (i = x0; i >= 1; i = i - zero[i])
        for (j = y0; j >= 1; j = j - zero[j])
            if (AIB[i][j] > best)
                best = AIB[i][j];

    return best;
}

void Upgrade (int x0, int y0, int val, int N)
{
    int i, j;

    for (i = x0; i <= N; i = i + zero[i])
        for (j = y0; j <= N; j = j + zero[j])
            if (AIB[i][j] < val)
                AIB[i][j] = val;
}

int main ()
{
    int N, i, pw = 0, Q, ans, now;

    freopen ("cutii.in", "r", stdin);
    freopen ("cutii.out", "w", stdout);

    scanf ("%d%d", &N, &Q);

    zero[1] = 1;
    for (i = 2; i <= N; i ++)
        if (i == (1 << (pw + 1)))
            zero[i] = i, pw ++;
        else
            zero[i] = zero[i - (1 << pw)];

    while (Q --)
    {
        for (i = 1; i <= N; i ++)
            scanf ("%d%d%d", &x[i].x, &x[i].y, &x[i].z);

        sort (x + 1, x + N + 1, comp);

        ans = 0;
        for (i = 1; i <= N; i ++)
        {
            now = Query (x[i].x, x[i].y) + 1;
            if (now > ans)
                ans = now;
            Upgrade (x[i].x, x[i].y, now, N);
        }

        printf ("%d\n", ans);

        for (i = 1; i <= N; i ++)
            set (x[i].x, x[i].y, N);
    }

    return 0;
}