Problem 693
Snapshot Array
Medium · 20 pts · Design, Binary Search, Hash Table
Problem
An array of N elements starts with every element equal to 0. Process Q operations. 'set i v' sets element i (counted from 0) to v. 'snap' takes a snapshot of the whole array and prints its id: the number of snapshots taken before it. 'get i s' prints the value element i had when snapshot s was taken. Copying the whole array at every snap is too slow; instead store, for every index, a list of (snap id, value) changes and binary search it.
Input
The first line contains N and Q. Each of the next Q lines contains one operation. Every get names an existing snapshot.
Output
Print the result of every snap and get, one per line.
Constraints
1 <= N, Q <= 50000; 0 <= i < N; 0 <= v <= 10^9
Example input
3 4 set 0 5 snap set 0 6 get 0 0
Example output
0 5
Explanation
Set element 0 to 5, then snap prints 0. After setting element 0 to 6, get 0 0 still prints 5, because snapshot 0 was taken before the change.