➤ Problem Link : RATING
👉 Hint : Policy based DS - ordered_set
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long int
#define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll> >, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
int main()
{
ll n;
cin>>n;
pair<pair<ll,ll>,ll> arr[n];
map<pair<ll,ll>,ll> rpt;
ll ans[n];
for(ll i=0;i<n;i++)
{
cin>>arr[i].first.first>>arr[i].first.second;
arr[i].second=i;
}
sort(arr,arr+n);
ordered_set oset;
unordered_map<ll,ll> cnt;
ans[arr[0].second]=0;
ll p=arr[0].first.second;
cnt[p]++;
oset.insert(mp(arr[0].first.second,cnt[arr[0].first.second]));
rpt[arr[0].first]=1;
for(ll i=1;i<n;i++)
{
cnt[arr[i].first.second]++;
oset.insert(mp(arr[i].first.second,cnt[arr[i].first.second]));
ans[arr[i].second]=oset.order_of_key(mp(arr[i].first.second,cnt[arr[i].first.second]))-rpt[arr[i].first];
rpt[arr[i].first]++;
}
for(auto it : ans)
cout<<it<<"\n";
}
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!
