92% beat || C++ || O(n * log n) and O(n)
Intuition\nThe problem involves distributing elements between two arrays based on a set of conditions. The code utilizes Fenwick trees to efficiently calculate 
# Intuition\nThe problem involves distributing elements between two arrays based on a set of conditions. The code utilizes Fenwick trees to efficiently calculate the number of elements greater than a particular value in each array.\n\n# Approach\nThe code maintains two arrays, `arr1` and `arr2`, and uses Fenwick trees (`tree1` and `tree2`) to keep track of the number of elements greater than a specific value in each array.\nIt initializes the arrays with the first two elements from the given `nums` array and updates the Fenwick trees accordingly.\nIt then iterates through the remaining elements of nums and, for each element, calculates the number of elements greater than it in both arrays.\nBased on the conditions specified in the problem, it appends the current element to the array with fewer elements or to `arr1` in case of a tie.\nAfter the iterations, it concatenates `arr1` and `arr2` to form the result array.\n\n# Complexity\n- Time complexity:\nThe time complexity is O(n * log n), where n is the length of the input `nums`. This is due to the sorting operation in the `getRanks` function and the updates in the Fenwick trees.\n\n- Space complexity:\nThe space complexity is O(n) due to the storage of arrays `arr1` and `arr2`, Fenwick trees, and the `ranks` map.\n\n# Note\n- The code efficiently uses Fenwick trees to maintain information about the number of elements greater than a specific value. The `getRanks` function is used to obtain the ranks of elements for quick comparisons, and the `add` function adds elements to the arrays and updates the Fenwick trees accordingly. The algorithm follows the rules specified in the problem to distribute elements between the two arrays.\n\n# Code\n```\nclass FenwickTree {\n public:\n  FenwickTree(int n) : sums(n + 1) {}\n\n  void update(int i, int delta) {\n    while (i < sums.size()) {\n      sums[i] += delta;\n      i += lowbit(i);\n    }\n  }\n\n  int get(int i) const {\n    int sum = 0;\n    while (i > 0) {\n      sum += sums[i];\n      i -= lowbit(i);\n    }\n    return sum;\n  }\n\n private:\n  vector<int> sums;\n\n  static inline int lowbit(int i) {\n    return i & -i;\n  }\n};\n\nclass Solution {\n public:\n  vector<int> resultArray(vector<int>& nums) {\n    vector<int> arr1;\n    vector<int> arr2;\n    const unordered_map<int, int> ranks = getRanks(nums);\n    FenwickTree tree1(ranks.size());\n    FenwickTree tree2(ranks.size());\n\n    add(nums[0], arr1, tree1, ranks);\n    add(nums[1], arr2, tree2, ranks);\n\n    for (int i = 2; i < nums.size(); ++i) {\n      const int greaterCount1 = arr1.size() - tree1.get(ranks.at(nums[i]));\n      const int greaterCount2 = arr2.size() - tree2.get(ranks.at(nums[i]));\n      if (greaterCount1 > greaterCount2)\n        add(nums[i], arr1, tree1, ranks);\n      else if (greaterCount1 < greaterCount2)\n        add(nums[i], arr2, tree2, ranks);\n      else if (arr1.size() > arr2.size())\n        add(nums[i], arr2, tree2, ranks);\n      else\n        add(nums[i], arr1, tree1, ranks);\n    }\n\n    arr1.insert(arr1.end(), arr2.begin(), arr2.end());\n    return arr1;\n  }\n\n private:\n  unordered_map<int, int> getRanks(const vector<int>& nums) {\n    unordered_map<int, int> ranks;\n    set<int> sorted(nums.begin(), nums.end());\n    int rank = 0;\n    for (const int num : sorted)\n      ranks[num] = ++rank;\n    return ranks;\n  }\n\n  void add(int num, vector<int>& arr, FenwickTree& tree,\n           const unordered_map<int, int>& ranks) {\n    arr.push_back(num);\n    tree.update(ranks.at(num), 1);\n  };\n};\n```
['C++']