树状数组解题
const int MAXN = 1000000;
int tree[MAXN];
class Solution {
public:
inline int lowbit(int pos) {
return pos & -pos;
}
void update(int pos) {
while (pos <= MAXN) {
tree[pos] += 1;
pos += lowbit(pos);
}
}
int query(int pos) {
int ans = 0;
while (pos > 0) {
ans += tree[pos];
pos -= lowbit(pos);
}
return ans;
}
int inversePairs(vector<int> nums) {
size_t size = nums.size();
int result = 0;
for (int i = 0; i < size; i++) {
update(nums[i] + 1);
result += (i + 1 - query(nums[i]+1));
}
return result;
}
};