L-R queries segment tree

My Solution for problem Problem passes all test cases except 2 of them are giving WA and I have followed everything as in editorial and also checked for overflow still WA. Can anyone find error ? Thanks.

You may refer to alternative solutions mentioned here

I have understood the solution,I just need to know where my solution is going wrong as it is passing almost all test cases.

I’ll have a look soon…

I’m out at the moment.

@rj25 actually multiset.erase(key) removes all elements equal to key. To remove a single instance, you can instead do multiset.erase(multiset.find(key)). Generally you should first check whether multiset.find(key) is past-the-end iterator, but key in guaranteed to exist in the multiset in this situation so it is not necessary. I made the change in your code, and it works- here :smiley:


I used multiset for the first time so didn’t knew about it. I was stuck on this for hours. Thanks a lot man. :slight_smile:

No problem :slight_smile:
I didn’t know about this too, but managed to find the bug by comparing with some accepted solutions.

1 Like