problems / 133

Config History Lookup

easy binary-search hashing strings 1000 ms · 256 MB

Config History Lookup

Your team's config service never overwrites anything: every change is appended to an audit log as a record SET key value t, meaning that from second t on, key holds value. The log is written by a single leader with a monotonic clock, so the times of SET records strictly increase through the log.

While the log is being written, engineers debugging incidents ask questions of the form GET key t: "what was key set to at second t?" The answer is the value of the SET record for key with the largest time that is at most t, or NULL if there is no such record. A query only sees the records that come before it in the log; it may ask about any time, including times before the first record or after the last one written so far.

Some keys are hot and change on every deploy, so a key may have a very long history. Answer every query.

Input

The first line contains q (1 ≤ q ≤ 2·10^5), the number of log lines. Each of the next q lines is one of:

  • SET key value t: set key to value from second t on;
  • GET key t: ask for the value of key at second t.

Keys are strings of 1 to 20 characters, each a lowercase English letter, a digit, . or _. Values are strings of 1 to 20 characters, each a lowercase English letter or a digit. All times satisfy 1 ≤ t ≤ 10^9. The times of the SET lines strictly increase in the order they appear; times of GET lines are arbitrary and unrelated to each other or to the SET times.

Output

For every GET line, in order, print one line with the answer: the value, or NULL if key had no value at second t according to the records written before the query.

Notes

In the first example, db.pool is set to 10 at second 5 and to 25 at second 12. A query at second 12 sees the change made at exactly that second, while a query at second 11 still sees 10, and a query at second 3 comes before any change, so its answer is NULL.

In the second example, the first query asks about second 100 but is written before the SET at second 50, so it cannot see it and its answer is NULL. The same query repeated after that SET returns on.

Example 1

Input

9
SET db.pool 10 5
GET db.pool 7
SET cache.ttl 300 8
SET db.pool 25 12
GET db.pool 12
GET db.pool 11
GET db.pool 3
GET cache.ttl 1000000000
GET log_level 20

Output

10
25
10
NULL
300
NULL

Example 2

Input

6
GET feature.beta 100
SET feature.beta on 50
GET feature.beta 100
SET feature.beta off 70
GET feature.beta 69
GET feature.beta 70

Output

NULL
on
on
off

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

Hint 1

Because SET times strictly increase in the log, what can you say about each key's history as records are appended? Think about what a GET must look up.

Hint 2

Keep each key's times and values in arrays as they arrive, and answer each GET with a binary search for the last time not after the query. Remember that a SET exactly at the query time counts, and that a GET only sees records written before it.

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

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