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;
  struct pnt {
    int x, y, id;
    pnt(int _x = 0, int _y = 0, int _id = 0) : x(_x), y(_y), id(_id) {}
  };
  bool xicmp(pnt a, pnt b) { return a.x < b.x; }
  bool xdcmp(pnt a, pnt b) { return a.x > b.x; }
  bool yicmp(pnt a, pnt b) { return a.y < b.y; }
  bool ydcmp(pnt a, pnt b) { return a.y > b.y; }
  pnt C[4][4][400000], P[1000000];
  int cntc[4];
  vector<int> V[4];
  bool vis[4];
  int xu, xd, yu, yd;
  bool dfs(int cur, int x) {
    int XU = xu, XD = xd, YU = yu, YD = yd;
    V[cur].clear();
    for (int i = 1; i <= 3; i++) {
      if (!vis[i]) {
        if (cur == 3) {
          int cnt = 0;
          for (int j = 1; j <= n && cnt < x; j++)
            if (C[i][0][j].x > xd && C[i][0][j].x < xu && C[i][0][j].y > yd &&
                C[i][0][j].y < yu)
              cnt++, V[cur].push_back(C[i][0][j].id);
          if (cnt == x) return 1;
          V[cur].clear();
        } else {
          vis[i] = 1;
          for (int k = 0; k < 4; k++) {
            int cnt = 0;
            for (int j = 1; j <= n && cnt < x; j++)
              if (C[i][k][j].x > xd && C[i][k][j].x < xu && C[i][k][j].y > yd &&
                  C[i][k][j].y < yu)
                cnt++, V[cur].push_back(C[i][k][j].id);
            if (cnt < x) {
              V[cur].clear();
              continue;
            }
            int u = V[cur][x - 1];
            if (k == 0)
              xd = max(xd, P[u].x);
            else if (k == 1)
              xu = min(xu, P[u].x);
            else if (k == 2)
              yd = max(yd, P[u].y);
            else
              yu = min(yu, P[u].y);
            if (dfs(cur + 1, x)) return 1;
            V[cur].clear();
            xd = XD, xu = XU, yd = YD, yu = YU;
          }
          vis[i] = 0;
        }
        V[cur].clear();
      }
    }
    return 0;
  }
  const int INF = 1e9 + 1;
  bool check(int x) {
    for (int i = 1; i <= 3; i++) V[i].clear(), vis[i] = 0;
    xu = yu = INF, xd = yd = -INF;
    return dfs(1, x);
  }
  int main() {
    scanf("%d", &n);
    n /= 3;
    for (int i = 1, x = 0, y = 0, c = 0; i <= 3 * n; i++) {
      scanf("%d%d%d", &x, &y, &c);
      C[c][0][++cntc[c]] = (pnt(x, y, i));
      P[i].x = x, P[i].y = y, P[i].id = c;
    }
    for (int i = 1; i <= 3; i++)
      for (int j = 1; j <= 3; j++)
        for (int k = 1; k <= n; k++) C[i][j][k] = C[i][0][k];
    for (int i = 1; i <= 3; i++)
      sort(C[i][0] + 1, C[i][0] + n + 1, xicmp),
          sort(C[i][1] + 1, C[i][1] + n + 1, xdcmp),
          sort(C[i][2] + 1, C[i][2] + n + 1, yicmp),
          sort(C[i][3] + 1, C[i][3] + n + 1, ydcmp);
    int l = 0, r = n;
    while (l < r) {
      int mid = (l + r + 1) >> 1;
      if (check(mid))
        l = mid;
      else
        r = mid - 1;
    }
    printf("%d\n", 3 * l);
  }
  