Physics Lab
Time limit2sMemory limit256 MB
Given cold and hot vessel volumes and temperatures C and H, decide for each reported fraction p/q whether some cold and hot vessel pair can produce that mixture temperature.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Hash map, Binary search
- Solved
- No attempts yet
Statement
Physics labs at school are interesting and educational. Not so much for the person who grades the reports, though. The school physics teacher Vitaly Evgenievich drew that short straw. The goal of the lab is to study how the temperature of water changes when the contents of two vessels with water at different temperatures are mixed.
The lab has n vessels of cold water at temperature C and m vessels of hot water at temperature H. Each vessel holds some volume. Students take turns choosing one vessel of hot water and one vessel of cold water, mix their contents, measure the resulting temperature, and then refill the vessels: the vessel that held cold water is filled back with cold water to the same volume as before the experiment, and the vessel that held hot water is likewise filled back with hot water.
After the lab, each of the k students submits a report giving the measured temperature of a mixture. The temperature is written as a fraction p/q. Assume the thermometer is exact and no energy is lost during the experiment. Then mixing liquids of volumes U and V at temperatures X and Y produces a mixture at temperature (UX + VY) / (U + V).
Some students were lazy and did not actually run the experiment, writing a made-up temperature in their report instead. Your task is to help Vitaly Evgenievich determine which students definitely cheated and which ones could really have obtained the temperature written in their report.
Input
The first line contains five integers n, m, C, H, and k (1 ≤ n, m, C, H, k ≤ 105, C < H). The second line contains n integers c1, ..., cn (1 ≤ ci ≤ 105), the volumes of the vessels with cold water. The third line contains m integers h1, ..., hm (1 ≤ hi ≤ 105), the volumes of the vessels with hot water. The next k lines each contain two integers pi and qi (1 ≤ pi, qi ≤ 105), the numerator and denominator of the fraction written in the report of student i.
Output
Print k lines. On line i, print "YES" if student i could have obtained the temperature pi/qi, and "NO" otherwise.