Cod sursa(job #1655637)

Utilizator cristina_borzaCristina Borza cristina_borza Data 18 martie 2016 09:51:26
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
#include <algorithm>
#include <fstream>
#include <cstring>
#include <cstdio>

#define NMAX 4000

using namespace std;

struct type {
    int x , y , z;
} a[NMAX];

bool comp(type a , type b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y) || (a.x == b.x && a.y == b.y && a.z < b.z);
}

int aib[NMAX][NMAX];
int n , T;

int query(int x , int y);
int lsb(int x);

void update(int x , int y , int val);

void read(int &x) {
    int sign = 1;
    char ch;
    x = 0;

    while(!isdigit(ch = getchar())) {
        if(ch == '-') {
            sign = -1;
        }

        else {
            sign = 1;
        }
    }

    do {
        x = x * 10 + ch - '0';
    }while(isdigit(ch = getchar()));
    x *= sign;
}


void zero(int x, int y) {
    for (int i = x ; i <= n ; i += lsb(i)) {
        for (int j = y; j <= n; j += lsb(i)) {
            aib[i][j] = 0;
        }
    }
}

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

    read(n) , read(T);
    while(T--) {
        for(int i = 1 ; i <= n ; ++i) {
            read(a[i].x);
            read(a[i].y);
            read(a[i].z);
        }

        sort(a + 1 , a + n + 1 , comp);
        for(int i = 1 ; i <= n ; ++i) {
            int aux = query(a[i].y - 1 , a[i].z - 1);
            update(a[i].y , a[i].z , aux + 1);
        }
        printf("%d\n" , query(n , n));

        for(int i = 1 ; i <= n ; ++i) {
            zero(a[i].y  , a[i].z);
        }

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

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

    return sol;
}

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)) {
            aib[i][j] = max(aib[i][j] , val);
        }
    }
}