#P8929. Piecewise Function Queries

    ID: 22093 Type: Default 1000ms 256MiB

Piecewise Function Queries

Piecewise Function Queries

You are given a piecewise function composed of n segments. Each segment is defined either by a linear function or a quadratic function. For a linear segment, the function is given by \(f(x)=ax+b\); for a quadratic segment, it is given by \(f(x)=ax^2+bx+c\). Each segment is defined over a closed interval \([l, r]\).

There are \(q\) queries. Each query is one of the following two types:

  • Type 1: Given \(x = k\), compute \(f(k)\) where \(k\) belongs to the domain of one of the segments.
  • Type 2: Given \(y = k\), count the total number of intersection points between the horizontal line \(y=k\) and the piecewise function. For each segment, you should count an intersection only if the corresponding solution \(x\) lies within the interval \([l, r]\) of that segment.

Note that for a quadratic function, the equation \(ax^2+bx+(c-k)=0\) must be solved using the discriminant \(\Delta=b^2-4a(c-k)\). Only count the solution(s) that fall within the segment's interval. It is guaranteed that for linear functions, the coefficient \(a\) is nonzero.

inputFormat

The first line contains two integers \(n\) and \(q\) representing the number of segments and the number of queries.

The next \(n\) lines describe each segment. Each segment is described as follows:

  • If the segment is linear: l r 1 a b where \([l, r]\) is the domain and \(f(x)=ax+b\).
  • If the segment is quadratic: l r 2 a b c where \([l, r]\) is the domain and \(f(x)=ax^2+bx+c\).

The following \(q\) lines represent the queries. Each query is given by:

  • For type 1: 1 k meaning query the value of \(f(k)\), where \(k\) will lie in one of the segments' domain.
  • For type 2: 2 k meaning count how many intersection points exist between the line \(y=k\) and the piecewise function.

outputFormat

For each query, output the corresponding answer on a separate line. For type 1 queries, output the computed value (which may be a real number). For type 2 queries, output the count of intersection points (an integer).

sample

2 3
0 2 1 2 1
3 5 2 1 -4 3
1 1
2 3
1 4
3

2 3

</p>