luogu#P16384. [IATI 2025] Soft Drinks

[IATI 2025] Soft Drinks

Problem Description

Peter has recently taken up mixing soft drinks as a part-time hobby. He has purchased the access to an infinite supply of NN different drink types. Each ii-th drink type (0i<N0 \leq i < N) contains AiA_i grams of sugar per liter and BiB_i grams of acids per liter.

Peter is hosting a gathering at his home and has invited QQ of his closest friends. Each friend is quite health-conscious and picky and arrives with the following constraints:

  • MAM_A: the maximum total weight (in grams) of sugar they are willing to consume.
  • MBM_B: the maximum total weight (in grams) of acids they are willing to consume.
  • LAL_A, RAR_A: they will only drink from those drink types whose sugar concentration (sugar grams per liter) is in the range [LA,RA][L_A, R_A] (inclusive).

Peter can mix varying quantities (including fractional amounts) of each drink type to prepare a custom mocktail. Formally, let the real non-negative\textbf{real non-negative} ViV_i be the amount (in liters) of the ii-th drink used in the final mixture. Then:

  • The total volume of the mocktail is i=0N1Vi\sum_{i=0}^{N-1} V_i liters.
  • The total weight of sugar in the mixture is i=0N1Vi×Ai\sum_{i=0}^{N-1} V_i \times A_i grams.
  • The total weight of acids in the mixture is i=0N1Vi×Bi\sum_{i=0}^{N-1} V_i \times B_i grams.

Your task is to help Peter determine the maximum total volume (in liters)\textbf{maximum total volume (in liters)} he can serve to each friend, while respecting their individual constraints.

Implementation details

You need to implement the following functions as defined in soft_drinks.h\texttt{soft\_drinks.h}:

void init(const std::vector<int>& A, const std::vector<int>& B);

The init\verb|init| function gets called exactly once before any other queries. Its parameters correspond to:

  • A\texttt{A}: a vector of length NN where A[i]\texttt{A[i]} is the sugar content per liter of the ii-th drink.
  • B\texttt{B}: a vector of length NN where B[i]\texttt{B[i]} is the acid content per liter of the ii-th drink.
double friendDrink(int Ma, int Mb, int La, int Ra);

The friendDrink\verb|friendDrink| function gets called once for each of the QQ friends. Its parameters correspond to:

  • Ma\texttt{Ma}: the maximum total weight of sugar the friend will consume.
  • Mb\texttt{Mb}: the maximum total weight of acids the friend will consume.
  • La,Ra\texttt{La}, \texttt{Ra}: the inclusive bounds on the sugar concentration of the drink types the friend will accept drinking from.

The function should return a double\texttt{double} representing the maximum volume (in liters) that Peter can serve this friend without violating any of his constraints.

Your answer is considered correct, if its absolute or relative error doesn't exceed 10610^{-6}. Namely, if your answer is aa, and the correct answer is bb, then your answer is accepted, if abmax(1,b)106\frac{|a-b|}{\max(1, |b|)} \le 10^{-6}.

Local Testing

A local grader Lgrader.cpp\texttt{Lgrader.cpp} and the corresponding header file soft_drinks.h\texttt{soft\_drinks.h} are provided.

Input Format

Input format:

  • First line: integers NN and QQ.
  • Next NN lines: two integers AiA_i and BiB_i for each drink.
  • Next QQ lines: four integers MAM_A, MBM_B, LAL_A, and RAR_A for each friend.

The grader will call init\texttt{init}, followed by friendDrink\texttt{friendDrink} for each friend, printing the result to standard output.

4 3
4 0
2 8
6 4
3 6
8 2 4 6
8 2 3 6
0 10 0 10
2
2.0833333
0

Hint

Constraints

  • 1N,Q1051 \leq N, Q \leq 10^5
  • 0Ai,Bi,MA,MB,LA,RA1090 \leq A_i, B_i, M_A, M_B, L_A, R_A \leq 10^9
  • Either Ai0A_i \neq 0 or Bi0B_i \neq 0.

Subtasks

Subtask Points NN QQ Additional constraints
00 -- The sample test
11 66 2\leq 2 100\leq 100 --
22 99 105\leq 10^5 MA=MBM_A = M_B
33 77 MA=0M_A = 0, Ai=0A_i = 0
44 99 500\leq 500 --
55 1515 5000\leq 5000
66 77 105\leq 10^5 1000\leq 1000 (LA,RA)=(0,109)(L_A, R_A) = (0, 10^9)
77 1818 105\leq 10^5
88 2929 --

You get the points for a given subtask only if you correctly pass all tests in it and all other subtasks that are included in it.