Bungmint's blog

By Bungmint, history, 3 years ago, In English

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
  • Vote: I like it
  • +13
  • Vote: I do not like it

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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