#include <iostream>
#include<bits/stdc++.h>
using namespace std;
void mergeHelp(vector<pair<int,int> >&store,int start,int mid,int end,vector<int>&ans){
vector<pair<int,int> >temp(end-start+1);
int i=start,j=mid,k=0,cnt=0;
while(i<mid && j<=end){
if(store[i].first<=store[j].first){
ans[store[i].second]+=cnt;
temp[k++]=store[i++];
}else{
temp[k++]=store[j++];
cnt++;
}
}
while(i<mid){
ans[store[i].second]+=cnt;
cout<<"store:"<<store[i].first<<endl;
temp[k++]=store[i++];
}
while(j<=end){
temp[k++]=store[j++];
}
j=start;
for(int i=0;i<k;++i){
store[j++]=temp[i];
// cout<<" "<<store[j-1].first<<" ";
}
//cout<<endl;
return;
}
void countSmaller(vector<pair<int,int> >&store,int start,int end,vector<int>&ans){
if(start<end){
int mid=(start+end)/2;
countSmaller(store,start,mid,ans);
countSmaller(store,mid+1,end,ans);
mergeHelp(store,start,mid+1,end,ans);
}
return;
}
vector<int> countSmaller(vector<int>& nums) {
int size=nums.size();
vector<pair<int,int> >store(size);
for(int i=0;i<size;++i){
store[i]=make_pair(nums[i],i);
}
vector<int>ans(size,0);
countSmaller(store,0,size-1,ans);
return ans;
}
int main() {
vector<int>nums={5, 2, 6, 1};
vector<int>ans=countSmaller(nums);
for(int i=0;i<ans.size();++i){
cout<<ans[i]<<" ";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKIHZvaWQgbWVyZ2VIZWxwKHZlY3RvcjxwYWlyPGludCxpbnQ+ID4mc3RvcmUsaW50IHN0YXJ0LGludCBtaWQsaW50IGVuZCx2ZWN0b3I8aW50PiZhbnMpewogICAgICAgIHZlY3RvcjxwYWlyPGludCxpbnQ+ID50ZW1wKGVuZC1zdGFydCsxKTsKICAgICAgICAKICAgICAgICBpbnQgaT1zdGFydCxqPW1pZCxrPTAsY250PTA7CiAgICAgICAgd2hpbGUoaTxtaWQgJiYgajw9ZW5kKXsKICAgICAgICAgICAgaWYoc3RvcmVbaV0uZmlyc3Q8PXN0b3JlW2pdLmZpcnN0KXsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgYW5zW3N0b3JlW2ldLnNlY29uZF0rPWNudDsKICAgICAgICAgICAgICAgIHRlbXBbaysrXT1zdG9yZVtpKytdOwogICAgICAgICAgICAgICAgCiAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICB0ZW1wW2srK109c3RvcmVbaisrXTsKICAgICAgICAgICAgICAgIGNudCsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgIH0KICAgICAgICAKICAgICAgICB3aGlsZShpPG1pZCl7CiAgICAgICAgICAgIGFuc1tzdG9yZVtpXS5zZWNvbmRdKz1jbnQ7CiAgICAgICAgICAgIGNvdXQ8PCJzdG9yZToiPDxzdG9yZVtpXS5maXJzdDw8ZW5kbDsKICAgICAgICAgICAgdGVtcFtrKytdPXN0b3JlW2krK107CiAgICAgICAgICAgIAogICAgICAgIH0KICAgICAgICAKICAgICAgICB3aGlsZShqPD1lbmQpewogICAgICAgICAgICB0ZW1wW2srK109c3RvcmVbaisrXTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgaj1zdGFydDsKICAgICAgICBmb3IoaW50IGk9MDtpPGs7KytpKXsKICAgICAgICAgICAgCiAgICAgICAgICAgIHN0b3JlW2orK109dGVtcFtpXTsKICAgICAgICAvLyAgICBjb3V0PDwiICI8PHN0b3JlW2otMV0uZmlyc3Q8PCIgIjsKICAgICAgICB9CiAgICAgICAgLy9jb3V0PDxlbmRsOwogICAgICAgIHJldHVybjsKICAgICAgICAKICAgIH0KICAgIAogICAgdm9pZCBjb3VudFNtYWxsZXIodmVjdG9yPHBhaXI8aW50LGludD4gPiZzdG9yZSxpbnQgc3RhcnQsaW50IGVuZCx2ZWN0b3I8aW50PiZhbnMpewogICAgICAgIGlmKHN0YXJ0PGVuZCl7CiAgICAgICAgICAgIGludCBtaWQ9KHN0YXJ0K2VuZCkvMjsKICAgICAgICAgICAgCiAgICAgICAgICAgIGNvdW50U21hbGxlcihzdG9yZSxzdGFydCxtaWQsYW5zKTsKICAgICAgICAgICAgY291bnRTbWFsbGVyKHN0b3JlLG1pZCsxLGVuZCxhbnMpOwogICAgICAgICAgICAKICAgICAgICAgICAgbWVyZ2VIZWxwKHN0b3JlLHN0YXJ0LG1pZCsxLGVuZCxhbnMpOwogICAgICAgIH0KICAgICAgICByZXR1cm47CiAgICB9CiAgICAKICAgIHZlY3RvcjxpbnQ+IGNvdW50U21hbGxlcih2ZWN0b3I8aW50PiYgbnVtcykgewogICAgICAgIGludCBzaXplPW51bXMuc2l6ZSgpOwogICAgICAgIHZlY3RvcjxwYWlyPGludCxpbnQ+ID5zdG9yZShzaXplKTsKICAgICAgICBmb3IoaW50IGk9MDtpPHNpemU7KytpKXsKICAgICAgICAgICAgc3RvcmVbaV09bWFrZV9wYWlyKG51bXNbaV0saSk7CiAgICAgICAgfQogICAgICAgIHZlY3RvcjxpbnQ+YW5zKHNpemUsMCk7CiAgICAgICAgY291bnRTbWFsbGVyKHN0b3JlLDAsc2l6ZS0xLGFucyk7CiAgICAgICAgcmV0dXJuIGFuczsKICAgICAgICAKICAgIH0KICAgIAogICAgCmludCBtYWluKCkgewoJdmVjdG9yPGludD5udW1zPXs1LCAyLCA2LCAxfTsKCXZlY3RvcjxpbnQ+YW5zPWNvdW50U21hbGxlcihudW1zKTsKCWZvcihpbnQgaT0wO2k8YW5zLnNpemUoKTsrK2kpewoJCWNvdXQ8PGFuc1tpXTw8IiAiOwoJfQoJcmV0dXJuIDA7Cn0=