Cod sursa(job #2534642)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 30 ianuarie 2020 20:07:47
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 3500;

struct Box {
    int x;
    int y;
    int z;
};

bool cmp (Box a, Box b) {
    return a.x < b.x;
}

Box a[1 + N];
int n;
int aib[1 + N][1 + N];
int best[1 + N];


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

void upd (int x, int y, int val) {
    while (x <= n) {
        int cy = y;
        while (y <= n) {
            aib[x][y] = max (aib[x][y], val);
            y += lsb (y);
        }
        y = cy;
        x += lsb (x);
    }
}

int qry (int x, int y) {
    int ans = 0;
    while (x) {
        int cy = y;
        while (y) {
            ans = max (ans, aib[x][y]);
            y -= lsb (y);
        }
        y = cy;
        x -= lsb (x);
    }
    return ans;
}
int cln (int x, int y) {
    while (x <= n) {
        int cy = y;
        while (y <= n) {
            aib[x][y] = 0;
            y += lsb (y);
        }
        y = cy;
        x += lsb (x);
    }
}

void solveTest () {
    for (int i = 1; i <= n; i++) {
        cin >> a[i].x >> a[i].y >> a[i].z;
    }
    sort (a + 1, a + n + 1, cmp);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        best[i] = qry (a[i].y, a[i].z) + 1;
        upd (a[i].y, a[i].z, best[i]);
        ans = max (ans, best[i]);
    }
    for (int i = 1; i <= n; i++) cln (a[i].y, a[i].z);
    cout << ans << "\n";
}

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

    ios::sync_with_stdio (false);
    cin.tie (0); cout.tie (0);

    int t;
    cin >> n >> t;
    while (t--)
        solveTest ();
    return 0;
}