A coastal authority tracks its lighthouses across many inspection cycles. Each time a lighthouse is inspected, at most one intensity reading is logged for that lighthouse for that cycle — a lighthouse may be inspected in some cycles and skipped entirely in others, and no lighthouse ever receives two readings in the same cycle. Given the full archive of logged readings and a list of lookup requests, report the intensity reading for each requested lighthouse-and-cycle pair, or 0 if that lighthouse was never inspected during that exact cycle.
The first line contains two integers n and m — the number of archive entries and the number of lookup requests.
Each of the next n lines contains three integers id, cycle, and value — the lighthouse id, the inspection cycle, and the intensity reading logged for that lighthouse in that cycle.
Each of the next m lines contains two integers id and cycle — a lookup request.
Print m lines. For the i-th request, print the archived value for that (id, cycle) pair, or 0 if no archive entry matches it exactly.
(id, cycle) pair.Example 1
Input
3 4 1 2020 500 1 2021 -200 2 2020 800 1 2020 1 2021 1 2022 2 2020
Expected
500 -200 0 800
Explanation
The archive records three readings: lighthouse 1 in cycle 2020 (500), lighthouse 1 in cycle 2021 (-200), and lighthouse 2 in cycle 2020 (800). The four requests ask for (1,2020) -> 500, (1,2021) -> -200, (1,2022) which was never logged -> 0, and (2,2020) -> 800.
Example 2
Input
2 3 5 100 -300 5 101 400 5 100 5 99 5 101
Expected
-300 0 400
Explanation
Lighthouse 5 has logged readings in cycle 100 (-300) and cycle 101 (400). The request for cycle 99 has no matching archive entry, so it defaults to 0; the requests for cycles 100 and 101 return the archived values directly, including the negative reading.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →