Cod sursa(job #2885811)

Utilizator iancupoppPopp Iancu Alexandru iancupopp Data 6 aprilie 2022 17:00:44
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 3505;

struct Cutie {
  int x, y, z;
  bool operator<(const Cutie& c) const {
    if (x != c.x) return x < c.x;
    if (y != c.y) return y > c.y;
    return z > c.z;
  }
};

Cutie v[N];
int aib[N][N], n;

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

int query(const Cutie& c) {
  int ans = 0;
  for (int y = c.y; y > 0; y -= lsb(y))
    for (int z = c.z; z > 0; z -= lsb(z))
      ans = max(ans, aib[y][z]);
  return ans;
}

void update(const Cutie& c, const int val) {
  for (int y = c.y; y <= n; y += lsb(y))
    for (int z = c.z; z <= n; z += lsb(z))
      aib[y][z] = max(aib[y][z], val);
}

void clean(const Cutie& c) {
  for (int y = c.y; y <= n; y += lsb(y))
    for (int z = c.z; z <= n; z += lsb(z))
      aib[y][z] = 0;
}

int main() {
  ifstream cin("cutii.in");
  ofstream cout("cutii.out");
  int t;
  cin >> n >> t;
  while (t--) {
    for (int i = 0; i < n; ++i)
      cin >> v[i].x >> v[i].y >> v[i].z;
    sort(v, v + n);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int cnt = query({v[i].x, v[i].y - 1, v[i].z - 1});
      update(v[i], cnt + 1);
      ans = max(ans, cnt + 1);
    }
    cout << ans << "\n";
    for (int i = 0; i < n; ++i)
      clean(v[i]);
  }
  cin.close();
  cout.close();
  return 0;
}