class Solution {
public:
int lowbit(int x) {return x&(-x);}
void update(int i,vector<int>& c)
{//单点更新,更新这个点还要更新他的父亲所以是i+=lowbit(i)
while(i <= c.size())
{
c[i]+=1;
i+=lowbit(i);
}
}
int getsum(int i,vector<int>& c)
{//获取前缀和,前缀和肯定是比i小的索引,所以i-=lowbit(i);
int res=0;
while(i)
{
res+=c[i];
i-=lowbit(i);
}
return res;
}
int reversePairs(vector<int>& nums) {
int n=nums.size();
vector<int> vt;
vector<int> c(n+20);
for(auto i:nums) vt.push_back(i);
sort(vt.begin(),vt.end());
vt.erase(unique(vt.begin(),vt.end()),vt.end());
unordered_map<int,int> mp;
int cont=1;
for(auto i:vt) mp[i]=cont++;
int ans=0;
for(int i=0;i<nums.size();i++)
{
update(mp[nums[i]],c);
ans+=(i+1)-getsum(mp[nums[i]],c);
}
return ans;
}
};