#P9853. [入门赛 #17] 方程求解

[入门赛 #17] 方程求解

Problem Description

Xiao A has nn equations about xx. The ii-th equation is in the form aixi+bi=cia_ix_i+b_i=c_i. The solutions xx are all positive integers. For example, the following equations all meet the requirement:

2x+4=10
-3x+13=10
4x-8=16

Among them, the solution to the first equation is x1=3x_1=3, the solution to the second equation is x2=1x_2=1, and the solution to the third equation is x3=6x_3=6.

Xiao A wants to know: given L,RL,R, within the range LxRL\leq x\leq R, how many positive integers xx satisfy that xx is a solution to at least one of the equations. To prevent you from tricking him, he will ask you QQ times.

Input Format

The first line contains two positive integers n,Qn,Q, representing the number of equations Xiao A has and the number of queries he will ask.

Starting from the second line, the next nn lines each contain a string describing an equation.

Starting from line (n+2)(n+2), the next QQ lines each contain two positive integers L,RL,R, representing one query: given L,RL,R, ask how many positive integers xx in the range LxRL\leq x\leq R satisfy that xx is a solution to at least one of the equations.

Output Format

For each query, output one line with one integer, indicating how many positive integers xx in the range LxRL\leq x\leq R satisfy that xx is a solution to at least one of the equations.

3 4
2x+4=10
-3x+13=10
4x-8=16
1 6
1 8
3 6
4 5
3
3
2
0
5 3
5x-2=13
8x+5=45
4x-12=8
-2x+10=4
3x-7=2
1 3
1 5
3 5
1
2
2

Hint

[Sample Explanation]

For the first sample group, it is the example given in the statement. The solutions of the three equations are x1=3,x2=1,x3=6x_1=3,x_2=1,x_3=6. Then:

  • For the range 1x61\leq x\leq 6, there are 33 values of xx (x=1,3,6x=1,3,6) that are solutions to at least one equation.
  • For the range 1x81\leq x\leq 8, it is the same as above.
  • For the range 3x63\leq x\leq 6, there are 22 values of xx (x=3,6x=3,6) that are solutions to at least one equation.
  • For the range 4x54\leq x\leq 5, there is no xx that is a solution to at least one equation.
  • Therefore, the outputs are 3,3,2,03,3,2,0.

For the second sample group, the solutions of the five equations are x1=3,x2=5,x3=5,x4=3,x5=3x_1=3,x_2=5,x_3=5,x_4=3,x_5=3. Then:

  • For the range 1x31\leq x\leq 3, only x=3x=3 is a solution to at least one equation.
  • For the range 1x51\leq x\leq 5, there are 22 values of xx (x=3,5x=3,5) that are solutions to at least one equation.
  • For the range 3x53\leq x\leq 5, there are 22 values of xx (x=3,5x=3,5) that are solutions to at least one equation.
  • Therefore, the outputs are 1,2,21,2,2.

[Constraints]

The testdata guarantees that 1n,Q2×1051\leq n,Q\leq 2\times 10^5. In each equation, ai,bi,cia_i,b_i,c_i satisfy 1ai,bi,ci1091 \leq |a_i|,|b_i|,|c_i| \leq 10^9, and the solution xix_i of each equation must be a positive integer. In each query, L,RL,R satisfy 1LR2×1091\leq L\leq R\leq 2\times 10^9.

The input size is large, so please pay attention to the efficiency of input and output in your code.

Translated by ChatGPT 5