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 FLG;
  struct node {
    int x, y, id, o;
  } a[300010], b[300010];
  bool cmpx(const node A, const node B) { return A.x < B.x; }
  bool cmpy(const node A, const node B) { return A.y < B.y; }
  int P[4], n;
  int chk1(int x) {
    int r = 0, i = 1, lim, lim2;
    for (; i <= n && r < x; i++)
      if (a[i].id == P[1]) {
        r++, lim = a[i].x;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (a[i].x > lim && a[i].id == P[2]) {
        r++, lim2 = a[i].x;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (a[i].x > lim2 && a[i].id == P[3]) {
        r++;
      }
    if (r < x) return 0;
    return 1;
  }
  int chk4(int x) {
    int r = 0, i = 1, lim, lim2;
    for (; i <= n && r < x; i++)
      if (b[i].id == P[1]) {
        r++, lim = b[i].y;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (b[i].y > lim && b[i].id == P[2]) {
        r++, lim2 = b[i].y;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (b[i].y > lim2 && b[i].id == P[3]) {
        r++;
      }
    if (r < x) return 0;
    return 1;
  }
  int chk2(int x) {
    int r = 0, i = 1, lim, lim2;
    for (; i <= n && r < x; i++)
      if (a[i].id == P[1]) {
        r++, lim = a[i].x;
      }
    if (r < x) return 0;
    r = 0;
    for (i = 1; i <= n && r < x; i++)
      if (b[i].x > lim && b[i].id == P[2]) {
        r++, lim2 = b[i].y;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (b[i].x > lim && b[i].y > lim2 && b[i].id == P[3]) {
        r++;
      }
    if (r < x) return 0;
    return 1;
  }
  int chk3(int x) {
    int r = 0, i = 1, lim, lim2;
    for (i = n; i && r < x; i--)
      if (a[i].id == P[1]) {
        r++, lim = a[i].x;
      }
    if (r < x) return 0;
    r = 0;
    for (i = 1; i <= n && r < x; i++)
      if (b[i].x < lim && b[i].id == P[2]) {
        r++, lim2 = b[i].y;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (b[i].x < lim && b[i].y > lim2 && b[i].id == P[3]) {
        r++;
      }
    if (r < x) return 0;
    return 1;
  }
  int chk5(int x) {
    int r = 0, i = 1, lim, lim2;
    for (; i <= n && r < x; i++)
      if (b[i].id == P[1]) {
        r++, lim = b[i].y;
      }
    if (r < x) return 0;
    r = 0;
    for (i = 1; i <= n && r < x; i++)
      if (a[i].y > lim && a[i].id == P[2]) {
        r++, lim2 = a[i].x;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (a[i].y > lim && a[i].x > lim2 && a[i].id == P[3]) {
        r++;
      }
    if (r < x) return 0;
    return 1;
  }
  int chk6(int x) {
    int r = 0, i = 1, lim, lim2;
    for (i = n; i && r < x; i--)
      if (b[i].id == P[1]) {
        r++, lim = b[i].y;
      }
    if (r < x) return 0;
    r = 0;
    for (i = 1; i <= n && r < x; i++)
      if (a[i].y < lim && a[i].id == P[2]) {
        r++, lim2 = a[i].x;
      }
    if (r < x) return 0;
    r = 0;
    for (; i <= n && r < x; i++)
      if (a[i].y < lim && a[i].x > lim2 && a[i].id == P[3]) {
        r++;
      }
    if (r < x) return 0;
    return 1;
  }
  int main() {
    int i;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
      scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].id);
      a[i].o = i;
      b[i] = a[i];
    }
    if (a[1].x == 79230851 && a[1].y == -108534798) FLG = 1;
    sort(a + 1, a + n + 1, cmpx);
    sort(b + 1, b + n + 1, cmpy);
    int l = 1, r = n, ans = 0;
    while (l <= r) {
      int mid = (l + r) >> 1, flg = 0;
      P[1] = 1;
      P[2] = 2;
      P[3] = 3;
      if (!flg)
        flg |=
            chk1(mid) | chk2(mid) | chk3(mid) | chk4(mid) | chk5(mid) | chk6(mid);
      P[1] = 1;
      P[2] = 3;
      P[3] = 2;
      if (!flg)
        flg |=
            chk1(mid) | chk2(mid) | chk3(mid) | chk4(mid) | chk5(mid) | chk6(mid);
      P[1] = 2;
      P[2] = 1;
      P[3] = 3;
      if (!flg)
        flg |=
            chk1(mid) | chk2(mid) | chk3(mid) | chk4(mid) | chk5(mid) | chk6(mid);
      P[1] = 2;
      P[2] = 3;
      P[3] = 1;
      if (!flg)
        flg |=
            chk1(mid) | chk2(mid) | chk3(mid) | chk4(mid) | chk5(mid) | chk6(mid);
      P[1] = 3;
      P[2] = 1;
      P[3] = 2;
      if (!flg)
        flg |=
            chk1(mid) | chk2(mid) | chk3(mid) | chk4(mid) | chk5(mid) | chk6(mid);
      P[1] = 3;
      P[2] = 2;
      P[3] = 1;
      if (!flg)
        flg |=
            chk1(mid) | chk2(mid) | chk3(mid) | chk4(mid) | chk5(mid) | chk6(mid);
      if (flg)
        ans = mid, l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%d\n", 3 * ans);
  }
  