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;
  const long long maxn = 2e5 + 100;
  const long long mod = 1e9 + 7;
  const long long inf = 1e18;
  long long n, c[3], ans, fen[2][maxn];
  pair<pair<long long, long long>, long long> a[maxn];
  vector<long long> vec;
  void upd(long long f, long long x, long long y) {
    for (x += 5; x < maxn; x += x & -x) {
      fen[f][x] += y;
    }
    return;
  }
  long long get(long long f, long long x) {
    long long y = 0;
    for (x += 5; x > 0; x -= x & -x) {
      y += fen[f][x];
    }
    return y;
  }
  void solve() {
    fill(fen[0], fen[0] + n + 100, 0);
    fill(fen[1], fen[1] + n + 100, 0);
    sort(a, a + n);
    long long x0 = 0, x1 = 0;
    for (long long i = 0; i < n; i++) {
      if (a[i].second == c[0]) x0++;
    }
    for (long long i = 0; i < n; i++) {
      if (i == 0 || a[i].first.first != a[i - 1].first.first) {
        long long l = -1, r = n, mid;
        while (r - l > 1) {
          mid = (l + r) >> 1;
          long long p1 = get(1, mid), p2 = x1 - get(0, mid);
          if (p1 <= p2)
            l = mid;
          else
            r = mid;
        }
        ans = max(ans, min({x0, get(1, l), x1 - get(0, l)}));
        ans = max(ans, min({x0, get(1, r), x1 - get(0, r)}));
      }
      if (a[i].second == c[0]) {
        x0--;
      }
      if (a[i].second == c[1]) {
        upd(0, a[i].first.second, 1);
        x1++;
      }
      if (a[i].second == c[2]) {
        upd(1, a[i].first.second, 1);
      }
    }
    fill(fen[0], fen[0] + n + 100, 0);
    fill(fen[1], fen[1] + n + 100, 0);
    x0 = 0, x1 = 0;
    for (long long i = 0; i < n; i++) {
      if (a[i].second == c[0]) x0++;
    }
    for (long long i = 0; i < n; i++) {
      if (i == 0 || a[i].first.first != a[i - 1].first.first) {
        long long l = -1, r = n, mid;
        while (r - l > 1) {
          mid = (l + r) >> 1;
          long long p1 = get(1, mid), p2 = x1 - get(0, mid);
          if (p1 <= p2)
            l = mid;
          else
            r = mid;
        }
        ans = max(ans, min({x0, get(1, l), x1 - get(0, l)}));
        ans = max(ans, min({x0, get(1, r), x1 - get(0, r)}));
      }
      if (a[i].second == c[0]) {
        x0--;
      }
      if (a[i].second == c[1]) {
        upd(0, a[i].first.first, 1);
        x1++;
      }
      if (a[i].second == c[2]) {
        upd(1, a[i].first.first, 1);
      }
    }
    return;
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ;
    cin >> n;
    for (long long i = 0; i < n; i++) {
      cin >> a[i].first.first >> a[i].first.second >> a[i].second;
      vec.push_back(a[i].first.first);
    }
    sort((vec).begin(), (vec).end());
    vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
    for (long long i = 0; i < n; i++) {
      a[i].first.first =
          lower_bound((vec).begin(), (vec).end(), a[i].first.first) - vec.begin();
    }
    vec.clear();
    for (long long i = 0; i < n; i++) {
      vec.push_back(a[i].first.second);
    }
    sort((vec).begin(), (vec).end());
    vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
    for (long long i = 0; i < n; i++) {
      a[i].first.second =
          lower_bound((vec).begin(), (vec).end(), a[i].first.second) -
          vec.begin();
    }
    c[0] = 1;
    c[1] = 2;
    c[2] = 3;
    do {
      for (long long t = 0; t < 2; t++) {
        for (long long o = 0; o < 2; o++) {
          solve();
          for (long long i = 0; i < n; i++) {
            a[i].first.first = n - 1 - a[i].first.first;
          }
        }
        for (long long i = 0; i < n; i++) {
          swap(a[i].first.first, a[i].first.second);
        }
      }
    } while (next_permutation(c, c + 3));
    cout << ans * 3;
    return 0;
  }
  