eolymp
bolt
Try our new interface for solving problems
Problems

Homer Simpsons barrels

Homer Simpsons barrels

Homer Simpson has barrels - there are different kinds of barrels to hold different kinds of liquids. For example, water barrels, oil barrels and so on. He keeps these barrels on a basement (i.e, below the ground floor) of his house in sequential order. Each barrel has capacity, which is equal to the volume of the liquid, when this barrel is filled as much as possible, with corresponding liquid. Let's denote the capacity of \textbf{i}'s barrel with \textbf{Cap}\[\textbf{i}\]. There are \textbf{n} barrels and numbering of them begins with \textbf{1}. So that, \textbf{1}st barrel has \textbf{Cap}\[\textbf{1}\] capacity, ..., \textbf{n}th barrel has \textbf{Cap}\[\textbf{n}\] capacity. Nowadays, Homer's son Bart Simpson is involved to stole liquids from city markets. He steals water, oil, yogurt, etc. - everything that can be specified as liquid. Everyday Bart comes to his father with specific liquid \textbf{l}, having volume \textbf{v}. Homer is not opposite to this situation, because everything, which is free, is acceptable by the laws of Simpson family. Homer tries to pour the liquid to one of the corresponding barrels. He does this using following strategy, which is called "Simpson Barrel Finding Algorithm" (\textbf{SBFA}): First, he finds all barrels which have their free volume ≥ \textbf{v} (initially, the free volume of each barrel is equal to its capacity). If there are several choices, he then finds all barrels which have their free volume as least as possible. If there are again several choices, Homer chooses barrel with minimum index. Let's say that, he chose the barrel with index \textbf{b}, using \textbf{SBFA}. Then Homer and Bart pour the liquid to \textbf{b}'th barrel, so free volume of \textbf{b}'th barrel decreases by \textbf{v}, from now on. \InputFile First line contains \textbf{3} integers: \textbf{n }- number of barrels on basement (\textbf{1} ≤ \textbf{n} ≤ \textbf{1000000}), \textbf{L} - number of liquid types \textbf{1} ≤ \textbf{L }≤ \textbf{1000}), \textbf{q }- number of distinct days, on which Bart stole something from markets (number of queries, \textbf{1 }≤ \textbf{q }≤ \textbf{100000}). Following line contains \textbf{n} integers, \textbf{i}'th integer equals to \textbf{Cap}\[\textbf{i}\]-capacity of \textbf{i}'th barrel (initial free volume, \textbf{1} ≤ \textbf{Cap}\[\textbf{i}\] ≤ \textbf{10^9}). Next line contains \textbf{n} integers, \textbf{i}'th integer equals to some number \textbf{l }- which indicates the type of \textbf{i}'th barrel (oil, water, soap, yogurt, etc., \textbf{1} ≤ \textbf{l} ≤ \textbf{L}). Next \textbf{q} lines contain queries. \textbf{i}'th query is defined by \textbf{2} numbers \textbf{l} and \textbf{v}, separated by space (\textbf{1} ≤ \textbf{l} ≤ \textbf{L}, \textbf{1} ≤ \textbf{v} ≤ \textbf{10^9}).\textbf{ Output} For each query you need to output on a separate line the index of the barrel \textbf{b} (\textbf{1} ≤ \textbf{b} ≤ \textbf{n}), which \textbf{SBFA} finds after processing this query. Notice that, after \textbf{SBFA} finishes, Homer and Bart may ignore the liquid, if there is no corresponding barrel with enough free volume. In this case, output \textbf{-1}.
Time limit 3 seconds
Memory limit 64 MiB
Input example #1
3 2 9
400 100 600
1 2 1
1 500
1 200
1 50
2 50
1 300
1 199
1 51
2 51
2 50
Output example #1
3
1
3
2
-1
1
-1
-1
2
Source 2014 Azerbaijan - Zadeh cup