5
int query(int index, int low, int high, int l, int r) {6
if (low >= l && high <= r) {9
if (high < l || low > r) return 0;11
int mid = (low + high) / 2;12
int left = query(2 * index + 1, low, mid, l, r);13
int right = query(2 * index + 2, mid + 1, high, l, r);17
void update(int index, int low, int high, int pos) {22
int mid = (low + high) / 2;24
update(2 * index + 1, low, mid, pos);26
update(2 * index + 2, mid + 1, high, pos);28
seg[index] = seg[2 * index + 1] + seg[2 * index + 2];32
int createSortedArray(vector<int> &instructions) {33
// using 100001 as 1e5 is the max element value34
seg.resize(4 * 100001, 0);37
for (int i = 0; i < instructions.size(); i++) {38
int mi = query(0, 0, 100001, 0,39
instructions[i] - 1); // number of elements smaller than instruction[i]40
int mx = query(0, 0, 100001, instructions[i] + 1,41
100001); // number of elements greater than instruction[i]42
update(0, 0, 100001, instructions[i]);43
ans = (ans + min(mi, mx)) % mod;47
/*TLE approch using STL49
int m=instructions.size();54
unordered_map<int,int>um;55
for(auto x:instructions)57
int xx=lower_bound(a.begin(),a.end(),x)-a.begin();68
a.insert(a.begin()+xx,x);72
ans=(ans+min(mi,m-mi-um[x]))%mod;