class Solution {public: int numTeams(vector<int>& rating) { set<int> LS, RS; for (const auto& x : rating) { RS.insert(x); } auto cnt_lt = [](set<int>& s, int x) -> int { return distance(s.begin(), s.lower_bound(x)); }; auto cnt_gt = [](set<int>& s, int x) -> int { return distance(s.upper_bound(x), s.end()); }; int ret = 0; for (const auto& x : rating) { RS.erase(x); ret += cnt_lt(LS, x) * cnt_gt(RS, x); ret += cnt_gt(LS, x) * cnt_lt(RS, x); LS.insert(x); } return ret; }};