Cost of Living

Time limit2sMemory limit512 MB

Summary
Given some yearly inflation rates and commodity prices, use relationships between consecutive years to deduce unknown values and answer price queries.
Level

Medium6 of 10

Topics
Graph, Union-find, Math, Implementation
Solved
No attempts yet

Problem

A newspaper columnist recently wrote a column comparing Disneyland's price increases to other things in the economy; for example, if gasoline had increased at the same rate as Disneyland's admission since 1990, it would cost $6.66 per gallon.

Consider the prices of a number of commodities over a number of consecutive years. There is an inflation rate (inflation(x)>0\text{inflation}(x) > 0) for year xx to the next year (x+1x+1). Also, each commodity AA has a modifier (modifier(A)>0\text{modifier}(A) > 0) that is fixed for that commodity. So, for commodity AA in year x+1x+1:

price(A,x+1)=price(A,x)⋅inflation(x)⋅modifier(A)\text{price}(A, x+1) = \text{price}(A, x) \cdot \text{inflation}(x) \cdot \text{modifier}(A)

Unfortunately, the modifiers are unknown, and some of the prices and inflation rates are unknown.

Given some inflation rates, the prices for a number of commodities over a number of consecutive years, and some queries for prices for certain commodities in certain years, answer the queries.

Input

Each test case will begin with a line with three space-separated integers yy (1≤y≤101 \le y \le 10), cc (1≤c≤1001 \le c \le 100), and qq (1≤q≤y⋅c1 \le q \le y \cdot c), where yy is the number of consecutive years, cc is the number of commodities, and qq is the number of queries to answer.

Each of the next y−1y - 1 lines will contain a single real number rr (1.0≤r≤1.51.0 \le r \le 1.5, or r=−1.0r = -1.0), which are the inflation rates. A value of −1.0-1.0 indicates that the inflation rate is unknown. The first inflation rate is the change from year 1 to year 2, the second from year 2 to year 3, and so on. Known inflation rates will conform to the limits specified; unknown but uniquely determinable inflation rates may not, and are only guaranteed to be strictly greater than zero.

Each of the next yy lines will describe one year's prices. They will contain cc space-separated real numbers pp (1.0<p<1,000,000.01.0 < p < 1{,}000{,}000.0, or p=−1.0p = -1.0), which indicate the price of that commodity in that year. A value of −1.0-1.0 indicates that the price is unknown.

Each of the next qq lines will contain two space-separated integers aa (1≤a≤c1 \le a \le c) and bb (1≤b≤y1 \le b \le y), which represent a query for the price of commodity aa in year bb. All queries will be distinct.

All prices that can be uniquely determined will be strictly greater than zero and strictly less than 1,000,000.01{,}000{,}000.0. Values for prices and inflation rates in the input may not be exact, but will be accurate to 10 decimal places. All real values contain no more than 10 digits after the decimal point.

Output

Produce qq lines of output. Each line should contain a single real number, which is the price of the given commodity in the given year, or −1.0-1.0 if it cannot be determined. Answer the queries in the order that they appear in the input. Your answers will be accepted if they are within an absolute or relative error of 10−410^{-4}.

Examples1

  1. Example 1

    Input
    4 2 2
    1.3333333333
    1.2500000000
    -1
    3.00 -1
    4.00 8.00
    5.00 10.00
    -1 11.00
    2 1
    1 4
    
    Expected output
    6.0000000000
    5.5000000000