Budget Analysis

시간 제한4초메모리 제한1024 MB

요약
각 질의 구간과 정규화 계수에 대해 릿지 회귀 직선을 적합한 뒤 주어진 광고비에서의 예상 매출을 출력한다.
난이도

어려움10점 중 8점

유형
누적 합, 수학, 이분 탐색
정답자
아직 제출이 없습니다

문제

You are an analyst, studying the relationship between advertisement budget spending (denoted by xx) and sales (denoted by yy) over the period of nn months. More specifically, for every month of time from 1 to n you have the value of spending x_ix\_i and sales y_iy\_i.

To quantify the relationship you are using linear regression with regularisation, which means that you are modelling yy as y=Kx+By=Kx+B, where KK and BB are real numbers minimising the penalty function:

p(K,B)=∑((K⋅x_i+B−y_i)2)+λ⋅(K2+B2)p(K, B) = \sum \big( (K \cdot x\_i + B - y\_i)^2 \big) + \lambda \cdot (K^2 + B^2)

(Note: this is the standard penalty function for L2 regularised linear regression.)

For the report requested by your manager, you need to make several predictions. More specifically, you have a list of prediction queries, each described by four numbers --- L_jL\_j, R_jR\_j, λ_j\lambda\_j and X_jX\_j. To process such a query you need to perform the following steps:

  • take the spending and sales values for the months from L_jL\_j to R_jR\_j inclusive;
  • find the coefficients KK and BB, which minimise the penalty function for the given regularisation coefficient λ_j\lambda\_j;
  • plug the X_jX\_j into the resulting model and compute the prediction.

You are given the ads spending and sales data, and the prediction queries descriptions. You are to process the queries and output the predictions.

입력

First line of the input file contains an integer number nn (2≤n≤1062 \le n \le 10^6) denoting the number of months in the period you are studying.

Each of the following nn lines describes one month and contains two non-negative real numbers x_ix\_i and y_iy\_i not exceeding 10. They denote the budget spending and sales in the corresponding month.

The following line contains an integer number mm (1≤m≤1061 \le m \le 10^6) denoting the number of predictions to be made. Each of the following mm lines contains four numbers: L_jL\_j, R_jR\_j, lambda_jlambda\_j and X_jX\_j (1≤L_j<R_j≤n1 \le L\_j < R\_j \le n, 0≤λ_j,X_j≤100 \le \lambda\_j, X\_j \le 10). First two of them are integers, the remaining are real.

출력

For each prediction query output one real number on a separate line --- the predicted sales assuming the advertisement spending is X_jX\_j and the linear model has been fitted on months from L_jL\_j to R_jR\_j using L2-regularisation with λ_j\lambda\_j regularisation coefficient. The output must be accurate to an absolute or relative error of at most 10−610^{-6}.

예제2

  1. 예제 1

    입력
    5
    1 2
    3 4
    5 6
    7 8
    9 0
    2
    1 3 0 10
    1 5 1 10
    
    예상 출력
    11
    4.90566037735849125
    
  2. 예제 2

    입력
    3
    1 1.0
    2 2.1
    3 2.8
    3
    1 2 0 1.5
    2 3 0 2.5
    1 3 0 1.5
    
    예상 출력
    1.55
    2.45
    1.516666666666667