Posted on

Description

Submission

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;

typedef tree<
pair<long, int>,
null_type,
less<pair<long,int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;


class Solution {
public:
    int reversePairs(vector<int>& nums) {
        ordered_set Set;
        int ret = 0;
        int n = nums.size();

        for(int i = 0; i < n; ++i) {
            int k = Set.order_of_key({2 * (long)nums[i] + 1, 0});

            ret += i - k;

            Set.insert({nums[i], i});
        }
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *