Ishan.nitj's blog

By Ishan.nitj, history, 10 years ago, In English

I am trying to solve this problem http://codeforces.me/contest/652/problem/D. My approach is http://codeforces.me/contest/652/submission/18906153 which is nlogn and should pass the time constraint.However I am getting TLE. I think this might be due std::distance which i have used to calculate distance b/w 2 elements in set.Please HELP.

  • Vote: I like it
  • +4
  • Vote: I do not like it

»
10 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

For random access iterators, std::distance is O(1). Unfortunately set iterator does not support random access, so the std::distance algorithm has to iterate over the pointers to compute distance, and worst case is O(n). You can achieve O(1) distance calc in sets if you use order-statistic balanced BST (there is some post in Codeforces teaching how to build this data structure using some hidden C++ libraries)