Cod sursa(job #2147040)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 28 februarie 2018 13:26:50
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_N = 3500;

struct Cutie {
  int x, y, z;
  bool operator < (const Cutie& other) const {
    return x < other.x;
  }
}v[MAX_N + 5];

int aib[MAX_N + 5][MAX_N + 5];

void update(int x, int y, int n, int val) {
  for (int x1 = x; x1 <= n; x1 += (x1 & -x1))
    for (int y1 = y; y1 <= n; y1 += (y1 & -y1))
      if (val != 0)
        aib[x1][y1] = max(aib[x1][y1], val);
      else
        aib[x1][y1] = 0;
}

int query(int x, int y) {
  int ans = 0;
  for (int x1 = x; x1 >= 1; x1 -= (x1 & -x1))
    for (int y1 = y; y1 >= 1; y1 -= (y1 & -y1))
      ans = max(ans, aib[x1][y1]);
  return ans;
}

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

  int n, t;
  scanf("%d%d", &n, &t);
  while (t--) {
    for (int i = 1; i <= n; ++i)
      scanf("%d%d%d", &v[i].x, &v[i].y, &v[i].z);
    sort(v + 1, v + n + 1);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      int aux = query(v[i].y - 1, v[i].z - 1);
      aux++;
      update(v[i].y, v[i].z, n, aux);
      ans = max(ans, aux);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= n; ++i)
      update(v[i].y, v[i].z, n, 0);
  }
  return 0;
}