problems / 222

Staged Config Changes

medium design hashing stack simulation 1000 ms · 256 MB

Staged Config Changes

Your deployment tool keeps a store of feature flags. Each flag has a name and an integer value (a rollout percentage, a timeout, a version number). Operators don't edit the live store blindly: they open a change set, make edits, look at the result, and then either publish it or throw it away. Change sets can be nested, so a cautious operator can try something inside a change set that is itself still being tried.

Implement the store. It starts empty, with no change set open, and processes these commands:

  • SET k v: flag k now has value v (it is created if it does not exist).
  • UNSET k: flag k no longer exists. Nothing happens if it does not exist.
  • GET k: print the value of flag k, or NULL if it does not exist.
  • COUNT v: print how many flags currently have value v.
  • BEGIN: open a new change set, nested inside the currently open ones.
  • ROLLBACK: close the innermost open change set and undo every SET and UNSET made since it was opened (including those made inside change sets nested in it that were committed into it). Every flag returns to exactly the state it had when that BEGIN ran.
  • COMMIT: close the innermost open change set and keep its edits. If an enclosing change set is still open, the edits now belong to it, so rolling that one back later undoes them too. If no change set remains open, the edits are final.

If ROLLBACK or COMMIT is issued while no change set is open, print NO CHANGE SET and change nothing.

GET and COUNT always see the current state, including the edits of all open change sets.

Input

The first line contains q (1 ≤ q ≤ 2·10^5), the number of commands. Each of the next q lines contains one command in the format above.

Flag names k consist of 1 to 10 lowercase English letters and digits. Values v are integers with −10^9 ≤ v ≤ 10^9. At least one command is a GET or a COUNT. Change sets still open after the last command are simply left open.

Output

For every GET and COUNT, and for every ROLLBACK or COMMIT issued with no change set open, print one line with the result, in the order of the commands.

Notes

In the first example the inner rollback brings search back with value 50, and the outer one restores checkout to 50.

In the second example the first COMMIT has nothing to close. The second COMMIT merges the inner change set into the outer one, so the following ROLLBACK undoes the edits of both: beta and gamma are gone again. The last ROLLBACK has nothing to close.

Example 1

Input

14
SET checkout 50
SET search 50
COUNT 50
BEGIN
SET checkout 100
GET checkout
COUNT 50
BEGIN
UNSET search
COUNT 50
ROLLBACK
GET search
ROLLBACK
GET checkout

Output

2
100
1
0
50
50

Example 2

Input

13
COMMIT
BEGIN
SET beta 7
BEGIN
SET beta 9
SET gamma 9
COMMIT
COUNT 9
ROLLBACK
GET beta
GET gamma
COUNT 9
ROLLBACK

Output

NO CHANGE SET
2
NULL
NULL
0
NO CHANGE SET

Hints · reveal one at a time if you're stuck

Hint 1

GET and COUNT must be fast with up to 2·10^5 commands. What could you keep up to date on every SET and UNSET so that both answers are immediate?

Hint 2

Keep a stack with one marker per open change set, plus a log of previous states for each flag change made while a change set is open. ROLLBACK replays the log backwards down to the innermost marker, while COMMIT only removes that marker.

report an issue Prefer your own editor? sooks prep 222 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.