ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

how to efficiently find number of members less than x in a set<int>?
Posted by Howard Liu 2 May 2008 11:16
Suppose I have a set<int> S;

I want to know how many members of S are less than x. Naively I could do
set<int>::iterator it;
it = S.lower_bound(x);
distance(S.begin(), it);

However, distance() is linear time. Is there a way to find this quanity using STL in log time? I could write an AVL binary tree too, but I'm hoping for a simple way to do it.
Re: how to efficiently find number of members less than x in a set<int>?
Posted by [SPbSU ITMO] WiNGeR 3 May 2008 01:16
std::set does not support this operation. You must reimplement BST by your own.