Cod sursa(job #2211203)

Utilizator ContDeRacistAliniateEBlat ContDeRacist Data 9 iunie 2018 16:04:32
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <fstream>
#include <algorithm>
using namespace std;

#define zeros(x) ((x^(x-1))&x)
ifstream cin("cutii.in");
ofstream cout("cutii.out");

const int N = 3507;

int t[N][N];

pair < int, pair < int, int > > c[N];

int query(pair < int, int > val) {
    int ans(0);
    for (int i = val.first; i > 0; i -= i&(-i)) {
        for (int j = val.second; j > 0; j -= j&(-j)) {
            ans = max(ans, t[i][j]);
        }
    }
    return ans;
}

void update(int val, pair < int, int > val2) {
    for (int i = val2.first; i < N; i += i&(-i)) {
        for (int j = val2.second; j < N; j += j&(-j)) {
            t[i][j] = max(t[i][j], val);
        }
    }
}

void reset(int x, int y) {
    for (int i = x; i < N; i += i&(-i)) {
        for (int j = y; j < N; j += j&(-j)) {
            t[i][j] = 0;
        }
    }
}

void _update(int x,int y,int nr)
{
    for(int i=x;i<=N;i=i+zeros(i))
    {
        for(int j=y;j<=N;j=j+zeros(j))
        t[i][j]=max(t[i][j],nr);
    }
}
void _init(int x,int y)
{
    for(int i=x;i<=N;i=i+zeros(i))
    { for(int j=y;j<=N;j=j+zeros(j))
    t[i][j]=0;}
}
int _query(int x,int y)
{
    int maxi=0;
    for(int i=x;i>=1;i=i-zeros(i))
    {
        for(int j=y;j>=1;j=j-zeros(j))
        maxi=max(maxi,t[i][j]);
    }
    return maxi;
}

int main()
{
    int n, t, solution(-1);
    cin >> n >> t;
    while (t--) {
        solution = -1;
        for (int i = 0; i < n; ++i) {
            cin >> c[i].first >> c[i].second.first >> c[i].second.second;
        }
        sort(c, c + n);
        for (int i = 0; i < n; ++i) {
            int answer = _query(c[i].second.first - 1, c[i].second.first - 1) + 1;
            solution = max(solution, answer);
            _update(c[i].second.first, c[i].second.second, answer);
        }
        for (int i = 0; i < n; ++i) {
            _init(c[i].second.first, c[i].second.second);
        }
        cout << solution << "\n";
    }
    return 0;
}