Ordered Set Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n#include<bits/stdc++.h>\n#include<ext/pb_ds/assoc_container.hpp>\n#include<ext/pb_ds/tree_policy.hpp>\nusing namespace std;\nusing namespace __gnu_pbds;\ntemplate<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;\ntemplate<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;\nclass Solution {\npublic:\n    vector<int> resultArray(vector<int>& a) {\n        ordered_multiset<int> s1;\n        map<int,int> m1;\n        ordered_multiset<int> s2;\n        map<int,int> m2;\n        vector<int> nums1;\n        vector<int> nums2;\n        nums1.push_back(a[0]);\n        s1.insert(a[0]); m1[a[0]]++;\n        nums2.push_back(a[1]);\n        s2.insert(a[1]); m2[a[1]]++;\n        auto greatercount1 = [&](int v)\n        {\n            return nums1.size() - s1.order_of_key(v) - m1[v];\n        };\n        auto greatercount2 = [&](int v)\n        {\n            return nums2.size() - s2.order_of_key(v) - m2[v];\n        };\n        for(int i = 2; i < a.size(); i++)\n        {\n            int v1 = greatercount1(a[i]);\n            int v2 = greatercount2(a[i]);\n            if(v1 > v2)\n            {\n                nums1.push_back(a[i]);\n                s1.insert(a[i]);\n                m1[a[i]]++;\n            }\n            if(v1 < v2)\n            {\n                nums2.push_back(a[i]);\n                s2.insert(a[i]);\n                m2[a[i]]++;\n            }\n\n            if(v1 == v2)\n            {\n                if(nums1.size() <= nums2.size())\n                {\n                    nums1.push_back(a[i]);\n                    s1.insert(a[i]);\n                    m1[a[i]]++;\n                }\n                else if(nums1.size() > nums2.size())\n                {\n                    nums2.push_back(a[i]);\n                    s2.insert(a[i]);\n                    m2[a[i]]++;\n                }\n            }\n        }\n        vector<int> ans;\n        for(auto it : nums1)\n        {\n            ans.push_back(it);\n        }\n        for(auto it : nums2)\n        {\n            ans.push_back(it);\n        }\n        return ans;\n    }\n};\n```
['C++']