#P11037. 【MX-X3-T4】「RiOI-4」上课

【MX-X3-T4】「RiOI-4」上课

Background

Original problem link: https://oier.team/problems/X3E


One day, Little M got up in the dorm at 6:536:53, and the morning self-study starts at 7:007:00

Problem Description

Given positive integers n,qn, q and nn intervals [li,ri][l_i, r_i]

There are qq queries. In each query, an integer xx is given. Choose an integer aia_i in each interval (liairil_i \leq a_i \leq r_i), so that the sum of all chosen integers equals xx, and the variance of the chosen sequence aa is minimized. Output the minimum variance modulo 998244353998\,244\,353. It is guaranteed that there is at least one valid selection.

For the definition of variance, refer to this Luogu paste. For taking a rational number modulo, refer to [Template] Rational Modulo

Input Format

The first line contains two positive integers n,qn, q

The next nn lines each contain two natural numbers li,ril_i, r_i

The last qq lines each contain an integer xx, representing one query. It is guaranteed that there exists at least one valid selection。

Output Format

Output qq lines, each containing one integer: the minimum variance modulo 998244353998\,244\,353

3 3
1 3
2 3
3 5
6
9
11
665496236
0
554580197
4 3
1 4
11 12
3 9
6 10
21
29
26
811073551
811073543
748683272

Hint

Sample Explanation #1

For query 1, a selection with minimum variance is 1,2,3{1,2,3}. The minimum variance is 23\frac{2}{3}. Since $665\,496\,236 \times 3 \equiv 2 \pmod{998\,244\,353}$, output 665496236665\,496\,236

For query 2, a selection with minimum variance is 3,3,3{3,3,3}. The minimum variance is 00. Since 0×10(mod998244353)0 \times 1 \equiv 0 \pmod{998\,244\,353}, output 00

For query 3, a selection with minimum variance is 3,3,5{3,3,5}. The minimum variance is 89\frac{8}{9}. Since $554\,580\,197 \times 9 \equiv 8 \pmod{998\,244\,353}$, output 554580197554\,580\,197

Constraints

This problem uses bundled testdata.

Subtask Score nn qq Special Property
11 99 5\le 5 ri5r_i \le 5
22 1313 2×103\le 2\times 10^3 ri2×103r_i \le 2\times 10^3
33 1616 106\le 10^6 =1= 1
44 2525 105\le 10^5 ri105r_i \le 10^5
55 3737 106\le 10^6

For all testdata, 1n,q1061 \leq n, q \leq 10^6, 0liri1060 \leq l_i \leq r_i \leq 10^6. For each xx, it is guaranteed that there exists a valid selection.

Translated by ChatGPT 5