description: On an endless checkered sheet of paper, n cells are chosen and colored in three colors, where n is divisible by 3. It turns out that there are exactly n/3 marked cells of each of three colors! 
  
  Find the largest such k that it's possible to choose k/3 cells of each color, remove all other marked cells, and then select three rectangles with sides parallel to the grid lines so that the following conditions hold:
  
    * No two rectangles can intersect (but they can share a part of the boundary). In other words, the area of intersection of any two of these rectangles must be 0.
    * The i-th rectangle contains all the chosen cells of the i-th color and no chosen cells of other colors, for i = 1, 2, 3. 
  
  Input
  
  The first line of the input contains a single integer n — the number of the marked cells (3 ≤ n ≤ 10^5, n is divisible by 3).
  
  The i-th of the following n lines contains three integers x_i, y_i, c_i (|x_i|,|y_i| ≤ 10^9; 1 ≤ c_i ≤ 3), where (x_i, y_i) are the coordinates of the i-th marked cell and c_i is its color.
  
  It's guaranteed that all cells (x_i, y_i) in the input are distinct, and that there are exactly n/3 cells of each color.
  
  Output
  
  Output a single integer k — the largest number of cells you can leave.
  
  Examples
  
  Input
  
  
  9
  2 3 1
  4 1 2
  2 1 3
  3 4 1
  5 3 2
  4 4 3
  2 4 1
  5 2 2
  3 5 3
  
  
  Output
  
  
  6
  
  
  Input
  
  
  3
  1 1 1
  2 2 2
  3 3 3
  
  
  Output
  
  
  3
  
  Note
  
  In the first sample, it's possible to leave 6 cells with indexes 1, 5, 6, 7, 8, 9.
  
  In the second sample, it's possible to leave 3 cells with indexes 1, 2, 3.
solution: #include <bits/stdc++.h>
  using namespace std;
  template <class T>
  bool uin(T &a, T b) {
    return a > b ? (a = b, true) : false;
  }
  template <class T>
  bool uax(T &a, T b) {
    return a < b ? (a = b, true) : false;
  }
  vector<pair<int, int> > byx[4][3], byy[4][3];
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
    vector<vector<pair<int, int> > > a(3);
    int n;
    cin >> n;
    for (int i = 0; i < (int)(n); ++i) {
      pair<int, int> p;
      int c;
      cin >> p.first >> p.second >> c;
      --c;
      a[c].push_back(p);
    }
    for (int z = 0; z < (int)(4); ++z) {
      for (int c = 0; c < (int)(3); ++c) {
        byx[z][c] = a[c];
        sort((byx[z][c]).begin(), (byx[z][c]).end());
        byy[z][c] = a[c];
        sort((byy[z][c]).begin(), (byy[z][c]).end(),
             [&](pair<int, int> a, pair<int, int> b) {
               return a.second < b.second;
             });
        for (auto &p : a[c]) {
          swap(p.first, p.second);
          p.second = -p.second;
        }
      }
    }
    int L = 0, R = n / 3 + 1;
    while (R - L > 1) {
      int M = (L + R) / 2;
      bool ok = false;
      for (int z = 0; z < (int)(4); ++z)
        for (int c = 0; c < (int)(3); ++c) {
          int lx = byx[z][c][M - 1].first + 1;
          vector<vector<pair<int, int> > > b(2);
          for (int d = 0; d < (int)(2); ++d) {
            int cc = (c + 1 + d) % 3;
            for (auto p : byy[z][cc])
              if (p.first >= lx) b[d].push_back(p);
          }
          if (b[0].size() < M || b[1].size() < M) continue;
          for (int z = 0; z < (int)(2); ++z)
            ok |= b[z][M - 1].second < b[z ^ 1].end()[-M].second;
          for (int d = 0; d < (int)(2); ++d) {
            int lxx = lx;
            bool good = true;
            for (int e = 0; e < (int)(2); ++e) {
              int cc = (c + 1 + (d ^ e)) % 3;
              auto it1 = lower_bound((byx[z][cc]).begin(), (byx[z][cc]).end(),
                                     make_pair(lxx, -1));
              if (byx[z][cc].end() - it1 < M) {
                good = false;
                break;
              }
              lxx = (it1 + M - 1)->first + 1;
            }
            if (good) ok = true;
          }
        }
      (ok ? L : R) = M;
    }
    cout << 3 * L << '\n';
    return 0;
  }
  