Comments

But how can we calculate -S(t) fast before adding node?

Firstly, standard map give you opportunity to get value by key in O(log n), unordered_map give O(1), but it can increase to O(n) in bad input. But this isn't the reason of your tle, the reason is limit on K in problem. K can be 10^9, it mean, that your solution will do 10^9 iteration in the end, this is so much for 2 sec.

My recommendation for you, you can iterate all pair in map with using construction for(auto el:[your map]{ Do something with el(el.first — key, el second — value) }