Cod sursa(job #1706817)

Utilizator BrandonChris Luntraru Brandon Data 23 mai 2016 14:14:02
Problema Cutii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

ifstream cin("cutii.in");
ofstream cout("cutii.out");

const int MaxN = 3505;

int BITree[MaxN][MaxN];
int n, T;

class box {
public:
  int a, b, c;

  box(int _a = 0, int _b = 0, int _c = 0) {
    a = _a;
    b = _b;
    c = _c;
  }

  inline bool operator < (const box &other) const &{
    return a < other.a;
  }
};

vector <box> boxes;

inline void Update(int pos1, int pos2, int val) {
  for (; pos1 <= MaxN; pos1 += pos1 & (-pos1)) {
    for (int pos2c = pos2; pos2c <= MaxN; pos2c += pos2c & (-pos2c)) BITree[pos1][pos2c] = max(BITree[pos1][pos2c], val);
  }
}

inline int Query(int pos1, int pos2) {
  int ans = 0;
  for (; pos1; pos1 -= pos1 & (-pos1)) {
    for (int pos2c = pos2; pos2c; pos2c -= pos2c & (-pos2c)) ans = max(ans, BITree[pos1][pos2c]);
  }
  return ans;
}

void BITreeClear() {
  for (auto it: boxes) {
    for (int i = it.b; i <= MaxN; i += i & (-i)) {
      for (int j = it.c; j <= MaxN; j += j & (-j)) BITree[i][j] = 0;
    }
  }
}

inline void TaskInit() {
  BITreeClear();
  boxes.clear();
}

int Lis() {
  int ans = 0;
  for (auto it: boxes) {
    int CurrLng = Query(it.b - 1, it.c - 1) + 1;
    Update(it.b, it.c, CurrLng);
    ans = max(ans, CurrLng);
  }
  return ans;
}

int main() {
  cin >> n >> T;
  for (int task = 1; task <= T; ++task) {
    for (int i = 1; i <= n; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      boxes.push_back(box(a, b, c));
    }
    sort(boxes.begin(), boxes.end());
    cout << Lis() << '\n';
    TaskInit();
  }
  return 0;
}