Cod sursa(job #747954)

Utilizator SchumiDumitru Andrei Georgian Schumi Data 12 mai 2012 09:29:06
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn = 3505;
struct cutii {
    int a;
    int b;
    int c;
};
cutii c[maxn];

int aib[maxn][maxn], n, t, sol, d[maxn];

int cmp(cutii a, cutii b)
{
    return a.a < b.a;
}

int lsb(int x)
{
    return (x & (x - 1)) ^ x;
}

int query(int a, int b)
{
    int i, j, sol = 0;
    for(i = a; i >= 1; i -= lsb(i))
        for(j = b; j >= 1; j -= lsb(j))
            sol = max(sol, aib[i][j]);
    return sol;
}

void update(int a, int b, int c)
{
    int i, j;
    for(i = a; i <= n; i += lsb(i))
        for(j = b; j <= n; j += lsb(j))
            aib[i][j] = max(aib[i][j], c);
}

void update0(int a, int b)
{
    int i, j;
    for(i = a; i <= n; i += lsb(i))
        for(j = b; j <= n; j += lsb(j))
            aib[i][j] = 0;
}

int main()
{
    int test, i;
    ifstream f ("cutii.in");
    ofstream g ("cutii.out");
    f >> n >> t;
    for(test = 1; test <= t; ++test) {
        sol = 0;
        for(i = 1; i <= n; ++i)
           f >> c[i].a >> c[i].b >> c[i].c;
        sort(c + 1, c + n + 1, cmp);
        for(i = 1; i <= n; ++i) {
            d[i] = query(c[i].b - 1, c[i].c - 1) + 1;
            update(c[i].b, c[i].c, d[i]);
            if(d[i] > sol)
                sol = d[i];
        }
        g << sol << "\n";
        for(i = 1; i <= n; ++i)
            update0(c[i].b, c[i].c);
    }
    return 0;
}