-
Notifications
You must be signed in to change notification settings - Fork 111
/
count-of-smaller-numbers-after-self.cc
55 lines (50 loc) · 1.26 KB
/
count-of-smaller-numbers-after-self.cc
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
// Count of Smaller Numbers After Self
// divide and conquer
typedef pair<int, int> pii;
#define REP(i, n) for (int i = 0; i < (n); i++)
class Solution {
vector<pii> a, b;
vector<int> c;
void f(int l, int r) {
if (r-l <= 1)
return;
int m = l+(r-l)/2;
f(l, m);
f(m, r);
for (int i = l, j = m, k = l; i < m || j < r; )
if (j == r || i < m && a[i].first <= a[j].first) {
c[a[i].second] += j-m;
b[k++] = a[i++];
} else
b[k++] = a[j++];
copy(b.begin()+l, b.begin()+r, a.begin()+l);
}
public:
vector<int> countSmaller(vector<int>& nums) {
int n = nums.size();
a.resize(n);
b.resize(n);
c.assign(n, 0);
REP(i, n)
a[i] = pii{nums[i], i};
f(0, n);
return c;
}
};
// order statistics tree
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef pair<int, int> pii;
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> tr;
ROF(i, 0, nums.size()) {
tr.insert(pii{nums[i], i});
nums[i] = tr.order_of_key(pii{nums[i], 0});
}
return nums;
}
};