Cod sursa(job #2920810)

Utilizator Alex_HossuHossu Alexandru Alex_Hossu Data 25 august 2022 22:16:51
Problema Cutii Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <bits/stdc++.h>
using namespace std;

#define MAX_N 3500

pair<int, int> aib[MAX_N + 1][MAX_N + 1];

struct Box {
  int x;
  int y;
  int z;
};
Box a[MAX_N + 1];

int n, t;

void update(int x, int y, int val, int test) {
  for (int i = x; i <= n; i += i & -i) {
    for (int j = y; j <= n; j += j & -j) {
      if (aib[i][j].second == test)
        aib[i][j].first = max(aib[i][j].first, val);
      else
        aib[i][j] = {val, test};
    }
  }
}

int query(int x, int y, int test) {
  int i, j, ans = 0;

  for (i = x; i >= 1; i -= i & -i)
    for (j = y; j >= 1; j -= j & -j)
      if (aib[i][j].second == test)
        ans = max(ans, aib[i][j].first);

  return ans;
}

ifstream fin("cutii.in");
ofstream fout("cutii.out");

int main() {
  int test, i, x;
  int ans, val;

  fin >> n >> t;
  for (test = 1; test <= t; test++) {
    for (i = 1; i <= n; i++) {
      fin >> x;
      fin >> a[x].y >> a[x].z;
    }

    ans = 0;
    for (i = 1; i <= n; i++) {
      /* val = numarul maxim de cutii
      care pot fi bagate una in alta astfel
      incat a i-a cutie sa fie ultima */
      val = query(a[i].y, a[i].z, test) + 1;
      ans = max(ans, val);
      update(a[i].y, a[i].z, val, test);
    }

    fout << ans << "\n";
  }

  return 0;
}