Help me out in this problem

#include<bits/stdc++.h>
using namespace std;
int main(){

int n;
cin>>n;
int ar[n];
for(int i=0;i<n;i++)
{
	cin>>ar[i];
}
set<int> s;
int count[n];

set<int>::iterator it;


for(int i=n-1;i>=0;i--)
{
	s.insert(ar[i]);
	
	count[i]=s.lower_bound(ar[i])-s.begin();
}
for(int i=0;i<n;i++)
{
	cout<<count[i]<<" ";
}

}

Note that the iterators of a set are not like a normal vector. set is basically a BST, so you can’t use the iterators like this.

how can i use in coorect way can you modify the code

i think you got the logic…

Yes, I get it. But unfortunately, there is no such way with normal sets.

But luckily you can use Policy Bases Data Structures (popularly known as ordered set), with the same logic.

The other possible ways are using segment tree / fenwick tree and other is D&C.

ok bro i will try this…

1 Like