Блог пользователя Ishan.nitj

Автор Ishan.nitj, история, 8 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

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)