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;
  int n;
  class BIT {
    int Len;
    int b[200020];
    inline int lowbit(int x) { return x & (-x); }
  
   public:
    void init(int _n) {
      Len = _n;
      for (int i = 1; i <= Len; ++i) b[i] = 0;
    }
    inline void Add(int x, int d) {
      while (x <= Len) b[x] += d, x += lowbit(x);
    }
    inline int Ask(int x) {
      int ans = 0;
      while (x) ans += b[x], x -= lowbit(x);
      return ans;
    }
  } B[3];
  int x[200020], y[200020], c[200020], bx[200020], by[200020], mx, my, p[200020];
  int D[200020][3], U[200020][3], L[200020][3], R[200020][3];
  vector<int> X[200020], Y[200020];
  int Get(int m, int c0, int c1) {
    int l = 1, r = m + 1, mx = 0;
    while (l < r) {
      int mid = (l + r) >> 1;
      int t0 = B[c0].Ask(mid), t1 = B[c1].Ask(m) - B[c1].Ask(mid);
      mx = max(mx, min(t0, t1));
      if (t0 <= t1)
        l = mid + 1;
      else
        r = mid;
    }
    return mx;
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> x[i] >> y[i] >> c[i];
      bx[i] = x[i], by[i] = y[i];
      --c[i];
    }
    sort(bx + 1, bx + n + 1);
    sort(by + 1, by + n + 1);
    mx = unique(bx + 1, bx + n + 1) - bx - 1;
    my = unique(by + 1, by + n + 1) - by - 1;
    for (int i = 1; i <= n; ++i) {
      x[i] = lower_bound(bx + 1, bx + mx + 1, x[i]) - bx;
      y[i] = lower_bound(by + 1, by + my + 1, y[i]) - by;
      p[i] = i;
    }
    for (int i = 1; i <= n; ++i) {
      X[x[i]].push_back(i);
      Y[y[i]].push_back(i);
    }
    {
      int now[3] = {0, 0, 0};
      for (int i = 1; i <= mx; ++i) {
        for (auto z : X[i]) {
          now[c[z]]++;
        }
        for (int j = 0; j < 3; ++j) L[i][j] = now[j];
      }
    }
    {
      int now[3] = {0, 0, 0};
      for (int i = mx; i >= 1; --i) {
        for (auto z : X[i]) {
          now[c[z]]++;
        }
        for (int j = 0; j < 3; ++j) R[i][j] = now[j];
      }
    }
    {
      int now[3] = {0, 0, 0};
      for (int i = 1; i <= my; ++i) {
        for (auto z : Y[i]) {
          now[c[z]]++;
        }
        for (int j = 0; j < 3; ++j) D[i][j] = now[j];
      }
    }
    {
      int now[3] = {0, 0, 0};
      for (int i = my; i >= 1; --i) {
        for (auto z : Y[i]) {
          now[c[z]]++;
        }
        for (int j = 0; j < 3; ++j) U[i][j] = now[j];
      }
    }
    int ans = 0;
    {
      for (int i = 0; i < 3; ++i) B[i].init(my);
      for (int i = 1; i <= mx; ++i) {
        for (auto z : X[i]) {
          B[c[z]].Add(y[z], 1);
        }
        for (int j = 0; j < 3; ++j) {
          ans = max(ans, 3 * min(Get(my, (j + 1) % 3, (j + 2) % 3), R[i + 1][j]));
          ans = max(ans, 3 * min(Get(my, (j + 2) % 3, (j + 1) % 3), R[i + 1][j]));
        }
      }
    }
    {
      for (int i = 0; i < 3; ++i) B[i].init(my);
      for (int i = mx; i >= 1; --i) {
        for (auto z : X[i]) {
          B[c[z]].Add(y[z], 1);
        }
        for (int j = 0; j < 3; ++j) {
          ans = max(ans, 3 * min(Get(my, (j + 1) % 3, (j + 2) % 3), L[i - 1][j]));
          ans = max(ans, 3 * min(Get(my, (j + 2) % 3, (j + 1) % 3), L[i - 1][j]));
        }
      }
    }
    {
      for (int i = 0; i < 3; ++i) B[i].init(mx);
      for (int i = 1; i <= my; ++i) {
        for (auto z : Y[i]) {
          B[c[z]].Add(x[z], 1);
        }
        for (int j = 0; j < 3; ++j) {
          ans = max(ans, 3 * min(Get(mx, (j + 1) % 3, (j + 2) % 3), U[i + 1][j]));
          ans = max(ans, 3 * min(Get(mx, (j + 2) % 3, (j + 1) % 3), U[i + 1][j]));
        }
      }
    }
    {
      for (int i = 0; i < 3; ++i) B[i].init(mx);
      for (int i = my; i >= 1; --i) {
        for (auto z : Y[i]) {
          B[c[z]].Add(x[z], 1);
        }
        for (int j = 0; j < 3; ++j) {
          ans = max(ans, 3 * min(Get(mx, (j + 1) % 3, (j + 2) % 3), D[i - 1][j]));
          ans = max(ans, 3 * min(Get(mx, (j + 2) % 3, (j + 1) % 3), D[i - 1][j]));
        }
      }
    }
    for (int i = 0; i < 3; ++i) B[i].init(mx);
    for (int i = 1; i <= n; ++i) {
      B[c[i]].Add(x[i], 1);
    }
    for (int i = 1; i <= mx; ++i) {
      int d[3] = {0, 1, 2};
      do {
        ans = max(ans, 3 * min(R[i + 1][d[2]], Get(i, d[0], d[1])));
      } while (next_permutation(d, d + 3));
    }
    for (int i = 0; i < 3; ++i) B[i].init(my);
    for (int i = 1; i <= n; ++i) {
      B[c[i]].Add(y[i], 1);
    }
    for (int i = 1; i <= my; ++i) {
      int d[3] = {0, 1, 2};
      do {
        ans = max(ans, 3 * min(U[i + 1][d[2]], Get(i, d[0], d[1])));
      } while (next_permutation(d, d + 3));
    }
    cout << ans << '\n';
    return 0;
  }
  