<p>There are <code>n</code> soldiers standing in a line. Each soldier is assigned a <strong>unique</strong> <code>rating</code> value.</p>

<p>You have to form a team of 3 soldiers amongst them under the following rules:</p>

<ul>
	<li>Choose 3 soldiers with index (<code>i</code>, <code>j</code>, <code>k</code>) with rating (<code>rating[i]</code>, <code>rating[j]</code>, <code>rating[k]</code>).</li>
	<li>A team is valid if: (<code>rating[i] &lt; rating[j] &lt; rating[k]</code>) or (<code>rating[i] &gt; rating[j] &gt; rating[k]</code>) where (<code>0 &lt;= i &lt; j &lt; k &lt; n</code>).</li>
</ul>

<p>Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> rating = [2,5,3,4,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> rating = [2,1,3]
<strong>Output:</strong> 0
<strong>Explanation:</strong> We can&#39;t form any team given the conditions.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> rating = [1,2,3,4]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == rating.length</code></li>
	<li><code>3 &lt;= n &lt;= 1000</code></li>
	<li><code>1 &lt;= rating[i] &lt;= 10<sup>5</sup></code></li>
	<li>All the integers in <code>rating</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:

    struct SegmentTree {
        int size;
        vector<int> nodes;
        
        SegmentTree(int n) {
            size = n;
            nodes.resize(2*n);
        }

        void update(int index, int val) {
            index += size;
            nodes[index] += val;

            for (int i = index/2; i > 0; i /= 2) {
                nodes[i] = nodes[2*i] + nodes[2*i+1];
            }
        }

        int query(int lower, int upper) {
            int sum = 0;
            for (lower += size, upper += size; lower <= upper; lower /= 2, upper /= 2) {
                if (lower & 1) sum += nodes[lower++];
                if (!(upper & 1)) sum += nodes[upper--];
            }
            return sum;
        }
    };

    struct BitTree {
        vector<int> bit;

        BitTree(int size) {
            bit.resize(size);
        }

        void update(int index, int size, int val) {
            for (index; index <= size; index += index & (-index)) {
                bit[index] += val;
            }
        }

        int sum(int index) {
            int s = 0;
            for (index; index > 0; index -= index & (-index)) {
                //cout << "i " << index << " size " << bit.size()<<"\n";
                s += bit[index];
            }
            return s;
        }

        int query(int low, int up) {
            if (low > up) return 0;
            return sum(up) - sum(low);
        }

        void print() {
            for (int i : bit) {
                cout << "   " << i;
            }
            cout << "\n";
        }
    };

    int numTeams(vector<int>& rating) {
        int size = rating.size();
        int max_val = *max_element(rating.begin(), rating.end());
        BitTree left_tree(max_val+2), right_tree(max_val+2);
        vector<int> left_low(size), left_up(size), right_low(size), right_up(size);

        for (int i = size-1; i >= 0; i--) {
            int r = rating[i];
            right_up[i] = right_tree.sum(max_val) - right_tree.sum(rating[i]);
            right_low[i] = right_tree.sum(r-1);//max(0, right_tree.sum(1) - right_tree.sum(rating[i]));
            cout << "right: " <<  right_low[i] << ", " <<  right_up[i] << "\n";
            right_tree.update(rating[i], max_val, 1);
        }

        //right_tree.print();

        for (int i = 0; i < size; i++) {
            left_up[i] = left_tree.sum(max_val) - left_tree.sum(rating[i]);
            left_low[i] = left_tree.sum(rating[i] - 1);
            cout << "left: " <<  left_low[i] << ", " <<  left_up[i] << "\n";
            left_tree.update(rating[i], max_val, 1);
        }

        //left_tree.print();

        int res = 0;
        for (int i = 0; i < size; i++) {
            res += left_up[i] * right_low[i];
            res += left_low[i] * right_up[i];
        }

        return res;
    }

    int numTeams2(vector<int>& rating) {
        int size = rating.size();
        int m = *max_element(rating.begin(), rating.end());

        SegmentTree L(m+1), R(m+1);

        for (int i : rating) R.update(i, 1);

        int res = 0;
        for (int rate : rating) {
            R.update(rate, -1);

            int left_lower = L.query(1, rate-1);
            int right_lower = R.query(1, rate-1);
            int left_upper = L.query(rate+1, m);
            int right_upper = R.query(rate+1, m);

            res += left_lower * right_upper + left_upper * right_lower;
            L.update(rate, 1);
        }

        return res;
    }
};
21763