-
Notifications
You must be signed in to change notification settings - Fork 0
/
315.count-of-smaller-numbers-after-self.cpp
64 lines (61 loc) · 1.25 KB
/
315.count-of-smaller-numbers-after-self.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
/*
* @lc app=leetcode id=315 lang=cpp
*
* [315] Count of Smaller Numbers After Self
*/
// @lc code=start
class FenwickTree
{
private:
vector<int> preSum;
static inline int lowbit(int x)
{
return x & -x;
}
public:
FenwickTree(int n) : preSum(n + 1, 0) {}
void update(int i, int delta)
{
while (i < preSum.size())
{
preSum[i] += delta;
i += lowbit(i);
}
}
int query(int i) const
{
int sum = 0;
while (i > 0)
{
sum += preSum[i];
i -= lowbit(i);
}
return sum;
}
};
class Solution
{
public:
vector<int> countSmaller(vector<int> &nums)
{
set<int> sorted(nums.begin(), nums.end());
unordered_map<int, int> ranks;
int rank = 0;
for (int num : sorted)
{
ranks[num] = ++rank;
}
vector<int> res;
FenwickTree tree(rank);
for (int i = nums.size() - 1; i >= 0; --i)
{
res.push_back(tree.query(ranks[nums[i]] - 1));
tree.update(ranks[nums[i]], 1);
}
reverse(res.begin(), res.end());
return res;
}
};
// @lc code=end