#C13971. FlatMap Data Structure Operations
FlatMap Data Structure Operations
FlatMap Data Structure Operations
This problem involves implementing a data structure called FlatMap that holds key-value pairs where both keys and values are integers. The structure allows you to perform the following operations:
put key value
: Insert a new key with its associated value or update the value if the key already exists.get key
: Retrieve the value associated with key. If the key does not exist, return0
.sumRange startKey endKey
: Compute the sum of the values of all keys in the inclusive range \( [startKey, endKey] \), i.e. \( \sum_{k=startKey}^{endKey} value(k) \).
You will be given several commands. For each command that produces output (get
and sumRange
), print the result on a new line.
Note: The number of operations is provided on the first line, followed by one operation per line.
inputFormat
Input is given via standard input (stdin). The first line contains an integer N representing the number of operations. The next N lines each contain one of the following commands:
put key value
get key
sumRange startKey endKey
outputFormat
For each get
and sumRange
operation, output the result to standard output (stdout) on a separate line.
7
put 1 5
put 2 3
put 1 2
get 1
get 2
get 10
sumRange 1 2
2
3
0
5
</p>