Блог пользователя Bungmint

Автор Bungmint, история, 3 года назад, По-английски

I was trying to solve this problem on CodeChef, QCHEF, using Mo's algorithm and realized it is not so simple to come up with a data structure that can save previous results, so that you can simply obtain the result after deletion. I looked up how to maintain previous results, and found this nice comment, link, where it describes this abstract notion on how one could have one function, snapshot(), for saving the current results, and another function, rollback() to go back to the previous state. I tried to implement this, code below, but it failed miserably. Any help would be appreciated.

UPD: Apparently I had a typo in one of the add functions, and fixing that immediately solved the problem lol.

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

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I think a struct like the one below is really convenient for these types of problems.

Code

The usage is basically:

  • If you know that you will want to get back to the current state some time in the future, make a checkpoint.
  • Do whatever you want (maybe other checkpoints?), but call save on every variable of relevance before you change it (only once after every checkpoint is really necessary).
  • Call rollback to revert to the previous state (to the way things were when you last made a checkpoint).
  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Wow, that is pretty nice. I think I did something similar to what the struct does, but my implementation is nowhere near as structured as that. I will try to implement the structure and hope my issue resolves.

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

Auto comment: topic has been updated by Bungmint (previous revision, new revision, compare).